From a621bb81974721df8493e7004c3c5b7f67b51147 Mon Sep 17 00:00:00 2001 From: Tom Hubrecht Date: Thu, 16 Sep 2021 16:43:27 +0200 Subject: [PATCH] =?UTF-8?q?On=20s=C3=A9rialise=20les=20mod=C3=A8les=20pour?= =?UTF-8?q?=20transmettre=20en=20JSON,=20on=20am=C3=A9liore=20le=20formula?= =?UTF-8?q?ire=20modal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- elections/models.py | 9 +++- .../templates/elections/admin/option.html | 2 +- .../templates/elections/admin/question.html | 4 +- .../templates/elections/election_admin.html | 14 ++---- elections/views.py | 19 +++++++- shared/json/__init__.py | 10 ++++ shared/json/mixins.py | 21 +++++++++ shared/templates/forms/modal-form.html | 46 +++++++++---------- 8 files changed, 87 insertions(+), 38 deletions(-) create mode 100644 shared/json/__init__.py create mode 100644 shared/json/mixins.py diff --git a/elections/models.py b/elections/models.py index 12593df..d90a6f4 100644 --- a/elections/models.py +++ b/elections/models.py @@ -7,6 +7,7 @@ from django.template.loader import render_to_string from django.utils.translation import gettext_lazy as _ from shared.auth import CONNECTION_METHODS +from shared.json import Serializer from shared.utils import choices_length from .staticdefs import ( @@ -86,7 +87,7 @@ class Election(models.Model): ordering = ["-start_date", "-end_date"] -class Question(models.Model): +class Question(Serializer, models.Model): election = models.ForeignKey( Election, related_name="questions", on_delete=models.CASCADE ) @@ -110,6 +111,8 @@ class Question(models.Model): blank=True, ) + serializable_fields = ["text_en", "text_fr", "type"] + def is_form_valid(self, vote_form): validate_function = getattr(ValidateFunctions, VALIDATE_FUNCTIONS[self.type]) return vote_form.is_valid() and validate_function(vote_form) @@ -154,7 +157,7 @@ class Question(models.Model): ordering = ["id"] -class Option(models.Model): +class Option(Serializer, models.Model): question = models.ForeignKey( Question, related_name="options", on_delete=models.CASCADE ) @@ -171,6 +174,8 @@ 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) + serializable_fields = ["text_fr", "text_en", "abbreviation"] + def save(self, *args, **kwargs): # On enlève les espaces et on passe tout en majuscules self.abbreviation = "".join(self.abbreviation.upper().split()) diff --git a/elections/templates/elections/admin/option.html b/elections/templates/elections/admin/option.html index fc5a6e3..cae4b93 100644 --- a/elections/templates/elections/admin/option.html +++ b/elections/templates/elections/admin/option.html @@ -9,7 +9,7 @@ - + diff --git a/elections/templates/elections/admin/question.html b/elections/templates/elections/admin/question.html index 2eea1a7..b7a6f26 100644 --- a/elections/templates/elections/admin/question.html +++ b/elections/templates/elections/admin/question.html @@ -21,7 +21,7 @@ - + @@ -56,7 +56,7 @@ {# Rajout d'une option #} {% if q.election.start_date > current_time %}
- + - -