views.py 4.4 KB
Newer Older
1
from django.contrib import messages
2
3
4
5
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404, redirect
from django.utils import timezone
from django.utils.decorators import method_decorator
6
from django.utils.translation import ugettext_lazy as _
7
8
from django.views import View
from django.views.generic import DetailView, TemplateView, FormView
Thom Wiggers's avatar
Thom Wiggers committed
9

10
11
from events import services
from events.exceptions import RegistrationError
12
from .forms import FieldsForm
13
from .models import Event, Registration
14

Luuk Scholten's avatar
Luuk Scholten committed
15

16
17
class EventIndex(TemplateView):
    template_name = 'events/index.html'
18

19
20
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
21

22
23
24
25
26
        upcoming_activity = Event.objects.filter(
            published=True,
            end__gte=timezone.now()
        ).order_by('end').first()
        context['upcoming_activity'] = upcoming_activity
27

28
        return context
29
30


31
32
33
34
35
class EventDetail(DetailView):
    model = Event
    queryset = Event.objects.filter(published=True)
    template_name = 'events/event.html'
    context_object_name = 'event'
36

37
38
39
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['user'] = self.request.user
40

41
42
43
44
        event = context['event']
        if event.max_participants:
            perc = 100.0 * len(event.participants) / event.max_participants
            context['registration_percentage'] = perc
45

46
47
48
        try:
            context['registration'] = Registration.objects.get(
                event=event,
49
                member=self.request.member
50
            )
51
        except (Registration.DoesNotExist, TypeError):
52
            pass
53

54
55
        context['permissions'] = services.event_permissions(
                self.request.member, event)
56

57
        return context
58

59

60
61
62
63
@method_decorator(login_required, name='dispatch')
class EventRegisterView(View):
    def get(self, request, *args, **kwargs):
        return redirect('events:event', pk=kwargs['pk'])
64

65
66
67
    def post(self, request, *args, **kwargs):
        event = get_object_or_404(Event, pk=kwargs['pk'])
        try:
68
            services.create_registration(request.member, event)
69
70

            if event.has_fields():
71
72
73
74
75
                return redirect('events:registration', event.pk)
            else:
                messages.success(request, _("Registration successful."))
        except RegistrationError as e:
            messages.error(request, e)
76
77
78
79

        return redirect(event)


80
81
82
83
@method_decorator(login_required, name='dispatch')
class EventCancelView(View):
    def get(self, request, *args, **kwargs):
        return redirect('events:event', pk=kwargs['pk'])
84

85
86
    def post(self, request, *args, **kwargs):
        event = get_object_or_404(Event, pk=kwargs['pk'])
87
        try:
88
            services.cancel_registration(request, request.member, event)
89
90
91
92
            messages.success(request,
                             _("Registration successfully cancelled."))
        except RegistrationError as e:
            messages.error(request, e)
93

94
        return redirect(event)
95
96


97
98
99
100
101
@method_decorator(login_required, name='dispatch')
class RegistrationView(FormView):
    form_class = FieldsForm
    template_name = 'events/registration.html'
    event = None
102

103
104
105
106
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['event'] = self.event
        return context
107

108
109
    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
110
        kwargs["fields"] = services.registration_fields(self.request.member,
111
112
                                                        self.event)
        return kwargs
113

114
115
116
    def form_valid(self, form):
        values = form.field_values()
        try:
117
118
            services.update_registration(self.request.member, self.event,
                                         values)
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
            messages.success(self.request,
                             _("Registration successfully saved."))
            return redirect(self.event)
        except RegistrationError as e:
            messages.error(self.request, e)
            return self.render_to_response(self.get_context_data(form=form))

    def dispatch(self, request, *args, **kwargs):
        self.event = get_object_or_404(Event, pk=self.kwargs['pk'])
        try:
            if self.event.has_fields():
                return super().dispatch(request, *args, **kwargs)
        except RegistrationError:
            pass
        return redirect(self.event)