diff --git a/elections/forms.py b/elections/forms.py index 0ea9a6d..dbcc88d 100644 --- a/elections/forms.py +++ b/elections/forms.py @@ -78,8 +78,14 @@ class OptionForm(forms.ModelForm): class Meta: model = Option - fields = [*Option.text.fields] + fields = [*Option.text.fields, "abbreviation"] widgets = {"text_fr": forms.TextInput, "text_en": forms.TextInput} + help_texts = { + "abbreviation": _( + "L'abbréviation est optionnelle et sert à identifier plus facilement les " + "différentes options. Elle est affiché sans espaces et en majuscules." + ) + } class DeleteVoteForm(forms.Form): @@ -102,7 +108,7 @@ class SelectVoteForm(forms.ModelForm): # We set the option's text as the label for the checkbox instance = kwargs.get("instance", None) if instance is not None: - self.fields["selected"].label = instance.text + self.fields["selected"].label = str(instance) selected = forms.BooleanField(required=False) @@ -118,7 +124,7 @@ class RankVoteForm(forms.ModelForm): # We set the option's text as the label for the rank instance = kwargs.get("instance", None) if instance is not None: - self.fields["rank"].label = instance.text + self.fields["rank"].label = str(instance) rank = forms.IntegerField(required=False) diff --git a/elections/migrations/0023_option_abbreviation.py b/elections/migrations/0023_option_abbreviation.py new file mode 100644 index 0000000..b0fd1b0 --- /dev/null +++ b/elections/migrations/0023_option_abbreviation.py @@ -0,0 +1,20 @@ +# Generated by Django 3.2 on 2021-04-16 23:21 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("elections", "0022_auto_20210415_1050"), + ] + + operations = [ + migrations.AddField( + model_name="option", + name="abbreviation", + field=models.CharField( + blank=True, max_length=8, verbose_name="abbréviation" + ), + ), + ] diff --git a/elections/models.py b/elections/models.py index 570c5d2..04358f7 100644 --- a/elections/models.py +++ b/elections/models.py @@ -137,6 +137,7 @@ class Option(models.Model): Question, related_name="options", on_delete=models.CASCADE ) text = TranslatedFieldWithFallback(models.TextField(_("texte"), blank=False)) + abbreviation = models.CharField(_("abbréviation"), max_length=8, blank=True) winner = models.BooleanField(_("option gagnante"), default=False) voters = models.ManyToManyField( @@ -148,7 +149,15 @@ class Option(models.Model): # For now, we store the amount of votes received after the election is tallied nb_votes = models.PositiveSmallIntegerField(_("nombre de votes reçus"), default=0) + def save(self, *args, **kwargs): + # On enlève les espaces et on passe tout en majuscules + self.abbreviation = "".join(self.abbreviation.upper().split()) + + super().save(*args, **kwargs) + def __str__(self): + if self.abbreviation: + return self.abbreviation + " - " + self.text return self.text class Meta: diff --git a/elections/templates/elections/election.html b/elections/templates/elections/election.html index 338b9e9..c7eec48 100644 --- a/elections/templates/elections/election.html +++ b/elections/templates/elections/election.html @@ -179,7 +179,7 @@
- {{ q.text }} + {{ q }}
{% if q in cast_questions %} @@ -211,7 +211,7 @@ {% endif %} - {{ o.text }} + {{ o }}
{% endfor %} diff --git a/elections/templates/elections/election_admin.html b/elections/templates/elections/election_admin.html index 87a9323..00bd6e0 100644 --- a/elections/templates/elections/election_admin.html +++ b/elections/templates/elections/election_admin.html @@ -163,7 +163,7 @@
- {{ q.text }} + {{ q }}
{% if election.start_date > current_time %} @@ -232,7 +232,7 @@ {% endif %} - {{ o.text }} + {{ o }}
{% endfor %}