Refactored has_voted

This commit is contained in:
Antoine Bartuccio 2016-12-23 23:49:00 +01:00
parent 37decde04d
commit 9d9c86ea0f
4 changed files with 41 additions and 21 deletions

View File

@ -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'),
),
]

View File

@ -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

View File

@ -1,7 +1,7 @@
{% extends "core/base.jinja" %}
{% block title %}
{% trans %}Vote{% endtrans %}
{% trans %}Candidate{% endtrans %}
{% endblock %}
{% block content %}

View File

@ -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)
if not election.has_voted(user):
for role in election.roles.all():
if not role.user_has_voted(user):
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()