On déplace l'enregistrement des votes
This commit is contained in:
parent
f2b0630791
commit
7c14c37d8a
5 changed files with 30 additions and 15 deletions
|
@ -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 = []
|
||||
|
|
|
@ -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"]
|
||||
|
||||
|
|
|
@ -29,3 +29,7 @@ VOTE_RULES = {
|
|||
"Vous pouvez également ne sélectionner aucune option."
|
||||
)
|
||||
}
|
||||
|
||||
CAST_FUNCTIONS = {
|
||||
"assentiment": "cast_select",
|
||||
}
|
||||
|
|
|
@ -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`.
|
||||
|
|
|
@ -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é !"))
|
||||
|
||||
|
|
Loading…
Reference in a new issue