From 9d9c86ea0f529a25f5ca536d4df8e7506ba950e0 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Fri, 23 Dec 2016 23:49:00 +0100 Subject: [PATCH] Refactored has_voted --- .../migrations/0006_auto_20161223_2315.py | 25 +++++++++++++++++++ election/models.py | 22 ++++++---------- .../{vote_form.jinja => candidate_form.jinja} | 2 +- election/views.py | 13 +++++----- 4 files changed, 41 insertions(+), 21 deletions(-) create mode 100644 election/migrations/0006_auto_20161223_2315.py rename election/templates/election/{vote_form.jinja => candidate_form.jinja} (88%) diff --git a/election/migrations/0006_auto_20161223_2315.py b/election/migrations/0006_auto_20161223_2315.py new file mode 100644 index 00000000..a6190ce5 --- /dev/null +++ b/election/migrations/0006_auto_20161223_2315.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +from django.conf import settings + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('election', '0005_auto_20161223_2240'), + ] + + operations = [ + migrations.RemoveField( + model_name='role', + name='has_voted', + ), + migrations.AddField( + model_name='election', + name='voters', + field=models.ManyToManyField(to=settings.AUTH_USER_MODEL, verbose_name='has voted', related_name='has_voted'), + ), + ] diff --git a/election/models.py b/election/models.py index 9a0ff0f4..28d18e0d 100644 --- a/election/models.py +++ b/election/models.py @@ -22,6 +22,7 @@ class Election(models.Model): view_groups = models.ManyToManyField(Group, related_name="viewable_elections", verbose_name=_("view groups"), blank=True) vote_groups = models.ManyToManyField(Group, related_name="votable_elections", verbose_name=_("vote groups"), blank=True) candidature_groups = models.ManyToManyField(Group, related_name="candidate_elections", verbose_name=_("candidature groups"), blank=True) + voters = models.ManyToManyField(User, verbose_name=('voters'), related_name='voted_elections') def __str__(self): return self.title @@ -40,12 +41,6 @@ class Election(models.Model): now = timezone.now() return bool(now <= self.end_candidature and now >= self.start_candidature) - def has_voted(self, user): - for role in self.roles.all(): - if role.user_has_voted(user): - return True - return False - def can_candidate(self, user): for group in self.candidature_groups.all(): if user.is_in_group(group): @@ -60,11 +55,15 @@ class Election(models.Model): return True return False + def has_voted(self, user): + return self.voters.filter(id=user.id).exists() + @property def results(self): results = {} + total_vote = self.voters.count() for role in self.roles.all(): - results[role.title] = role.results + results[role.title] = role.results(total_vote) return results # Permissions @@ -77,16 +76,11 @@ class Role(models.Model): election = models.ForeignKey(Election, related_name='roles', verbose_name=_("election")) title = models.CharField(_('title'), max_length=255) description = models.TextField(_('description'), null=True, blank=True) - has_voted = models.ManyToManyField(User, verbose_name=('has voted'), related_name='has_voted') max_choice = models.IntegerField(_('max choice'), default=1) - def user_has_voted(self, user): - return self.has_voted.filter(id=user.id).exists() - - @property - def results(self): + def results(self, total_vote): results = {} - total_vote = self.has_voted.count() * self.max_choice + total_vote *= self.max_choice if total_vote == 0: return None non_blank = 0 diff --git a/election/templates/election/vote_form.jinja b/election/templates/election/candidate_form.jinja similarity index 88% rename from election/templates/election/vote_form.jinja rename to election/templates/election/candidate_form.jinja index cf992b97..64a96d07 100644 --- a/election/templates/election/vote_form.jinja +++ b/election/templates/election/candidate_form.jinja @@ -1,7 +1,7 @@ {% extends "core/base.jinja" %} {% block title %} -{% trans %}Vote{% endtrans %} +{% trans %}Candidate{% endtrans %} {% endblock %} {% block content %} diff --git a/election/views.py b/election/views.py index 58220c89..a0c1892f 100644 --- a/election/views.py +++ b/election/views.py @@ -60,8 +60,8 @@ class CandidateForm(forms.Form): class VoteForm(forms.Form): def __init__(self, election, user, *args, **kwargs): super(VoteForm, self).__init__(*args, **kwargs) - for role in election.roles.all(): - if not role.user_has_voted(user): + if not election.has_voted(user): + for role in election.roles.all(): cand = role.candidatures if role.max_choice > 1: self.fields[role.title] = VoteCheckbox(cand, role.max_choice, required=False) @@ -112,9 +112,10 @@ class ElectionDetailView(CanViewMixin, DetailView): def get_context_data(self, **kwargs): """ Add additionnal data to the template """ kwargs = super(ElectionDetailView, self).get_context_data(**kwargs) - kwargs['candidate_form'] = CandidateForm(self.get_object().id) - kwargs['election_form'] = VoteForm(self.get_object(), self.request.user) - kwargs['election_results'] = self.get_object().results + kwargs['candidate_form'] = CandidateForm(self.object.id) + kwargs['election_form'] = VoteForm(self.object, self.request.user) + kwargs['election_results'] = self.object.results + print(self.object.results) return kwargs @@ -196,7 +197,7 @@ class VoteFormView(CanCreateMixin, FormView): vote = Vote(role=election_data[role_title].role) vote.save() vote.candidature.add(election_data[role_title]) - self.election.roles.get(title=role_title).has_voted.add(self.request.user) + self.election.voters.add(self.request.user) def get_form_kwargs(self): kwargs = super(VoteFormView, self).get_form_kwargs()