Commit 64f5fef8 authored by Jean-Baptiste Lenglet's avatar Jean-Baptiste Lenglet Committed by Sli

Display results only when the polls close

parent 1c761f9d
......@@ -31,6 +31,10 @@ class Election(models.Model):
now = timezone.now()
return bool(now <= self.end_date and now >= self.start_date)
@property
def is_vote_finished(self):
return bool(timezone.now() > self.end_date)
@property
def is_candidature_active(self):
now = timezone.now()
......
......@@ -235,6 +235,8 @@ th {
<p>
{%- if election.is_vote_active %}
{% trans %}Polls close {% endtrans %}
{%- elif election.is_vote_finished %}
{% trans %}Polls closed {% endtrans %}
{%- else %}
{% trans %}Polls will open {% endtrans %}
<time datetime="{{ election.start_date }}">{{ election.start_date|date("l d F Y") }}</time> at <time>{{ election.start_date|time("G:i") }}</time>
......@@ -290,7 +292,7 @@ th {
</label>
{%- set _ = count.append(count.pop() + 1) %}
{%- endif %}
{%- if election.has_voted(user) or not election.is_vote_active %}
{%- if not election.is_vote_finished %}
{%- set results = election_results[role.title]['blank vote'] %}
<div class="election__results">
<strong>{{results.vote}} {% trans %}votes{% endtrans %} ( {{results.percent}} %)</strong>
......@@ -319,7 +321,8 @@ th {
<span>{% trans %}Choose{% endtrans %} {{ candidature.user.nick_name or candidature.user.first_name }}</span>
</label>
{%- set _ = count.append(count.pop() + 1) %}
{%- else %}
{%- endif %}
{%- if not election.is_vote_finished %}
{%- set results = election_results[role.title][candidature.user.username] %}
<div class="election__results">
<strong>{{results.vote}} {% trans %}votes{% endtrans %} ( {{results.percent}} %)</strong>
......
......@@ -182,19 +182,21 @@ class VoteFormView(CanCreateMixin, FormView):
self.election = get_object_or_404(Election, pk=kwargs['election_id'])
return super(VoteFormView, self).dispatch(request, *arg, **kwargs)
def vote(self, data):
for key in data.keys():
if isinstance(data[key], QuerySet):
if data[key].count() > 0:
vote = Vote(role=data[key].first().role)
def vote(self, election_data):
for role_title in election_data.keys():
# If we have a multiple choice field
if isinstance(election_data[role_title], QuerySet):
if election_data[role_title].count() > 0:
vote = Vote(role=election_data[role_title].first().role)
vote.save()
for el in data[key]:
for el in election_data[role_title]:
vote.candidature.add(el)
elif data[key] is not None:
vote = Vote(role=data[key].role)
# If we have a single choice
elif election_data[role_title] is not None:
vote = Vote(role=election_data[role_title].role)
vote.save()
vote.candidature.add(data[key])
self.election.role.get(title=key).has_voted.add(self.request.user)
vote.candidature.add(election_data[role_title])
self.election.role.get(title=role_title).has_voted.add(self.request.user)
def get_form_kwargs(self):
kwargs = super(VoteFormView, self).get_form_kwargs()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment