diff --git a/kfet/forms.py b/kfet/forms.py index 5cc9d83f..8ab301a1 100644 --- a/kfet/forms.py +++ b/kfet/forms.py @@ -1,4 +1,4 @@ -from datetime import timedelta +from datetime import date, timedelta from decimal import Decimal from django import forms @@ -50,7 +50,23 @@ class DateTimeWidget(forms.DateTimeInput): # ----- +def default_promo(): + now = date.today() + return now.month <= 8 and now.year - 1 or now.year + + +def get_promo_choices(): + return [("", "Sans promo")] + [(r, r) for r in range(1980, date.today().year + 1)] + + class AccountForm(forms.ModelForm): + promo = forms.TypedChoiceField( + choices=get_promo_choices, + coerce=int, + empty_value=None, + initial=default_promo, + required=False, + ) # Surcharge pour passer data à Account.save() def save(self, data={}, *args, **kwargs): diff --git a/kfet/migrations/0079_auto_20210627_0022.py b/kfet/migrations/0079_auto_20210627_0022.py new file mode 100644 index 00000000..52900ed0 --- /dev/null +++ b/kfet/migrations/0079_auto_20210627_0022.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.17 on 2021-06-26 22:22 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("kfet", "0078_negative_end"), + ] + + operations = [ + migrations.AlterField( + model_name="account", + name="promo", + field=models.IntegerField(null=True), + ), + ] diff --git a/kfet/models.py b/kfet/models.py index 887d3701..8e9a6e42 100644 --- a/kfet/models.py +++ b/kfet/models.py @@ -1,5 +1,4 @@ import re -from datetime import date from django.contrib.auth.models import User from django.core.validators import RegexValidator @@ -19,11 +18,6 @@ from .config import kfet_config from .utils import to_ukf -def default_promo(): - now = date.today() - return now.month <= 8 and now.year - 1 or now.year - - class AccountManager(models.Manager): """Manager for Account Model.""" @@ -66,10 +60,7 @@ class Account(models.Model): is_frozen = models.BooleanField("est gelé", default=False) created_at = models.DateTimeField(default=timezone.now) # Optional - PROMO_CHOICES = [(r, r) for r in range(1980, date.today().year + 1)] - promo = models.IntegerField( - choices=PROMO_CHOICES, blank=True, null=True, default=default_promo() - ) + promo = models.IntegerField(null=True) nickname = models.CharField("surnom(s)", max_length=255, blank=True, default="") password = models.CharField( max_length=255, unique=True, blank=True, null=True, default=None diff --git a/kfet/templates/kfet/left_account.html b/kfet/templates/kfet/left_account.html index a68845ed..aa22cb03 100644 --- a/kfet/templates/kfet/left_account.html +++ b/kfet/templates/kfet/left_account.html @@ -35,7 +35,13 @@
  • {{ account.nickname }}
  • {% endif %}
  • {{ account.email|default:"Pas d'email!" }}
  • -
  • {{ account.departement }} {{ account.promo }}
  • +
  • + {% if account.promo %} + {{ account.departement }} {{ account.promo }} + {% else %} + Sans promo + {% endif %} +
  • {% if account.is_cof %} \ No newline at end of file +