Enregistre les votes pour des élections et des questions

This commit is contained in:
Tom Hubrecht 2020-12-24 01:41:29 +01:00
parent 1cafa2aedd
commit c7e5d9ad6e
4 changed files with 62 additions and 2 deletions

View file

@ -0,0 +1,21 @@
# Generated by Django 2.2.17 on 2020-12-24 00:18
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("elections", "0006_election_sent_mail"),
]
operations = [
migrations.AddField(
model_name="election",
name="voters",
field=models.ManyToManyField(
related_name="cast_elections", to=settings.AUTH_USER_MODEL
),
),
]

View file

@ -0,0 +1,21 @@
# Generated by Django 2.2.17 on 2020-12-24 00:38
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("elections", "0007_election_voters"),
]
operations = [
migrations.AddField(
model_name="question",
name="voters",
field=models.ManyToManyField(
related_name="cast_questions", to=settings.AUTH_USER_MODEL
),
),
]

View file

@ -32,9 +32,13 @@ class Election(models.Model):
null=True, null=True,
) )
voters = models.ManyToManyField(
settings.AUTH_USER_MODEL,
related_name="cast_elections",
)
results_public = models.BooleanField(_("résultats publics"), default=False) results_public = models.BooleanField(_("résultats publics"), default=False)
tallied = models.BooleanField(_("dépouillée"), default=False) tallied = models.BooleanField(_("dépouillée"), default=False)
# TODO : cache tally or recompute it each time ?
archived = models.BooleanField(_("archivée"), default=False) archived = models.BooleanField(_("archivée"), default=False)
@ -58,6 +62,11 @@ class Question(models.Model):
_("nombre maximal de votes reçus"), default=0 _("nombre maximal de votes reçus"), default=0
) )
voters = models.ManyToManyField(
settings.AUTH_USER_MODEL,
related_name="cast_questions",
)
class Meta: class Meta:
ordering = ["id"] ordering = ["id"]

View file

@ -335,7 +335,7 @@ class DelOptionView(CreatorOnlyEditMixin, BackgroundUpdateView):
# ############################################################################# # #############################################################################
# Common Views # Public Views
# ############################################################################# # #############################################################################
@ -364,6 +364,11 @@ class ElectionView(DetailView):
) )
class ElectionVotersView(DetailView):
model = Election
template_name = "elections/election_voters.html"
class VoteView(OpenElectionOnlyMixin, DetailView): class VoteView(OpenElectionOnlyMixin, DetailView):
model = Question model = Question
template_name = "elections/vote.html" template_name = "elections/vote.html"
@ -405,6 +410,10 @@ class VoteView(OpenElectionOnlyMixin, DetailView):
for v in vote_form: for v in vote_form:
v.record_vote(self.request.user) v.record_vote(self.request.user)
# On enregistre le vote pour la question et l'élection
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é !")) messages.success(self.request, _("Votre choix a bien été enregistré !"))
return HttpResponseRedirect(self.get_success_url()) return HttpResponseRedirect(self.get_success_url())