From e8d54764bd32abf6e8464a37272bbae37106d713 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Fri, 23 Dec 2016 17:13:46 +0100 Subject: [PATCH] Add election results --- election/models.py | 25 +++++++++++++++++++++++++ election/views.py | 31 +++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/election/models.py b/election/models.py index 0d382db0..c6df8537 100644 --- a/election/models.py +++ b/election/models.py @@ -56,6 +56,13 @@ class Election(models.Model): return True return False + @property + def get_results(self): + results = {} + for role in self.role.all(): + results[role.title] = role.get_results + return results + # Permissions @@ -72,6 +79,24 @@ class Role(models.Model): def user_has_voted(self, user): return self.has_voted.filter(id=user.id).exists() + @property + def get_results(self): + results = {} + total_vote = self.has_voted.count() * self.max_choice + if total_vote == 0: + return None + non_blank = 0 + for candidature in self.candidature.all(): + cand_results = {} + cand_results['vote'] = self.vote.filter(candidature=candidature).count() + cand_results['percent'] = cand_results['vote'] * 100 / total_vote + non_blank += cand_results['vote'] + results[candidature.user.username] = cand_results + results['total vote'] = total_vote + results['blank vote'] = {'vote': total_vote - non_blank, + 'percent': (total_vote - non_blank) * 100 / total_vote} + return results + def __str__(self): return ("%s : %s") % (self.election.title, self.title) diff --git a/election/views.py b/election/views.py index b8072e86..4bb27cde 100644 --- a/election/views.py +++ b/election/views.py @@ -67,7 +67,21 @@ class VoteForm(forms.Form): self.fields[role.title] = VoteCheckbox(cand, role.max_choice, required=False) else: self.fields[role.title] = forms.ModelChoiceField(cand, required=False, - widget=forms.RadioSelect(), empty_label=_("Blank vote")) + widget=forms.RadioSelect(), empty_label=_("Blank vote")) + + +class RoleForm(forms.ModelForm): + """ Form for creating a role """ + class Meta: + model = Role + fields = ['title', 'election', 'description', 'max_choice'] + + def clean(self): + cleaned_data = super(RoleForm, self).clean() + title = cleaned_data.get('title') + election = cleaned_data.get('election') + if Role.objects.filter(title=title, election=election).exists(): + raise forms.ValidationError(_("This role already exists for this election"), code='invalid') # Display elections @@ -110,10 +124,11 @@ class CandidatureCreateView(CanCreateMixin, FormView): View dedicated to a cundidature creation """ form_class = CandidateForm - template_name = 'core/page_prop.jinja' + template_name = 'election/election_detail.jinja' def dispatch(self, request, *arg, **kwargs): self.election_id = kwargs['election_id'] + self.election = get_object_or_404(Election, pk=self.election_id) return super(CandidatureCreateView, self).dispatch(request, *arg, **kwargs) def get_form_kwargs(self): @@ -142,6 +157,15 @@ class CandidatureCreateView(CanCreateMixin, FormView): return res return res + def get_context_data(self, **kwargs): + """ Add additionnal data to the template """ + kwargs = super(CandidatureCreateView, self).get_context_data(**kwargs) + kwargs['candidate_form'] = self.get_form() + kwargs['object'] = self.election + kwargs['election'] = self.election + kwargs['election_form'] = VoteForm(self.election, self.request.user) + return kwargs + def get_success_url(self, **kwargs): return reverse_lazy('election:detail', kwargs={'election_id': self.election_id}) @@ -229,8 +253,7 @@ class ElectionCreateView(CanCreateMixin, CreateView): class RoleCreateView(CanCreateMixin, CreateView): model = Role - form_class = modelform_factory(Role, - fields=['title', 'election', 'title', 'description', 'max_choice']) + form_class = RoleForm template_name = 'core/page_prop.jinja' def form_valid(self, form):