From c7e5d9ad6eaebcb4ce859717dd4399ceb3f430f9 Mon Sep 17 00:00:00 2001 From: Tom Hubrecht Date: Thu, 24 Dec 2020 01:41:29 +0100 Subject: [PATCH] =?UTF-8?q?Enregistre=20les=20votes=20pour=20des=20=C3=A9l?= =?UTF-8?q?ections=20et=20des=20questions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- elections/migrations/0007_election_voters.py | 21 ++++++++++++++++++++ elections/migrations/0008_question_voters.py | 21 ++++++++++++++++++++ elections/models.py | 11 +++++++++- elections/views.py | 11 +++++++++- 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 elections/migrations/0007_election_voters.py create mode 100644 elections/migrations/0008_question_voters.py diff --git a/elections/migrations/0007_election_voters.py b/elections/migrations/0007_election_voters.py new file mode 100644 index 0000000..454c60e --- /dev/null +++ b/elections/migrations/0007_election_voters.py @@ -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 + ), + ), + ] diff --git a/elections/migrations/0008_question_voters.py b/elections/migrations/0008_question_voters.py new file mode 100644 index 0000000..b61f737 --- /dev/null +++ b/elections/migrations/0008_question_voters.py @@ -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 + ), + ), + ] diff --git a/elections/models.py b/elections/models.py index 0bdaf03..27b926d 100644 --- a/elections/models.py +++ b/elections/models.py @@ -32,9 +32,13 @@ class Election(models.Model): null=True, ) + voters = models.ManyToManyField( + settings.AUTH_USER_MODEL, + related_name="cast_elections", + ) + results_public = models.BooleanField(_("résultats publics"), 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) @@ -58,6 +62,11 @@ class Question(models.Model): _("nombre maximal de votes reçus"), default=0 ) + voters = models.ManyToManyField( + settings.AUTH_USER_MODEL, + related_name="cast_questions", + ) + class Meta: ordering = ["id"] diff --git a/elections/views.py b/elections/views.py index b3a800c..10486ad 100644 --- a/elections/views.py +++ b/elections/views.py @@ -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): model = Question template_name = "elections/vote.html" @@ -405,6 +410,10 @@ class VoteView(OpenElectionOnlyMixin, DetailView): for v in vote_form: 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é !")) return HttpResponseRedirect(self.get_success_url())