diff --git a/apps/member/forms.py b/apps/member/forms.py index b34a3260..e546d652 100644 --- a/apps/member/forms.py +++ b/apps/member/forms.py @@ -25,6 +25,12 @@ class ProfileForm(forms.ModelForm): A form for the extras field provided by the :model:`member.Profile` model. """ + def save(self, commit=True): + if not self.instance.section or (("department" in self.changed_data + or "promotion" in self.changed_data) and "section" not in self.changed_data): + self.instance.section = self.instance.section_generated + return super().save(commit) + class Meta: model = Profile fields = '__all__' diff --git a/apps/member/models.py b/apps/member/models.py index 4be1b1a4..f4b4865d 100644 --- a/apps/member/models.py +++ b/apps/member/models.py @@ -3,6 +3,7 @@ import datetime import os +from math import ceil from django.conf import settings from django.contrib.auth.models import User @@ -23,18 +24,20 @@ class Profile(models.Model): We do not want to patch the Django Contrib :model:`auth.User`model; so this model add an user profile with additional information. - """ + user = models.OneToOneField( settings.AUTH_USER_MODEL, on_delete=models.CASCADE, ) + phone_number = models.CharField( verbose_name=_('phone number'), max_length=50, blank=True, null=True, ) + section = models.CharField( verbose_name=_('section'), help_text=_('e.g. "1A0", "9A♥", "SAPHIRE"'), @@ -42,12 +45,44 @@ class Profile(models.Model): blank=True, null=True, ) + + department = models.CharField( + max_length=8, + verbose_name=_("department"), + choices=[ + ('A0', _("Informatics (A0)")), + ('A1', _("Mathematics (A1)")), + ('A2', _("Physics (A2)")), + ("A'2", _("Applied physics (A'2)")), + ('A''2', _("Chemistry (A''2)")), + ('A3', _("Biology (A3)")), + ('B1234', _("SAPHIRE (B1234)")), + ('B1', _("Mechanics (B1)")), + ('B2', _("Civil engineering (B2)")), + ('B3', _("Mechanical engineering (B3)")), + ('B4', _("EEA (B4)")), + ('C', _("Design (C)")), + ('D2', _("Economy-management (D2)")), + ('D3', _("Social sciences (D3)")), + ('E', _("English (E)")), + ('EXT', _("External (EXT)")), + ] + ) + + promotion = models.PositiveIntegerField( + null=True, + default=datetime.date.today().year, + verbose_name=_("promotion"), + help_text=_("Year of entry to the school (None if not ENS student)"), + ) + address = models.CharField( verbose_name=_('address'), max_length=255, blank=True, null=True, ) + paid = models.BooleanField( verbose_name=_("paid"), help_text=_("Tells if the user receive a salary."), @@ -64,6 +99,23 @@ class Profile(models.Model): default=False, ) + @property + def ens_year(self): + """ + Number of years since the 1st august of the entry year, rounded up. + """ + if self.promotion is None: + return 0 + today = datetime.date.today() + years = today.year - self.promotion + if today.month >= 8: + years += 1 + return years + + @property + def section_generated(self): + return str(self.ens_year) + self.department + @property def soge(self): if "treasury" in settings.INSTALLED_APPS: diff --git a/apps/member/views.py b/apps/member/views.py index 4a9f1995..04742f32 100644 --- a/apps/member/views.py +++ b/apps/member/views.py @@ -526,6 +526,13 @@ class ClubAddMemberView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): # Now, all is fine, the membership can be created. + if club.name == "BDE": + # When we renew the BDE membership, we update the profile section. + # We could automate that and remove the section field from the Profile model, + # but with this way users can customize their section as they want. + user.profile.section = user.profile.section_generated + user.profile.save() + # Credit note before the membership is created. if credit_amount > 0: if not last_name or not first_name or (not bank and credit_type.special_type == "Chèque"): diff --git a/templates/wei/weimembership_form.html b/templates/wei/weimembership_form.html index c1a5d7fe..0cde7fe6 100644 --- a/templates/wei/weimembership_form.html +++ b/templates/wei/weimembership_form.html @@ -33,6 +33,12 @@ {% endif %} +
{% trans 'department'|capfirst %}
+
{{ registration.user.profile.department }}
+ +
{% trans 'ENS year'|capfirst %}
+
{{ registration.user.profile.ens_year }}
+
{% trans 'section'|capfirst %}
{{ registration.user.profile.section }}