From dfcddbd1fa7a4801aba8b6e8eeca16b8815646f2 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lenglet Date: Thu, 22 Dec 2016 22:00:02 +0100 Subject: [PATCH] Hide election detail parts when user cannot candidate or edit. --- election/models.py | 6 ++ .../templates/election/election_detail.jinja | 61 +++++++++++++++---- election/views.py | 7 +-- 3 files changed, 57 insertions(+), 17 deletions(-) diff --git a/election/models.py b/election/models.py index 18343383..525dc7a4 100644 --- a/election/models.py +++ b/election/models.py @@ -39,6 +39,12 @@ class Election(models.Model): def has_voted(self, user): 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 diff --git a/election/templates/election/election_detail.jinja b/election/templates/election/election_detail.jinja index ebcd4cb6..43a07ce3 100644 --- a/election/templates/election/election_detail.jinja +++ b/election/templates/election/election_detail.jinja @@ -35,6 +35,17 @@ th { margin-bottom: 5px; } +.election__elector-infos { + margin: 0; + margin-bottom: 5px; + font-weight: bolder; + color: darkgreen; +} + +.election__vote { + margin-bottom: 5px; +} + .election__vote-form { width: auto; } @@ -135,10 +146,10 @@ th { color: dimgray; text-align: center; 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; } @@ -149,8 +160,12 @@ th { color: darkgreen; } -.candidate__vote-input:checked + .candidate__vote-choice:hover, -.candidate__vote-input:checked + .candidate__vote-choice:focus { +.candidate__vote-input:not(:disabled) + .candidate__vote-choice { + cursor: pointer; +} + +.candidate__vote-input:checked:not(:disabled) + .candidate__vote-choice:hover, +.candidate__vote-input:checked:not(:disabled) + .candidate__vote-choice:focus { background: palegreen; } @@ -159,8 +174,8 @@ th { color: darkred; } -.role_error .candidate__vote-input:checked + .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:hover, +.role_error .candidate__vote-input:checked:not(:disabled) + .candidate__vote-choice:focus { background: indianred; } @@ -184,6 +199,18 @@ th { .election__sumbit-button:focus { 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; +} {%- endblock %} @@ -202,7 +229,7 @@ th {

{%- endif %} -
+
{% csrf_token %} @@ -234,7 +261,7 @@ th {
{%- if election.is_active and role.max_choice == 1 %} - + @@ -258,7 +285,7 @@ th { {%- if election.is_active %} - + @@ -275,15 +302,23 @@ th {
+ {%- if not election.has_voted(request.user) %}
+ {%- endif %} + {%- if request.user.can_edit(election) %}
{% trans %}Add a new list{% endtrans %} {% trans %}Add a new role{% endtrans %}
-
{{candidate_form}} - {% csrf_token %} -

-
+ {%- endif %} + {%- if election.can_candidate(request.user) %} +
+
{{candidate_form}} + {% csrf_token %} +

+
+
+ {%- endif %} {% endblock %} \ No newline at end of file diff --git a/election/views.py b/election/views.py index 5368bb3f..b8072e86 100644 --- a/election/views.py +++ b/election/views.py @@ -137,10 +137,9 @@ class CandidatureCreateView(CanCreateMixin, FormView): data = form.clean() res = super(FormView, self).form_valid(form) data['election'] = Election.objects.get(id=self.election_id) - for grp in data['election'].candidature_groups.all(): - if data['user'].is_in_group(grp): - self.create_candidature(data) - return res + if(data['election'].can_candidate(data['user'])): + self.create_candidature(data) + return res return res def get_success_url(self, **kwargs):