forms.py 2.72 KB
Newer Older
1
"""The forms defined by the registrations package"""
2
from django import forms
3
from django.forms import TypedChoiceField
4
from django.urls import reverse_lazy
5
from django.utils import timezone
6
from django.utils.safestring import mark_safe
7
8
from django.utils.translation import ugettext_lazy as _

9
from utils.snippets import datetime_to_lectureyear
Thom Wiggers's avatar
Thom Wiggers committed
10
from .models import Registration, Renewal
11
12


13
14
class BaseRegistrationForm(forms.ModelForm):
    """Base form for membership registrations"""
15

16
17
18
19
20
21
22
23
24
25
26
    birthday = forms.DateField(
        widget=forms.widgets.SelectDateWidget(years=[
            year for year in range(timezone.now().year - 50,
                                   timezone.now().year - 10)]),
        label=_('birthday')
    )

    privacy_policy = forms.BooleanField(
        required=True,
    )

27
28
29
30
31
32
33
34
35
36
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['privacy_policy'].label = mark_safe(_(
            'I accept the <a href="{}">privacy policy</a>.').format(
            reverse_lazy('privacy-policy')))


class MemberRegistrationForm(BaseRegistrationForm):
    """Form for member registrations"""

37
38
    this_year = datetime_to_lectureyear(timezone.now())
    years = reversed([(x, "{} - {}".format(x, x + 1)) for x in
39
                      range(this_year - 20, this_year + 2)])
40
41
42
43

    starting_year = TypedChoiceField(
        choices=years,
        coerce=int,
44
45
        empty_value=this_year,
        required=False
46
47
    )

48
49
50
    class Meta:
        model = Registration
        fields = '__all__'
51
        exclude = ['created_at', 'updated_at', 'status', 'username',
52
                   'payment', 'membership']
53
54


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class BenefactorRegistrationForm(BaseRegistrationForm):
    """Form for benefactor registrations"""

    icis_employee = forms.BooleanField(
        required=False,
        label=_('I am an employee of iCIS')
    )

    class Meta:
        model = Registration
        fields = '__all__'
        exclude = ['created_at', 'updated_at', 'status', 'username',
                   'starting_year', 'programme', 'payment', 'membership']


class RenewalForm(forms.ModelForm):
71
    """Form for membership renewals"""
72
73
74
75
76

    privacy_policy = forms.BooleanField(
        required=True,
    )

77
78
79
80
81
82
83
84
85
86
87
    icis_employee = forms.BooleanField(
        required=False,
        label=_('I am an employee of iCIS')
    )

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['privacy_policy'].label = mark_safe(_(
            'I accept the <a href="{}">privacy policy</a>.').format(
            reverse_lazy('privacy-policy')))

88
89
90
    class Meta:
        model = Renewal
        fields = '__all__'
91
92
        exclude = ['created_at', 'updated_at', 'status',
                   'payment', 'membership']