diff --git a/elections/forms.py b/elections/forms.py index 7da7185..a773319 100644 --- a/elections/forms.py +++ b/elections/forms.py @@ -71,13 +71,6 @@ class VoteForm(forms.ModelForm): selected = forms.BooleanField(required=False) - def record_vote(self, user): - self.full_clean() - if self.cleaned_data["selected"]: - self.instance.voters.add(user) - else: - self.instance.voters.remove(user) - class Meta: model = Option fields = [] diff --git a/elections/models.py b/elections/models.py index 7bd04e1..f20c20a 100644 --- a/elections/models.py +++ b/elections/models.py @@ -3,8 +3,8 @@ from django.contrib.auth.models import AbstractUser from django.db import models from django.utils.translation import gettext_lazy as _ -from .staticdefs import CONNECTION_METHODS, QUESTION_TYPES -from .utils import choices_length +from .staticdefs import CAST_FUNCTIONS, CONNECTION_METHODS, QUESTION_TYPES +from .utils import CastFunctions, choices_length # ############################################################################# # Models regarding an election @@ -72,6 +72,10 @@ class Question(models.Model): blank=True, ) + def cast_ballot(self, user, vote_form): + cast_function = getattr(CastFunctions, CAST_FUNCTIONS[self.type]) + cast_function(user, vote_form) + class Meta: ordering = ["id"] diff --git a/elections/staticdefs.py b/elections/staticdefs.py index c643a93..d0d5f16 100644 --- a/elections/staticdefs.py +++ b/elections/staticdefs.py @@ -29,3 +29,7 @@ VOTE_RULES = { "Vous pouvez également ne sélectionner aucune option." ) } + +CAST_FUNCTIONS = { + "assentiment": "cast_select", +} diff --git a/elections/utils.py b/elections/utils.py index 0c37de7..f3edaa3 100644 --- a/elections/utils.py +++ b/elections/utils.py @@ -17,6 +17,22 @@ def choices_length(choices): return m +class CastFunctions: + """Classe pour enregistrer les votes""" + + def cast_select(user, vote_form): + vote_form.full_clean() + selected, n_selected = [], [] + for v in vote_form: + if v.cleaned_data["selected"]: + selected.append(v.instance) + else: + n_selected.append(v.instance) + + user.votes.add(*selected) + user.votes.remove(*n_selected) + + def create_users(election, csv_file): """Crée les votant·e·s pour l'élection donnée, en remplissant les champs `username`, `election` et `full_name`. diff --git a/elections/views.py b/elections/views.py index cfad80a..dd28222 100644 --- a/elections/views.py +++ b/elections/views.py @@ -384,6 +384,8 @@ class VoteView(OpenElectionOnlyMixin, DetailView): q_index = questions.index(self.object) if q_index + 1 == len(questions): # On était à la dernière question + # On enregistre le vote pour l'élection + self.object.election.voters.add(self.request.user) return reverse("election.view", args=[self.object.election.pk]) # On récupère l'id de la prochaine question @@ -417,13 +419,9 @@ class VoteView(OpenElectionOnlyMixin, DetailView): self.object = self.get_object() vote_form = OptionFormSet(self.request.POST, instance=self.object) - # We record the cast votes - for v in vote_form: - v.record_vote(self.request.user) - - # On enregistre le vote pour la question et l'élection + # On enregistre le vote + self.object.cast_ballot(self.request.user, vote_form) self.object.voters.add(self.request.user) - self.object.election.voters.add(self.request.user) messages.success(self.request, _("Votre choix a bien été enregistré !"))