views.py 8.02 KB
Newer Older
1
"""Views provided by the registrations package"""
2
from django.conf import settings
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from django.contrib import messages
from django.contrib.admin.utils import model_ngettext
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.shortcuts import redirect
from django.template.defaultfilters import floatformat
from django.urls import reverse
from django.utils import timezone
from django.utils.decorators import method_decorator
from django.utils.translation import ugettext_lazy as _
from django.views import View
from django.views.generic import FormView
from django.views.generic.base import TemplateResponseMixin, TemplateView

from members.models import Membership
Thom Wiggers's avatar
Thom Wiggers committed
20
from . import emails, forms, services
21
from .models import Entry, Registration, Renewal
Thom Wiggers's avatar
Thom Wiggers committed
22

23
24

class BecomeAMemberView(TemplateView):
25
    """View that render a HTML template with context data"""
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    template_name = 'registrations/become_a_member.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['year_fees'] = floatformat(settings.MEMBERSHIP_PRICES[
                                               Entry.MEMBERSHIP_YEAR], 2)
        context['study_fees'] = floatformat(settings.MEMBERSHIP_PRICES[
                                                Entry.MEMBERSHIP_STUDY], 2)
        context['member_form_url'] = reverse('registrations:register')
        return context


@method_decorator(staff_member_required, name='dispatch')
@method_decorator(permission_required('registrations.review_entries'),
                  name='dispatch', )
class EntryAdminView(View):
42
    """
43
    View that handles the processing of entries
44
    """
45

46
47
    def post(self, request, *args, **kwargs):
        action = request.POST.get('action')
48
49
50
51
52
53
        entry_qs = Entry.objects.filter(pk=kwargs['pk'])
        try:
            entry = entry_qs.get()
        except Entry.DoesNotExist:
            return redirect('admin:index')

54
        if action == 'accept':
55
56
57
58
59
60
61
62
63
64
            if not services.check_unique_user(entry):
                messages.error(request, _('Could not accept %s. '
                                          'Username is not unique.') %
                               model_ngettext(entry, 1))
            elif services.accept_entries(entry_qs) > 0:
                messages.success(request, _('Successfully accepted %s.') %
                                 model_ngettext(entry, 1))
            else:
                messages.error(request, _('Could not accept %s.') %
                               model_ngettext(entry, 1))
65
        elif action == 'reject':
66
67
68
69
70
71
            if services.reject_entries(entry_qs) > 0:
                messages.success(request, _('Successfully rejected %s.') %
                                 model_ngettext(entry, 1))
            else:
                messages.error(request, _('Could not reject %s.') %
                               model_ngettext(entry, 1))
72
73
74
75
76
        elif action == 'resend':
            try:
                emails.send_registration_email_confirmation(entry.registration)
            except Registration.DoesNotExist:
                pass
77
78
79
80
81
82
83
84
85
86
87
88

        if entry_qs.filter(renewal=None).exists():
            content_type = ContentType.objects.get_for_model(Registration)
        else:
            content_type = ContentType.objects.get_for_model(Renewal)

        return redirect("admin:%s_%s_change" %
                        (content_type.app_label, content_type.model),
                        kwargs['pk'])


class ConfirmEmailView(View, TemplateResponseMixin):
89
90
91
92
    """
    View that renders an HTML template and confirms the email address
    of the provided registration
    """
93
94
95
96
97
    template_name = 'registrations/confirm_email.html'

    def get(self, request, *args, **kwargs):
        entry = Entry.objects.filter(pk=kwargs['pk'])

98
        processed = 0
99
100
101
102
103
        try:
            processed = services.confirm_entry(entry)
        except ValidationError:
            pass

104
105
106
        if processed == 0:
            return redirect('registrations:register')

107
        emails.send_new_registration_board_message(entry.get())
108
109
110
111
112

        return self.render_to_response({})


class MemberRegistrationFormView(FormView):
113
114
115
    """
    View that renders the membership registration form
    """
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    form_class = forms.MemberRegistrationForm
    template_name = 'registrations/register_member.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['year_fees'] = floatformat(settings.MEMBERSHIP_PRICES[
                                               Entry.MEMBERSHIP_YEAR], 2)
        context['study_fees'] = floatformat(settings.MEMBERSHIP_PRICES[
                                                Entry.MEMBERSHIP_STUDY], 2)
        context['privacy_policy_url'] = reverse('privacy-policy')
        return context

    def get(self, request, *args, **kwargs):
        if request.user.is_authenticated:
            return redirect('registrations:renew')
        return super().get(request, args, kwargs)

    def post(self, request, *args, **kwargs):
        request.POST = request.POST.dict()
        request.POST['language'] = request.LANGUAGE_CODE
        request.POST['membership_type'] = Membership.MEMBER
        return super().post(request, *args, **kwargs)

    def form_valid(self, form):
        form.save()
        return redirect('registrations:register-success')


@method_decorator(login_required, name='dispatch')
class RenewalFormView(FormView):
146
147
148
    """
    View that renders the membership renewal form
    """
149
150
151
152
153
154
155
156
157
158
    form_class = forms.MemberRenewalForm
    template_name = 'registrations/renewal.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['year_fees'] = floatformat(settings.MEMBERSHIP_PRICES[
                                               Entry.MEMBERSHIP_YEAR], 2)
        context['study_fees'] = floatformat(settings.MEMBERSHIP_PRICES[
                                                Entry.MEMBERSHIP_STUDY], 2)
        context['membership'] = self.request.member.latest_membership
159
160
        context['was_member'] = (Membership.objects.filter(user=self.request.member,
                                                           type=Membership.MEMBER).exists())
161
162
163
        if context['membership'] is not None:
            context['membership_type'] = (context['membership']
                                          .get_type_display())
164
        context['privacy_policy_url'] = reverse('privacy-policy')
165
166
167
168
169
        return context

    def get_form(self, form_class=None):
        form = super().get_form(form_class)
        member = self.request.member
170
        if member is not None and member.latest_membership is not None:
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
            latest_membership = member.latest_membership
            # If latest membership has not ended or does not ends
            # within 1 month: do not show 'year' length
            hide_year_choice = not (latest_membership is not None and
                                    latest_membership.until is not None and
                                    (latest_membership.until -
                                     timezone.now().date()).days <= 31)

            if hide_year_choice:
                form.fields['length'].choices = [
                    c for c in form.fields['length'].choices
                    if c[0] != Entry.MEMBERSHIP_YEAR
                ]

        return form

    def post(self, request, *args, **kwargs):
        request.POST = request.POST.dict()
189
190
        if request.member.latest_membership.type == Membership.BENEFACTOR:
            request.POST['membership_type'] = Membership.BENEFACTOR
191
            request.POST['length'] = Entry.MEMBERSHIP_YEAR
192
193
194
195
196
197
198
        request.POST['member'] = request.member.pk
        return super().post(request, *args, **kwargs)

    def form_valid(self, form):
        renewal = form.save()
        emails.send_new_renewal_board_message(renewal)
        return redirect('registrations:renew-success')