-
Notifications
You must be signed in to change notification settings - Fork 1
/
profile.py
102 lines (85 loc) 路 2.95 KB
/
profile.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
import enum
from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import CheckConstraint
from django.utils.translation import gettext_lazy as _
from django_countries.fields import CountryField
from apps.utils.models import BaseTimestampedModel
from apps.utils.models.query import Q
class UserProfile(BaseTimestampedModel):
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
on_delete=models.RESTRICT,
related_name="profile",
verbose_name=_("user"),
)
nationality = CountryField(blank=True, verbose_name=_("nationality"))
# TODO: define relation to section, membership
home_university = models.ForeignKey(
"universities.University",
on_delete=models.RESTRICT,
verbose_name=_("home university"),
help_text=_("home university for all users"),
related_name="home_university_user_profiles",
null=True,
blank=True,
db_index=True,
)
home_faculty = models.ForeignKey(
"universities.Faculty",
on_delete=models.RESTRICT,
verbose_name=_("home faculty"),
help_text=_("home faculty for members, empty for internationals"),
related_name="home_faculty_user_profiles",
null=True,
blank=True,
db_index=True,
)
guest_faculty = models.ForeignKey(
"universities.Faculty",
on_delete=models.RESTRICT,
verbose_name=_("guest faculty"),
help_text=_("guest faculty for international students, empty for members"),
related_name="guest_user_profiles",
blank=True,
null=True,
db_index=True,
)
# TODO: profile picture
picture = models.CharField
# TODO: phone, profiles
@enum.unique
class Preferences(enum.Flag):
WEEKLY_UPDATES = enum.auto()
# TODO: push notifications & emails
# TODO: define formfield/widget to handle flagging
preferences = models.PositiveSmallIntegerField(
default=0, verbose_name=_("user preferences as flags")
)
class Meta:
verbose_name = _("user profile")
verbose_name_plural = _("user profiles")
constraints = (
CheckConstraint(
# home university XOR home faculty
check=Q(home_university=None) ^ Q(home_faculty=None),
name="home_university_or_faculty",
),
)
def clean(self):
super().clean()
if not (bool(self.home_university) ^ bool(self.home_faculty)):
raise ValidationError(
{
"home_university": _(
"At least one from home university/faculty has to be set."
)
}
)
def __str__(self):
return (
f"{self.user} {self.nationality} "
f"{self.home_university or self.home_faculty.university} "
)
__all__ = ["UserProfile"]