Add election results

This commit is contained in:
Antoine Bartuccio 2016-12-23 17:13:46 +01:00
parent baf39d8f3b
commit e8d54764bd
2 changed files with 52 additions and 4 deletions

View File

@ -56,6 +56,13 @@ class Election(models.Model):
return True return True
return False return False
@property
def get_results(self):
results = {}
for role in self.role.all():
results[role.title] = role.get_results
return results
# Permissions # Permissions
@ -72,6 +79,24 @@ class Role(models.Model):
def user_has_voted(self, user): def user_has_voted(self, user):
return self.has_voted.filter(id=user.id).exists() 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): def __str__(self):
return ("%s : %s") % (self.election.title, self.title) return ("%s : %s") % (self.election.title, self.title)

View File

@ -70,6 +70,20 @@ class VoteForm(forms.Form):
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 # Display elections
@ -110,10 +124,11 @@ class CandidatureCreateView(CanCreateMixin, FormView):
View dedicated to a cundidature creation View dedicated to a cundidature creation
""" """
form_class = CandidateForm form_class = CandidateForm
template_name = 'core/page_prop.jinja' template_name = 'election/election_detail.jinja'
def dispatch(self, request, *arg, **kwargs): def dispatch(self, request, *arg, **kwargs):
self.election_id = kwargs['election_id'] 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) return super(CandidatureCreateView, self).dispatch(request, *arg, **kwargs)
def get_form_kwargs(self): def get_form_kwargs(self):
@ -142,6 +157,15 @@ class CandidatureCreateView(CanCreateMixin, FormView):
return res return res
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): def get_success_url(self, **kwargs):
return reverse_lazy('election:detail', kwargs={'election_id': self.election_id}) return reverse_lazy('election:detail', kwargs={'election_id': self.election_id})
@ -229,8 +253,7 @@ class ElectionCreateView(CanCreateMixin, CreateView):
class RoleCreateView(CanCreateMixin, CreateView): class RoleCreateView(CanCreateMixin, CreateView):
model = Role model = Role
form_class = modelform_factory(Role, form_class = RoleForm
fields=['title', 'election', 'title', 'description', 'max_choice'])
template_name = 'core/page_prop.jinja' template_name = 'core/page_prop.jinja'
def form_valid(self, form): def form_valid(self, form):