Display results only when the polls close

This commit is contained in:
Jean-Baptiste Lenglet 2016-12-23 21:53:54 +01:00 committed by klmp200
parent 1c761f9db2
commit 64f5fef89f
3 changed files with 21 additions and 12 deletions

View File

@ -31,6 +31,10 @@ class Election(models.Model):
now = timezone.now() now = timezone.now()
return bool(now <= self.end_date and now >= self.start_date) 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 @property
def is_candidature_active(self): def is_candidature_active(self):
now = timezone.now() now = timezone.now()

View File

@ -235,6 +235,8 @@ th {
<p> <p>
{%- if election.is_vote_active %} {%- if election.is_vote_active %}
{% trans %}Polls close {% endtrans %} {% trans %}Polls close {% endtrans %}
{%- elif election.is_vote_finished %}
{% trans %}Polls closed {% endtrans %}
{%- else %} {%- else %}
{% trans %}Polls will open {% endtrans %} {% 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> <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> </label>
{%- set _ = count.append(count.pop() + 1) %} {%- set _ = count.append(count.pop() + 1) %}
{%- endif %} {%- 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'] %} {%- set results = election_results[role.title]['blank vote'] %}
<div class="election__results"> <div class="election__results">
<strong>{{results.vote}} {% trans %}votes{% endtrans %} ( {{results.percent}} %)</strong> <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> <span>{% trans %}Choose{% endtrans %} {{ candidature.user.nick_name or candidature.user.first_name }}</span>
</label> </label>
{%- set _ = count.append(count.pop() + 1) %} {%- set _ = count.append(count.pop() + 1) %}
{%- else %} {%- endif %}
{%- if not election.is_vote_finished %}
{%- set results = election_results[role.title][candidature.user.username] %} {%- set results = election_results[role.title][candidature.user.username] %}
<div class="election__results"> <div class="election__results">
<strong>{{results.vote}} {% trans %}votes{% endtrans %} ( {{results.percent}} %)</strong> <strong>{{results.vote}} {% trans %}votes{% endtrans %} ( {{results.percent}} %)</strong>

View File

@ -182,19 +182,21 @@ class VoteFormView(CanCreateMixin, FormView):
self.election = get_object_or_404(Election, pk=kwargs['election_id']) self.election = get_object_or_404(Election, pk=kwargs['election_id'])
return super(VoteFormView, self).dispatch(request, *arg, **kwargs) return super(VoteFormView, self).dispatch(request, *arg, **kwargs)
def vote(self, data): def vote(self, election_data):
for key in data.keys(): for role_title in election_data.keys():
if isinstance(data[key], QuerySet): # If we have a multiple choice field
if data[key].count() > 0: if isinstance(election_data[role_title], QuerySet):
vote = Vote(role=data[key].first().role) if election_data[role_title].count() > 0:
vote = Vote(role=election_data[role_title].first().role)
vote.save() vote.save()
for el in data[key]: for el in election_data[role_title]:
vote.candidature.add(el) vote.candidature.add(el)
elif data[key] is not None: # If we have a single choice
vote = Vote(role=data[key].role) elif election_data[role_title] is not None:
vote = Vote(role=election_data[role_title].role)
vote.save() vote.save()
vote.candidature.add(data[key]) vote.candidature.add(election_data[role_title])
self.election.role.get(title=key).has_voted.add(self.request.user) self.election.role.get(title=role_title).has_voted.add(self.request.user)
def get_form_kwargs(self): def get_form_kwargs(self):
kwargs = super(VoteFormView, self).get_form_kwargs() kwargs = super(VoteFormView, self).get_form_kwargs()