Tweaked Election view and added request.user as user

This commit is contained in:
Jean-Baptiste Lenglet 2016-12-23 01:04:26 +01:00 committed by klmp200
parent a9d154506f
commit fa9e4e5dc6
3 changed files with 26 additions and 13 deletions

View File

@ -27,7 +27,7 @@ class Election(models.Model):
return self.title return self.title
@property @property
def is_active(self): def is_vote_active(self):
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)
@ -49,6 +49,8 @@ class Election(models.Model):
return False return False
def can_vote(self, user): def can_vote(self, user):
if not self.is_vote_active or self.has_voted(user):
return False
for group in self.vote_groups.all(): for group in self.vote_groups.all():
if user.is_in_group(group): if user.is_in_group(group):
return True return True

View File

@ -224,13 +224,23 @@ th {
<p class="election__description">{{ election.description }}</p> <p class="election__description">{{ election.description }}</p>
<hr> <hr>
<section class="election__details"> <section class="election__details">
<p class=""> <p>
{%- if election.is_vote_active %}
{% trans %}Polls close {% endtrans %} {% trans %}Polls close {% 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>
{% trans %}and will close {% endtrans %}
{%- endif %}
<time datetime="{{ election.end_date }}">{{ election.end_date|date("l d F Y") }}</time> at <time>{{ election.end_date|time("G:i") }}</time> <time datetime="{{ election.end_date }}">{{ election.end_date|date("l d F Y") }}</time> at <time>{{ election.end_date|time("G:i") }}</time>
</p> </p>
{%- if election.has_voted(request.user) %} {%- if election.has_voted(user) %}
<p class="election__elector-infos"> <p class="election__elector-infos">
{%- if election.is_vote_active %}
<span>{% trans %}You already have submitted your vote.{% endtrans %}</span> <span>{% trans %}You already have submitted your vote.{% endtrans %}</span>
{%- else %}
<span>{% trans %}You have voted in this election.{% endtrans %}</span>
{%- endif %}
</p> </p>
{%- endif %} {%- endif %}
</section> </section>
@ -241,7 +251,7 @@ th {
{%- set election_lists = election.election_list.all() -%} {%- set election_lists = election.election_list.all() -%}
<caption></caption> <caption></caption>
<thead> <thead>
{%- if not election.has_voted(request.user) %} {%- if not election.has_voted(user) %}
<th>{% trans %}Blank vote{% endtrans %}</th> <th>{% trans %}Blank vote{% endtrans %}</th>
{%- endif %} {%- endif %}
{%- for election_list in election_lists %} {%- for election_list in election_lists %}
@ -255,7 +265,7 @@ th {
<tr class="role__title"> <tr class="role__title">
<td colspan="{{election_lists.count() + 1}}"> <td colspan="{{election_lists.count() + 1}}">
<span>{{role.title}}</span> <span>{{role.title}}</span>
{%- if role.max_choice > 1 and not election.has_voted(request.user) %} {%- if role.max_choice > 1 and not election.has_voted(user) %}
<strong class="role__multiple-choices-label">{% trans %}You may choose up to{% endtrans %} {{ role.max_choice }} {% trans %}people.{% endtrans %}</strong> <strong class="role__multiple-choices-label">{% trans %}You may choose up to{% endtrans %} {{ role.max_choice }} {% trans %}people.{% endtrans %}</strong>
{%- endif %} {%- endif %}
{%- if election_form.errors[role.title] is defined %} {%- if election_form.errors[role.title] is defined %}
@ -266,10 +276,10 @@ th {
</td> </td>
</tr> </tr>
<tr class="role_candidates"> <tr class="role_candidates">
{%- if not election.has_voted(request.user) %} {%- if election.can_vote(user) %}
<td class="list-per-role"> <td class="list-per-role">
{%- if election.is_active and role.max_choice == 1 %} {%- if role.max_choice == 1 %}
<input id="id_{{ role.title }}_{{ count[0] }}" class="candidate__vote-input" type="radio" name="{{ role.title }}" value {{ '' if role_data in election_form else 'checked' }} {{ 'disabled' if election.has_voted(request.user) else '' }}> <input id="id_{{ role.title }}_{{ count[0] }}" class="candidate__vote-input" type="radio" name="{{ role.title }}" value {{ '' if role_data in election_form else 'checked' }} {{ 'disabled' if election.has_voted(user) else '' }}>
<label for="id_{{ role.title }}_{{ count[0] }}" class="candidate__vote-choice"> <label for="id_{{ role.title }}_{{ count[0] }}" class="candidate__vote-choice">
<span>{% trans %}Choose blank vote{% endtrans %}</span> <span>{% trans %}Choose blank vote{% endtrans %}</span>
</label> </label>
@ -293,8 +303,8 @@ th {
<q class="candidate__program">{{ candidature.program or '' }}</q> <q class="candidate__program">{{ candidature.program or '' }}</q>
</figcaption> </figcaption>
</figure> </figure>
{%- if election.is_active and not election.has_voted(request.user) %} {%- if election.can_vote(user) %}
<input id="id_{{ role.title }}_{{ count[0] }}" type="{{ 'checkbox' if role.max_choice > 1 else 'radio' }}" {{ 'checked' if candidature.id|string in role_data else '' }} {{ 'disabled' if election.has_voted(request.user) else '' }} name="{{ role.title }}" value="{{ candidature.id }}" class="candidate__vote-input"> <input id="id_{{ role.title }}_{{ count[0] }}" type="{{ 'checkbox' if role.max_choice > 1 else 'radio' }}" {{ 'checked' if candidature.id|string in role_data else '' }} {{ 'disabled' if election.has_voted(user) else '' }} name="{{ role.title }}" value="{{ candidature.id }}" class="candidate__vote-input">
<label for="id_{{ role.title }}_{{ count[0] }}" class="candidate__vote-choice"> <label for="id_{{ role.title }}_{{ count[0] }}" class="candidate__vote-choice">
<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>
@ -311,18 +321,18 @@ th {
</table> </table>
</form> </form>
</section> </section>
{%- if not election.has_voted(request.user) %} {%- if not election.has_voted(user) %}
<section class="election__sumbit-section"> <section class="election__sumbit-section">
<button class="election__sumbit-button" form="vote-form">{% trans %}Submit the vote !{% endtrans %}</button> <button class="election__sumbit-button" form="vote-form">{% trans %}Submit the vote !{% endtrans %}</button>
</section> </section>
{%- endif %} {%- endif %}
{%- if request.user.can_edit(election) %} {%- if user.can_edit(election) %}
<section class="election__add-elements"> <section class="election__add-elements">
<a href="{{url('election:create_list')}}">{% trans %}Add a new list{% endtrans %}</a> <a href="{{url('election:create_list')}}">{% trans %}Add a new list{% endtrans %}</a>
<a href="{{url('election:create_role')}}">{% trans %}Add a new role{% endtrans %}</a> <a href="{{url('election:create_role')}}">{% trans %}Add a new role{% endtrans %}</a>
</section> </section>
{%- endif %} {%- endif %}
{%- if election.can_candidate(request.user) %} {%- if election.can_candidate(user) or user.can_edit(election) %}
<section class="election__add-candidature"> <section class="election__add-candidature">
<form action="{{url('election:candidate', election_id=election.id)}}" method="post">{{candidate_form}} <form action="{{url('election:candidate', election_id=election.id)}}" method="post">{{candidate_form}}
{% csrf_token %} {% csrf_token %}

View File

@ -100,6 +100,7 @@ class ElectionDetailView(CanViewMixin, DetailView):
kwargs = super(ElectionDetailView, self).get_context_data(**kwargs) kwargs = super(ElectionDetailView, self).get_context_data(**kwargs)
kwargs['candidate_form'] = CandidateForm(self.get_object().id) kwargs['candidate_form'] = CandidateForm(self.get_object().id)
kwargs['election_form'] = VoteForm(self.get_object(), self.request.user) kwargs['election_form'] = VoteForm(self.get_object(), self.request.user)
kwargs['user'] = self.request.user
return kwargs return kwargs