Hide election detail parts when user cannot candidate or edit.

This commit is contained in:
Jean-Baptiste Lenglet 2016-12-22 22:00:02 +01:00 committed by klmp200
parent a3a5a0446d
commit dfcddbd1fa
3 changed files with 57 additions and 17 deletions

View File

@ -39,6 +39,12 @@ class Election(models.Model):
def has_voted(self, user): def has_voted(self, user):
return hasattr(user, 'has_voted') and user.has_voted.all() == list(self.role.all()) return hasattr(user, 'has_voted') and user.has_voted.all() == list(self.role.all())
def can_candidate(self, user):
for group in self.candidature_groups.all():
if user.is_in_group(group):
return True
return False
# Permissions # Permissions

View File

@ -35,6 +35,17 @@ th {
margin-bottom: 5px; margin-bottom: 5px;
} }
.election__elector-infos {
margin: 0;
margin-bottom: 5px;
font-weight: bolder;
color: darkgreen;
}
.election__vote {
margin-bottom: 5px;
}
.election__vote-form { .election__vote-form {
width: auto; width: auto;
} }
@ -135,10 +146,10 @@ th {
color: dimgray; color: dimgray;
text-align: center; text-align: center;
font-weight: bolder; font-weight: bolder;
cursor: pointer;
} }
.candidate__vote-choice:hover, .candidate__vote-choice:focus { .candidate__vote-input:not(:disabled:checked) + .candidate__vote-choice:hover,
.candidate__vote-input:not(:disabled:checked) + .candidate__vote-choice:focus {
background: lightgrey; background: lightgrey;
} }
@ -149,8 +160,12 @@ th {
color: darkgreen; color: darkgreen;
} }
.candidate__vote-input:checked + .candidate__vote-choice:hover, .candidate__vote-input:not(:disabled) + .candidate__vote-choice {
.candidate__vote-input:checked + .candidate__vote-choice:focus { cursor: pointer;
}
.candidate__vote-input:checked:not(:disabled) + .candidate__vote-choice:hover,
.candidate__vote-input:checked:not(:disabled) + .candidate__vote-choice:focus {
background: palegreen; background: palegreen;
} }
@ -159,8 +174,8 @@ th {
color: darkred; color: darkred;
} }
.role_error .candidate__vote-input:checked + .candidate__vote-choice:hover, .role_error .candidate__vote-input:checked:not(:disabled) + .candidate__vote-choice:hover,
.role_error .candidate__vote-input:checked + .candidate__vote-choice:focus { .role_error .candidate__vote-input:checked:not(:disabled) + .candidate__vote-choice:focus {
background: indianred; background: indianred;
} }
@ -184,6 +199,18 @@ th {
.election__sumbit-button:focus { .election__sumbit-button:focus {
background-color: lightskyblue; background-color: lightskyblue;
} }
.election__add-elements a {
display: inline-block;
border: solid 1px darkgrey;
height: 20px;
line-height: 20px;
padding: 10px;
}
.election__add-candidature {
margin-top: 5px;
}
</style> </style>
{%- endblock %} {%- endblock %}
@ -202,7 +229,7 @@ th {
</p> </p>
{%- endif %} {%- endif %}
</section> </section>
<section> <section class="election__vote">
<form action="/election/1/vote" method="post" class="election__vote-form" name="vote-form" id="vote-form"> <form action="/election/1/vote" method="post" class="election__vote-form" name="vote-form" id="vote-form">
{% csrf_token %} {% csrf_token %}
<table> <table>
@ -234,7 +261,7 @@ th {
<tr class="role_candidates"> <tr class="role_candidates">
<td class="list-per-role"> <td class="list-per-role">
{%- if election.is_active and role.max_choice == 1 %} {%- if election.is_active and 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' }}> <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 '' }}>
<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>
@ -258,7 +285,7 @@ th {
</figcaption> </figcaption>
</figure> </figure>
{%- if election.is_active %} {%- if election.is_active %}
<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 '' }} 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(request.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>
@ -275,15 +302,23 @@ th {
</table> </table>
</form> </form>
</section> </section>
{%- if not election.has_voted(request.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 %}
{%- if request.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>
<form action="{{url('election:candidate', election_id=election.id)}}" method="post">{{candidate_form}} {%- endif %}
{% csrf_token %} {%- if election.can_candidate(request.user) %}
<p><input type="submit" value="{% trans %}Candidate{% endtrans %}" /></p> <section class="election__add-candidature">
</form> <form action="{{url('election:candidate', election_id=election.id)}}" method="post">{{candidate_form}}
{% csrf_token %}
<p><input type="submit" value="{% trans %}Candidate{% endtrans %}" /></p>
</form>
</section>
{%- endif %}
{% endblock %} {% endblock %}

View File

@ -137,10 +137,9 @@ class CandidatureCreateView(CanCreateMixin, FormView):
data = form.clean() data = form.clean()
res = super(FormView, self).form_valid(form) res = super(FormView, self).form_valid(form)
data['election'] = Election.objects.get(id=self.election_id) data['election'] = Election.objects.get(id=self.election_id)
for grp in data['election'].candidature_groups.all(): if(data['election'].can_candidate(data['user'])):
if data['user'].is_in_group(grp): self.create_candidature(data)
self.create_candidature(data) return res
return res
return res return res
def get_success_url(self, **kwargs): def get_success_url(self, **kwargs):