forms.py 3.9 KB
Newer Older
1
"""The forms defined by the registrations package"""
2
from django import forms
3
from django.core.exceptions import NON_FIELD_ERRORS, ValidationError
4
from django.forms import TypedChoiceField
5
from django.urls import reverse_lazy
6
from django.utils import timezone
7
from django.utils.safestring import mark_safe
8
from django.utils.text import capfirst
9 10
from django.utils.translation import ugettext_lazy as _

11 12
from members.models import Membership
from registrations import services
13
from utils.snippets import datetime_to_lectureyear
14
from .models import Registration, Renewal, Reference
15 16


17 18
class BaseRegistrationForm(forms.ModelForm):
    """Base form for membership registrations"""
19

20 21 22 23
    birthday = forms.DateField(
        widget=forms.widgets.SelectDateWidget(years=[
            year for year in range(timezone.now().year - 50,
                                   timezone.now().year - 10)]),
24
        label=capfirst(_('birthday'))
25 26 27 28 29 30
    )

    privacy_policy = forms.BooleanField(
        required=True,
    )

31 32 33 34 35 36 37 38 39 40
    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"""

41 42
    this_year = datetime_to_lectureyear(timezone.now())
    years = reversed([(x, "{} - {}".format(x, x + 1)) for x in
43
                      range(this_year - 20, this_year + 2)])
44 45 46 47

    starting_year = TypedChoiceField(
        choices=years,
        coerce=int,
48 49
        empty_value=this_year,
        required=False
50 51
    )

52 53 54
    class Meta:
        model = Registration
        fields = '__all__'
55
        exclude = ['created_at', 'updated_at', 'status', 'username',
56
                   'payment', 'membership']
57 58


59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
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):
75
    """Form for membership renewals"""
76 77 78 79 80

    privacy_policy = forms.BooleanField(
        required=True,
    )

81 82 83 84 85 86 87 88 89 90 91
    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')))

92 93 94
    class Meta:
        model = Renewal
        fields = '__all__'
95 96
        exclude = ['created_at', 'updated_at', 'status',
                   'payment', 'membership']
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122


class ReferenceForm(forms.ModelForm):
    def clean(self):
        super().clean()
        membership = self.cleaned_data['member'].current_membership
        if membership and membership.type == Membership.BENEFACTOR:
            raise ValidationError(_('Benefactors cannot give '
                                    'references.'))

        membership = self.cleaned_data['member'].latest_membership
        if (membership and membership.until and
                membership.until < services.calculate_membership_since()):
            raise ValidationError(_("It's not possible to give references for "
                                    "memberships that start after your own "
                                    "membership's end."))

    class Meta:
        model = Reference
        fields = '__all__'
        error_messages = {
            NON_FIELD_ERRORS: {
                'unique_together':
                    _("You've already given a reference for this person."),
            }
        }