models.py 7.11 KB
Newer Older
1
import datetime
2

3
from django.db import models
4
from django.core import validators
5
6
7
from django.conf import settings
from django.utils.translation import ugettext_lazy as _

8
9
10
11
12
13
14
15
16
17
18
19
from localflavor.generic.countries.sepa import IBAN_SEPA_COUNTRIES
from localflavor.generic.models import IBANField


class Member(models.Model):
    """This class describes a member"""

    # No longer yearly membership as a type, use expiration date instead.
    MEMBERSHIP_TYPES = (
        ('benefactor', _('Benefactor')),
        ('member', _('Member')),
        ('honorary', _('Honorary Member')))
20

21
22
23
    PROGRAMME_CHOICES = (
        ('computingscience', _('Computing Science')),
        ('informationscience', _('Information Sciences')))
24

25
    user = models.OneToOneField(
26
27
28
29
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
    )

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    # ----- Registration information -----

    programme = models.CharField(
        max_length=20,
        choices=PROGRAMME_CHOICES,
        verbose_name=_('Study programme'),
        blank=True,
        null=True,
    )

    student_number = models.CharField(
        max_length=8,
        validators=[validators.RegexValidator(
            regex=r'(s\d{7}|[ezu]\d{6,7})',
            message=_('Enter a valid student- of e/z/u-number.'))],
        blank=True,
        null=True,
    )

49
    type = models.CharField(
50
        max_length=40,
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
        choices=MEMBERSHIP_TYPES,
        verbose_name=_('Membership type'),
    )

    registration_year = models.IntegerField(
        verbose_name=_('Registration year'),
        help_text=_('The year this member first became a part of Thalia'),
    )

    membership_expiration = models.DateField(
        verbose_name=_('Expiration date of membership'),
        help_text=_('Let the membership expire after this time'),
        null=True,
        blank=True,
    )

    # ---- Address information -----

    address_street = models.CharField(
        max_length=100,
        validators=[validators.RegexValidator(
            regex=r'^.+ \d+.+',
            message=_('Include the house number'),
        )],
        verbose_name=_('Street and house number'),
        null=True,
    )

    address_street2 = models.CharField(
        max_length=100,
        verbose_name=_('Second address line'),
        blank=True,
        null=True,
    )

    address_postal_code = models.CharField(
        max_length=10,
        verbose_name=_('Postal code'),
        null=True,
    )

    address_city = models.CharField(
        max_length=40,
        verbose_name=_('City'),
        null=True,
    )

    phone_number = models.CharField(
Thom Wiggers's avatar
Thom Wiggers committed
99
        max_length=20,
100
101
102
103
        verbose_name=_('Phone number'),
        help_text=_('Enter a phone number so Thalia may reach you'),
        validators=[validators.RegexValidator(
            regex=r'^\+?\d+$',
104
            message=_('Please enter a valid phone number'),
105
106
107
108
109
        )],
        null=True,
        blank=True,
    )

110
    # ---- Emergency contact ----
111

112
113
114
115
    emergency_contact = models.CharField(
        max_length=255,
        verbose_name=_('Emergency contact name'),
        help_text=_('Who should we contact in case of emergencies'),
116
117
118
119
        null=True,
        blank=True,
    )

120
    emergency_contact_phone_number = models.CharField(
Thom Wiggers's avatar
Thom Wiggers committed
121
        max_length=20,
122
123
        verbose_name=_('Emergency contact phone number'),
        help_text=_('The phone number for the emergency contact'),
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
        validators=[validators.RegexValidator(
            regex=r'^\+?\d+$',
            message=_('Voer een geldig telefoonnummer in.'),
        )],
        null=True,
        blank=True,
    )

    # ---- Personal information ------

    birthday = models.DateField(
        verbose_name=_('Birthday'),
        null=True
    )

139
140
141
142
143
144
145
146
    show_birthday = models.BooleanField(
        verbose_name=_('Display birthday'),
        help_text=_(
            'Show the birthday on your profile page and '
            'in the birthday calendar'),
        default=True,
    )

147
148
149
150
151
152
153
154
155
156
157
    website = models.URLField(
        max_length=200,
        verbose_name=_('Website'),
        help_text=_('Website to display on your profile page'),
        blank=True,
        null=True
    )

    profile_description = models.TextField(
        verbose_name=_('Profile text'),
        help_text=_('Text to display on your profile'),
158
        blank=True,
159
160
161
162
163
164
165
166
167
    )

    nickname = models.CharField(
        max_length=30,
        verbose_name=_('Nickname'),
        blank=True,
        null=True,
    )

168
169
170
171
172
173
174
175
176
177
178
    display_name_preference = models.CharField(
        max_length=10,
        verbose_name=_('How to display name'),
        choices=(('full', _('Show full name')),
                 ('nickname', _('Show only nickname')),
                 ('initials', _('Show initials and last name')),
                 ('fullnick', _("Show name like \"John 'nickname' Doe\"")),
                 ('nicklast', _("Show nickname and last name"))),
        default='full',
    )

179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
    # --- Communication preference ----

    language = models.CharField(
        verbose_name=_('Preferred language'),
        help_text=_('Preferred language for e.g. news letters'),
        max_length=5,
        choices=settings.LANGUAGES,
        default='nl',
    )

    receive_optin = models.BooleanField(
        verbose_name=_('Receive opt-in mailings'),
        help_text=_("Receive mailings about vacancies and events from Thalia's"
                    " sponsors."),
        default=True,
    )

    # --- Direct debit information ----

    direct_debit_authorized = models.BooleanField(
        choices=((True, _('Yes, I want Thalia to take the membership fees '
                          'from my bank account through direct debit for '
                          'each year.')),
                 (False, _('No, I will pay the contribution myself'))),
        verbose_name=_('Direct debit'),
        help_text=_('Each year, have Thalia take the membership fees from my '
                    'bank account'),
        default=False,
    )

    bank_account = IBANField(
        verbose_name=_('Bank account'),
211
212
213
        help_text=_('Bank account for direct debit'),
        include_countries=IBAN_SEPA_COUNTRIES,
        blank=True,
214
    )
215
216
217
218
219
220

    def is_active(self):
        """Is this member currently active

        Tested by checking if the expiration date has passed.
        """
Thom Wiggers's avatar
Thom Wiggers committed
221
        return self.membership_expiration > datetime.utcnow()
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
    # Special properties for admin site
    is_active.boolean = True
    is_active.short_description = _('Is this user currently active')

    def display_name(self):
        pref = self.display_name_preference
        if pref == 'nickname':
            return self.nickname
        elif pref == 'initials':
            return '{} {}'.format(self.initials, self.user.last_name)
        elif pref == 'fullnick':
            return "{} '{}' {}".format(self.user.first_name,
                                       self.nickname,
                                       self.user.last_name)
        elif pref == 'nicklast':
            return "'{}' {}".format(self.nickname,
                                    self.user.last_name)
        else:
240
            return self.user.get_full_name()
241
    display_name.short_description = _('Display name')
242
243
244

    def __str__(self):
        return self.display_name()