-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodels.py
598 lines (484 loc) · 19.5 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
from datetime import date
from django.core.exceptions import ValidationError
from django.db import models
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from ldb.querysets import EntityQuerySet, PersonQuerySet
from .country_field import CountryField
from .validators import validate_google_username
def get_attributes(self, attrs):
v = {}
for attr in attrs:
v[self._meta.get_field(attr).verbose_name] = self.__getattribute__(attr)
return v
class Entity(models.Model):
class Meta:
verbose_name = _("entity")
verbose_name_plural = _("entities")
# Address
street_name = models.CharField(_("street name"), max_length=75, blank=True)
house_number = models.CharField(_("house number"), max_length=7, blank=True)
address_2 = models.CharField(_("address row 2"), max_length=75, blank=True)
address_3 = models.CharField(_("address row 3"), max_length=75, blank=True)
postcode = models.CharField(_("postcode"), max_length=10, blank=True)
city = models.CharField(_("city"), max_length=50, blank=True)
country = CountryField(_("country"), blank=True)
email = models.EmailField(_("e-mail"), blank=True)
# Telephone
phone_fixed = models.CharField(_("phone fixed"), max_length=16, blank=True)
# Subscriptions
machazine = models.BooleanField(_("MaCHazine"), default=True)
# Other
comment = models.TextField(blank=True)
objects = EntityQuerySet.as_manager()
@property
def street_address(self):
strings = [
self.street_name + " " + self.house_number,
self.address_2,
self.address_3,
]
return "\n".join(filter(None, strings))
@property
def formatted_address(self):
strings = [
self.street_address,
self.postcode
+ (" " + self.city.upper() if self.postcode != "INTERN" else ""),
self.get_country_display() if self.country != "NL" else "",
]
return "\n".join(filter(None, strings))
@property
def subscriptions(self):
return get_attributes(
self,
[
"machazine",
],
)
def clean(self):
if (
self.street_name != ""
or self.house_number != ""
or self.address_2 != ""
or self.address_3 != ""
or self.postcode != ""
or self.city != ""
) and self.country == "":
raise ValidationError("Country is required if address is entered.")
def __str__(self):
return "{} {}, {} {}, {}".format(
self.street_name,
self.house_number,
self.postcode,
self.city,
self.country,
)
def set_address_incorrect(self):
self.street_name = ""
self.house_number = ""
self.address_2 = ""
self.address_3 = ""
self.postcode = ""
self.city = ""
self.country = ""
class Organization(Entity):
class Meta:
verbose_name = _("organization")
verbose_name_plural = _("organizations")
name_prefix = models.CharField(_("name prefix"), max_length=100, blank=True)
name = models.CharField(_("name"), max_length=100)
name_short = models.CharField(_("name short"), max_length=100, blank=True)
salutation = models.CharField(_("salutation"), max_length=100)
def get_absolute_url(self):
return reverse("ldb_organizations_detail", args=[str(self.id)])
def __str__(self):
return str(self.name)
class MembershipStatus:
NONE = 0
DONATING = 10
ALUMNUS = 20
REGULAR = 30
ASSOCIATE = 40
MERIT = 50
HONORARY = 60
labels = {
NONE: _("Not a member"),
DONATING: _("Donating member"),
ALUMNUS: _("Alumnus member"),
REGULAR: _("Regular member"),
ASSOCIATE: _("Associate member"),
MERIT: _("Merit member"),
HONORARY: _("Honorary member"),
}
@classmethod
def choices(cls):
return [(value, label) for value, label in cls.labels.items()]
class MembershipStatusField(models.IntegerField):
def __init__(self, enum, *args, **kwargs):
self.enum = enum
kwargs["choices"] = self.enum.choices()
super().__init__(*args, **kwargs)
def deconstruct(self):
name, path, args, kwargs = super().deconstruct()
if self.enum is not None:
kwargs["enum"] = self.enum
if "choices" in kwargs:
del kwargs["choices"]
return name, path, args, kwargs
class Person(Entity):
class Meta:
verbose_name = _("person")
verbose_name_plural = _("people")
GENDER_CHOICES = (
("M", _("Male")),
("F", _("Female")),
("NB", _("Non-binary")),
("O", _("Other")),
)
# Name
titles = models.CharField(_("titles"), max_length=20, blank=True)
initials = models.CharField(_("initials"), max_length=15)
firstname = models.CharField(_("first name"), max_length=50)
preposition = models.CharField(
_("preposition"), max_length=15, blank=True
) # Tussenvoegsel
surname = models.CharField(_("surname"), max_length=100)
postfix_titles = models.CharField(_("postfix titles"), max_length=20, blank=True)
# Telephone
phone_mobile = models.CharField(_("phone mobile"), max_length=16, blank=True)
# Other
gender = models.CharField(
_("gender"), max_length=2, choices=GENDER_CHOICES, blank=True
)
pronouns = models.CharField(_("pronouns"), max_length=100, blank=True)
birthdate = models.DateField(_("birthdate"), blank=True, null=True)
deceased = models.BooleanField(_("deceased"), default=False)
# https://code.djangoproject.com/ticket/7689
# _living_with =models.ManyToManyField('self')
living_with = models.OneToOneField(
"self", blank=True, null=True, on_delete=models.SET_NULL
)
# Subscriptions
mail_announcements = models.BooleanField(_("announcements mailing"), default=True)
mail_company = models.BooleanField(_("company mailing"), default=True)
mail_education = models.BooleanField(_("education mailing"), default=True)
# Internal Account
ldap_username = models.CharField(
_("LDAP username"), max_length=64, blank=True, null=True, unique=True
)
google_username = models.CharField(
_("Google username"), max_length=64, blank=True, null=True, unique=True
)
# External Accounts
netid = models.CharField(
_("NetID"), max_length=64, blank=True, null=True, unique=True
)
linkedin_id = models.CharField(
_("LinkedIn ID"), max_length=64, blank=True, null=True, unique=True
)
facebook_id = models.CharField(
_("Facebook ID"), max_length=64, blank=True, null=True, unique=True
)
# Membership status
_membership_status = MembershipStatusField(
enum=MembershipStatus,
db_column="membership_status",
default=MembershipStatus.NONE,
)
_original_living_with_id = None
objects = PersonQuerySet.as_manager()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._original_living_with_id = self.living_with_id
@property
def membership_status(self):
try:
member = self.member
except Member.DoesNotExist:
return MembershipStatus.NONE
if member.current_honorary_member:
return MembershipStatus.HONORARY
if member.current_merit_member:
return MembershipStatus.MERIT
if member.current_associate_member:
return MembershipStatus.ASSOCIATE
if member.current_regular_member:
return MembershipStatus.REGULAR
if member.current_alumnus_member:
return MembershipStatus.ALUMNUS
if member.current_donating_member:
return MembershipStatus.DONATING
else:
return MembershipStatus.NONE
@property
def valid_membership_status(self):
# We cannot do this in clean() because children aren't saved then,
# and we can't do this in save() because we can't raise nice errors there.
try:
member = self.member
if (
member.current_member
and self.membership_status == MembershipStatus.NONE
):
return False
except Member.DoesNotExist:
return True
@property
def formatted_name(self):
strings = [
self.titles,
self.initials,
self.preposition,
self.surname,
self.postfix_titles,
]
return " ".join(filter(None, strings))
@property
def subscriptions(self):
return get_attributes(
self,
[
"machazine",
"mail_announcements",
"mail_company",
"mail_education",
],
)
@property
def age(self):
born = self.birthdate
today = date.today()
# raised when birth date is February 29 and the current year is not a leap year
try:
birthday = born.replace(year=today.year)
except ValueError:
birthday = born.replace(year=today.year, day=born.day - 1)
if birthday > today:
return today.year - born.year - 1
else:
return today.year - born.year
def save(self, **kwargs):
self._membership_status = self.membership_status
super().save(**kwargs)
validate_google_username(self.google_username, self)
if self.pk is not None:
old = self._original_living_with_id
if old != self.living_with_id and old:
other = Person.objects.get(pk=old)
if other.living_with == self:
other.living_with = None
other.save()
if self.living_with:
other = self.living_with
# Avoid save loops
if (
other.living_with != self
or other.street_name != self.street_name
or other.house_number != self.house_number
or other.address_2 != self.address_2
or other.address_3 != self.address_3
or other.postcode != self.postcode
or other.city != self.city
or other.country != self.country
):
other.living_with = self
other.street_name = self.street_name
other.house_number = self.house_number
other.address_2 = self.address_2
other.address_3 = self.address_3
other.postcode = self.postcode
other.city = self.city
other.country = self.country
other.save()
self.__original_living_with = self.living_with
def if_student_number(self):
if hasattr(self, "student"):
return self.student.student_number
else:
return None
def get_absolute_url(self):
return reverse("ldb_people_detail", args=[str(self.id)])
def __str__(self):
return (
"%s, %s %s%s"
% (
self.surname,
self.firstname,
self.preposition,
" ✝" if self.deceased else "",
)
).strip()
class Member(models.Model):
class Meta:
verbose_name = _("member")
verbose_name_plural = _("members")
person = models.OneToOneField(Person, primary_key=True, on_delete=models.CASCADE)
date_from = models.DateField(_("date from"), blank=True, null=True)
date_to = models.DateField(_("date to"), blank=True, null=True)
date_paid = models.DateField(_("date paid"), blank=True, null=True)
amount_paid = models.IntegerField(_("amount paid"), blank=True, null=True)
associate_member = models.BooleanField(_("associate member"), default=False)
donating_member = models.BooleanField(_("donating member"), default=False)
merit_date_from = models.DateField(
_("merit member date from"), blank=True, null=True
)
merit_invitations = models.BooleanField(_("merit member invitations"), default=True)
merit_history = models.TextField(_("merit member history"), blank=True)
honorary_date_from = models.DateField(
_("honorary member from date"), blank=True, null=True
)
@property
def current_member(self):
return (
self.date_from is not None
and (self.date_to is None or self.date_to > date.today())
and not self.person.deceased
)
@property
def current_regular_member(self):
try:
student = self.person.student
return student.enrolled and self.current_member
except Student.DoesNotExist:
return False
@property
def current_alumnus_member(self):
return (
self.current_member
and Alumnus.objects.filter(person=self.person).exists() > 0
and not self.current_regular_member
)
@property
def current_associate_member(self):
return self.associate_member and self.current_member
@property
def current_donating_member(self):
return self.donating_member and self.current_member
@property
def current_merit_member(self):
return self.merit_date_from is not None and self.current_member
@property
def current_honorary_member(self):
return self.honorary_date_from is not None and self.current_member
def __str__(self):
return str(self.person)
def clean(self):
if (
self.date_from is not None
and self.date_to is not None
and self.date_from > self.date_to
) or (self.date_to is not None and self.date_from is None):
raise ValidationError("'Date to' cannot be before 'date from'")
if self.date_to is not None and (
self.merit_date_from is not None or self.honorary_date_from is not None
):
raise ValidationError(
"'Date to' cannot be set for merit and honorary members"
)
if self.merit_date_from is not None and self.merit_date_from > date.today():
raise ValidationError("Merit date from should be in the past")
if (
self.honorary_date_from is not None
and self.honorary_date_from > date.today()
):
raise ValidationError("Honorary date from should be in the past")
if self.merit_date_from is not None and self.date_from is None:
raise ValidationError(
"Cannot have a 'merit from date' without a 'date from'. (Merit member"
" must be a regular member too)"
)
if self.honorary_date_from is not None and self.date_from is None:
raise ValidationError(
"Cannot have a 'honorary from date' without a 'date from'. (Honorary"
" member must be a regular member too)"
)
if self.date_to is None and self.associate_member:
raise ValidationError("'Date to' is required for associate members")
if self.date_to is None and self.donating_member:
raise ValidationError("'Date to' is required for donating members")
class Student(models.Model):
class Meta:
verbose_name = _("student")
verbose_name_plural = _("students")
person = models.OneToOneField(Person, primary_key=True, on_delete=models.CASCADE)
enrolled = models.BooleanField(_("enrolled"), default=True)
study = models.CharField(_("study"), max_length=50)
first_year = models.IntegerField(_("first year"), blank=True, null=True)
student_number = models.CharField(
_("student number"), max_length=7, blank=True, null=True, unique=True
)
emergency_name = models.CharField(_("emergency name"), max_length=48, blank=True)
emergency_phone = models.CharField(_("emergency phone"), max_length=16, blank=True)
date_verified = models.DateField(_("date verified"), blank=True, null=True)
def __str__(self):
return "{} {}".format(self.student_number, str(self.person))
CONTACT_METHOD_CHOICES = (("m", "Mail"), ("e", "Email"))
class Alumnus(models.Model):
class Meta:
verbose_name = _("alumnus")
verbose_name_plural = _("alumni")
person = models.OneToOneField(Person, primary_key=True, on_delete=models.CASCADE)
study = models.CharField(_("study"), max_length=100, blank=True)
study_first_year = models.IntegerField(_("study first year"), blank=True, null=True)
study_last_year = models.IntegerField(_("study last year"), blank=True, null=True)
study_research_group = models.CharField(
_("research group"), max_length=100, blank=True
)
study_paper = models.CharField(_("paper"), max_length=300, blank=True)
study_professor = models.CharField(_("professor"), max_length=100, blank=True)
work_company = models.CharField(_("company"), max_length=100, blank=True)
work_position = models.CharField(_("position"), max_length=100, blank=True)
work_sector = models.CharField(_("sector"), max_length=100, blank=True)
contact_method = models.CharField(
_("contact method"), max_length=1, choices=CONTACT_METHOD_CHOICES, default="e"
)
def __str__(self):
return str(self.person)
class Employee(models.Model):
class Meta:
verbose_name = _("employee")
verbose_name_plural = _("employees")
person = models.OneToOneField(Person, primary_key=True, on_delete=models.CASCADE)
faculty = models.CharField(_("faculty"), max_length=50)
department = models.CharField(_("department"), max_length=50)
function = models.CharField(_("function"), max_length=50)
phone_internal = models.CharField(_("phone internal"), max_length=5)
def __str__(self):
return str(self.person)
class Committee(models.Model):
class Meta:
verbose_name = _("committee")
verbose_name_plural = _("committees")
ordering = ["name"]
name = models.CharField(_("name"), max_length=50)
description = models.TextField(_("description"), blank=True)
members = models.ManyToManyField(Person, through="CommitteeMembership")
def __str__(self):
return self.name
class CommitteeMembership(models.Model):
class Meta:
verbose_name = _("committee membership")
verbose_name_plural = _("committee memberships")
ordering = ["board", "committee__name"]
# Django admin doesn't support nested inlines,
# so we'll just link to Person instead.
person = models.ForeignKey(
Person, related_name="committee_memberships", on_delete=models.CASCADE
)
committee = models.ForeignKey(Committee, on_delete=models.CASCADE)
board = models.IntegerField(_("board"))
position = models.CharField(_("position"), max_length=50, blank=True)
ras_months = models.IntegerField(_("RAS months"), blank=True, null=True)
def __str__(self):
return "[{}] {} - {}".format(self.board, self.committee, self.person)
class Modification(models.Model):
class Meta:
verbose_name = _("modification")
verbose_name_plural = _("modifications")
person = models.ForeignKey(Person, on_delete=models.CASCADE)
date = models.DateTimeField(_("date"), auto_now_add=True)
ip = models.CharField(_("ip address"), max_length=40)
modification = models.TextField(_("modification"), blank=True)
def __str__(self):
return "Edit [{}] {}".format(self.date, self.person.__str__())