From 1c17e3ccc64f02e4a5a21dd9a826597d76668eb1 Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Fri, 29 Sep 2023 11:49:23 +0200
Subject: [PATCH] reform nomination round view for voting fellows

---
 .../_hx_nomination_decision_form.html         | 12 ++--
 .../colleges/_hx_nomination_vote.html         | 58 +++++-----------
 .../colleges/_hx_nomination_voter_table.html  | 66 +++++++++++--------
 .../_hx_nomination_voting_rounds_tab.html     | 58 +++++++++-------
 .../colleges/_hx_voting_round_details.html    | 64 ++++++++++++++----
 scipost_django/colleges/views.py              |  9 ++-
 6 files changed, 153 insertions(+), 114 deletions(-)

diff --git a/scipost_django/colleges/templates/colleges/_hx_nomination_decision_form.html b/scipost_django/colleges/templates/colleges/_hx_nomination_decision_form.html
index 619319851..dc9e78564 100644
--- a/scipost_django/colleges/templates/colleges/_hx_nomination_decision_form.html
+++ b/scipost_django/colleges/templates/colleges/_hx_nomination_decision_form.html
@@ -16,12 +16,12 @@
 
       </div>
 
-      {% comment %} {% if voting_round.decision.comments %} {% endcomment %}
-      <div class="col">
-        <h4>Decision comments</h4>
-        <p>{{ voting_round.decision.comments }}</p>
-      </div>
-      {% comment %} {% endif %} {% endcomment %}
+      {% if voting_round.decision.comments %}
+        <div class="col">
+          <h4>Decision comments</h4>
+          <p>{{ voting_round.decision.comments }}</p>
+        </div>
+      {% endif %}
 
     </div>
 
diff --git a/scipost_django/colleges/templates/colleges/_hx_nomination_vote.html b/scipost_django/colleges/templates/colleges/_hx_nomination_vote.html
index 26355ad54..9cb3d1f68 100644
--- a/scipost_django/colleges/templates/colleges/_hx_nomination_vote.html
+++ b/scipost_django/colleges/templates/colleges/_hx_nomination_vote.html
@@ -2,46 +2,24 @@
   <div class="col-12 col-md">
     <h3>Cast your vote:</h3>
 
-    {% if vote_object %}
-      <p>You have previously voted: {{ vote_object.get_vote_display }}.</p>
-    {% else %}
-      <p>You have not yet voted on this Nomination.</p>
-    {% endif %}
+    {% for vote_option, color in vote_options %}
+
+      <form hx-post="{% url 'colleges:_hx_nomination_vote' voting_round_id=voting_round.id %}"
+            hx-target="#nomination-{{ voting_round.nomination.id }}-vote">
+        {% csrf_token %}
+        <input type="hidden" name="vote" value="{{ vote_option }}" />
+        <button class="btn d-flex justify-content-between align-items-center w-100 mb-2 bg-{{ color }} text-white">
+          <div>{{ vote_option|title }}</div>
+
+          {% if vote_object.vote == vote_option %}
+            {% include 'bi/check-square-fill.html' %}
+          {% endif %}
+
+        </button>
+      </form>
+
+    {% endfor %}
+
 
-    <ul class="list-inline m-2">
-      <li class="list-inline-item">
-        <form hx-post="{% url 'colleges:_hx_nomination_vote' voting_round_id=voting_round.id %}"
-              hx-target="#nomination-{{ voting_round.nomination.id }}-vote">
-          {% csrf_token %}
-          <input type="hidden" name="vote" value="agree" />
-          <button class="btn btn-sm btn-success text-white">Agree</button>
-        </form>
-      </li>
-      <li class="list-inline-item">
-        <form hx-post="{% url 'colleges:_hx_nomination_vote' voting_round_id=voting_round.id %}"
-              hx-target="#nomination-{{ voting_round.nomination.id }}-vote">
-          {% csrf_token %}
-          <input type="hidden" name="vote" value="abstain" />
-          <button class="btn btn-sm btn-warning text-white">Abstain</button>
-        </form>
-      </li>
-      <li class="list-inline-item">
-        <form hx-post="{% url 'colleges:_hx_nomination_vote' voting_round_id=voting_round.id %}"
-              hx-target="#nomination-{{ voting_round.nomination.id }}-vote">
-          {% csrf_token %}
-          <input type="hidden" name="vote" value="disagree" />
-          <button class="btn btn-sm btn-danger text-white">Disagree</button>
-        </form>
-      </li>
-      <li class="list-inline-item">
-        <form hx-post="{% url 'colleges:_hx_nomination_vote' voting_round_id=voting_round.id %}"
-              hx-target="#nomination-{{ voting_round.nomination.id }}-vote">
-          {% csrf_token %}
-          <input type="hidden" name="vote" value="veto" />
-          <button class="btn btn-sm btn-dark text-white">Veto</button>
-        </form>
-      </li>
-    </ul>
   </div>
-  <div class="col-12 col-md">{% include "colleges/_voting_results_box.html" with voting_round=voting_round %}</div>
 </div>
diff --git a/scipost_django/colleges/templates/colleges/_hx_nomination_voter_table.html b/scipost_django/colleges/templates/colleges/_hx_nomination_voter_table.html
index b56ba572e..f14bba5b3 100644
--- a/scipost_django/colleges/templates/colleges/_hx_nomination_voter_table.html
+++ b/scipost_django/colleges/templates/colleges/_hx_nomination_voter_table.html
@@ -5,12 +5,16 @@
         <th>Fellow</th>
         <th>Specialties</th>
 
-        {% if not round.is_unscheduled %}
-          <th>Vote</th>
-          <th>Voted on</th>
+        {% if "edadmin" in user_roles %}
+
+          {% if not round.is_unscheduled %}
+            <th>Vote</th>
+            <th>Voted on</th>
+          {% endif %}
+
+          <th>Actions</th>
         {% endif %}
 
-        <th>Actions</th>
       </tr>
     </thead>
 
@@ -33,36 +37,40 @@
 
           </td>
 
-          {% if not round.is_unscheduled %}
-
-            {% if voter.vote.vote == "agree" %}
-              <td class="text-success">{{ voter.vote.get_vote_display }}</td>
-            {% elif voter.vote.vote == "abstain" %}
-              <td class="text-warning">{{ voter.vote.get_vote_display }}</td>
-            {% elif voter.vote.vote == "disagree" %}
-              <td class="text-danger">{{ voter.vote.get_vote_display }}</td>
-            {% elif voter.vote.vote == "veto" %}
-              <td>
-                <span class="badge bg-dark">{{ voter.vote.get_vote_display }}</span>
-              </td>
-            {% else %}
-              <td class="text-muted">No vote</td>
+          {% if "edadmin" in user_roles %}
+
+            {% if not round.is_unscheduled %}
+
+              {% if voter.vote.vote == "agree" %}
+                <td class="text-success">{{ voter.vote.get_vote_display }}</td>
+              {% elif voter.vote.vote == "abstain" %}
+                <td class="text-warning">{{ voter.vote.get_vote_display }}</td>
+              {% elif voter.vote.vote == "disagree" %}
+                <td class="text-danger">{{ voter.vote.get_vote_display }}</td>
+              {% elif voter.vote.vote == "veto" %}
+                <td>
+                  <span class="badge bg-dark">{{ voter.vote.get_vote_display }}</span>
+                </td>
+              {% else %}
+                <td class="text-muted">No vote</td>
+              {% endif %}
+
+              <td>{{ voter.vote.on }}</td>
             {% endif %}
 
-            <td>{{ voter.vote.on }}</td>
-          {% endif %}
+            <td>
 
-          <td>
+              {% if not round.is_closed %}
+                <button class="btn btn-sm btn-danger px-1 py-0 ms-auto"
+                        hx-get="{% url "colleges:_hx_nomination_round_eligible_voter_action" round_id=round.id fellowship_id=voter.id action='remove' %}"
+                        hx-target="#nomination-{{ round.nomination.id }}-round-{{ round.id }}-voters">
+                  {% include "bi/trash-fill.html" %}
+                </button>
+              {% endif %}
 
-            {% if not round.is_closed %}
-              <button class="btn btn-sm btn-danger px-1 py-0 ms-auto"
-                      hx-get="{% url "colleges:_hx_nomination_round_eligible_voter_action" round_id=round.id fellowship_id=voter.id action='remove' %}"
-                      hx-target="#nomination-{{ round.nomination.id }}-round-{{ round.id }}-voters">
-                {% include "bi/trash-fill.html" %}
-              </button>
-            {% endif %}
+            </td>
+          {% endif %}
 
-          </td>
         </tr>
       {% endfor %}
 
diff --git a/scipost_django/colleges/templates/colleges/_hx_nomination_voting_rounds_tab.html b/scipost_django/colleges/templates/colleges/_hx_nomination_voting_rounds_tab.html
index 6a88f8e41..4e5f8503f 100644
--- a/scipost_django/colleges/templates/colleges/_hx_nomination_voting_rounds_tab.html
+++ b/scipost_django/colleges/templates/colleges/_hx_nomination_voting_rounds_tab.html
@@ -1,38 +1,47 @@
 <div id="nomination-{{ nomination_id }}-round-tab-holder">
 
   <nav class="nav nav-pills m-2 overflow-scroll">
+
     {% if "edadmin" in user_roles %}
-      <div type="button" class="me-2 px-2 nav-link {% if new_round %}bg-success{% else %}border border-success{% endif %}" 
-      hx-get="{% url 'colleges:_hx_nomination_voting_rounds_create' nomination_id=nomination_id %}"
-      hx-target="#nomination-{{ nomination_id }}-round-tab-holder"
-      hx-swap="outerHTML">
-      <span class="fs-1 align-items-center text-{% if new_round %}white{% else %}success{% endif %}">+</span>
-    </div>
+      <div type="button" class="me-2 px-2 nav-link 
+        {% if new_round %}bg-success{% else %}border border-success{% endif %}
+         " hx-get="{% url 'colleges:_hx_nomination_voting_rounds_create' nomination_id=nomination_id %}" hx-target="#nomination-{{ nomination_id }}-round-tab-holder" hx-swap="outerHTML">
+        <span class="fs-1 align-items-center text- 
+          {% if new_round %}white{% else %}success{% endif %}
+         ">+</span>
+      </div>
     {% endif %}
 
     {% for voting_round in voting_rounds %}
-      <div type="button" class="me-2 nav-link {% if selected_round and selected_round.id == voting_round.id %}active{% endif %} {% if voting_round.id in inaccessible_round_ids %}disabled opacity-50{% endif %}" 
-          hx-get="{% url 'colleges:_hx_nomination_voting_rounds_tab' nomination_id=nomination_id round_id=voting_round.id %}"
-          hx-target="#nomination-{{ nomination_id }}-round-tab-holder"
-          hx-swap="outerHTML">
-          <span class="d-block text-nowrap">
-            {% if voting_round.voting_opens and voting_round.voting_deadline %}
-              <small>{{ voting_round.voting_opens|date:"d M Y" }} - {{ voting_round.voting_deadline|date:"d M Y" }}</small>
-              {% else %}
-              <span class="badge bg-warning">Unscheduled</span>
+      <div type="button" class="me-2 nav-link 
+        {% if selected_round and selected_round.id == voting_round.id %}active{% endif %}
+  
+        {% if voting_round.id in inaccessible_round_ids %}disabled opacity-50{% endif %}
+         " hx-get="{% url 'colleges:_hx_nomination_voting_rounds_tab' nomination_id=nomination_id round_id=voting_round.id %}" hx-target="#nomination-{{ nomination_id }}-round-tab-holder" hx-swap="outerHTML">
+        <span class="d-block text-nowrap">
+
+          {% if voting_round.voting_opens and voting_round.voting_deadline %}
+            <small>{{ voting_round.voting_opens|date:"d M Y" }} - {{ voting_round.voting_deadline|date:"d M Y" }}</small>
+          {% else %}
+            <span class="badge bg-warning">Unscheduled</span>
+          {% endif %}
+
+          <span class="d-flex justify-content-between align-items-center">
+            <span>Round #{{ forloop.revcounter }}</span>
+
+            {% if voting_round.is_scheduled %}
+              <span class="badge bg-primary">Scheduled</span>
+            {% elif voting_round.is_open %}
+              <span class="badge bg-success">Open</span>
             {% endif %}
-            <span class="d-flex justify-content-between align-items-center">
-              <span>Round #{{ forloop.revcounter }}</span>  
-              {% if voting_round.is_scheduled %}
-                <span class="badge bg-primary">Scheduled</span>
-              {% elif voting_round.is_open %}
-                <span class="badge bg-success">Open</span>
-              {% endif %}
-            </span>
+
           </span>
+        </span>
       </div>
     {% endfor %}
 
+
+
     <div id="indicator-nomination-{{ nomination_id }}-details-contents"
          class="htmx-indicator p-2 ms-auto">
       <button class="btn btn-warning" type="button" disabled>
@@ -47,7 +56,8 @@
   </nav>
 
   {% if selected_round %}
-    <div id="nomination-{{ nomination_id }}-round-{{ selected_round.id }}-tab-content-holder" class="mt-3 px-3">
+    <div id="nomination-{{ nomination_id }}-round-{{ selected_round.id }}-tab-content-holder"
+         class="p-3">
       {% include "colleges/_hx_voting_round_details.html" with voting_round=selected_round round_start_form=round_start_form new_voter_form=new_voter_form %}
     </div>
   {% endif %}
diff --git a/scipost_django/colleges/templates/colleges/_hx_voting_round_details.html b/scipost_django/colleges/templates/colleges/_hx_voting_round_details.html
index 53e52c987..1c803bd2e 100644
--- a/scipost_django/colleges/templates/colleges/_hx_voting_round_details.html
+++ b/scipost_django/colleges/templates/colleges/_hx_voting_round_details.html
@@ -1,7 +1,7 @@
 {% load crispy_forms_tags %}
 
-<div class="row">
-  <div class="col">
+<div class="row mb-0">
+  <div class="col mb-3">
     <h3>Eligible voters</h3>
     <div id="nomination-{{ voting_round.nomination.id }}-round-{{ voting_round.id }}-voters"
          hx-get="{% url 'colleges:_hx_nomination_voter_table' round_id=voting_round.id %}"
@@ -33,21 +33,57 @@
         ALL seniors
       </button>
     </div>
+ 
+  {% elif session_fellowship and session_fellowship in voting_round.eligible_to_vote.all %}
+
+    {% if voting_round.is_open %}
+      <div id="nomination-{{ voting_round.nomination.id }}-vote"
+           class="col-12 col-md-4"
+           hx-get="{% url 'colleges:_hx_nomination_vote' voting_round_id=voting_round.id %}"
+           hx-trigger="intersect once"></div>
+    {% elif voting_round.decision %}
+      <div class="col-12 col-md-4 d-flex flex-column justify-content-between">
+        <div>{% include "colleges/_voting_results_box.html" with voting_round=voting_round %}</div>
+ 
+        <div>
+          <h3>Decision</h3>
+
+          {% if voting_round.decision.outcome == 'elected' %}
+            <div class="badge fs-5 mb-2 bg-success">{{ voting_round.decision.get_outcome_display }}</div>
+          {% elif voting_round.decision.outcome == 'notelected' %}
+            <div class="badge fs-5 mb-2 bg-danger">{{ voting_round.decision.get_outcome_display }}</div>
+          {% endif %}
+
+          {% if voting_round.decision.comments %}
+            <h4 class="mt-2">Decision comments</h4>
+            <span>{{ voting_round.decision.comments }}</span>
+          {% endif %}
+
+        </div>
+
+      </div>
+
+    {% endif %}
+
   {% endif %}
 
 </div>
 
-{% if voting_round.is_closed %}
-  <div class="row mb-0">
-    <div class="col-12 col-md mt-3">
-      <div hx-get="{% url 'colleges:_hx_nomination_decision_form' round_id=voting_round.id %}"
-           hx-trigger="intersect once"></div>
+{% if "edadmin" in user_roles %}
+
+  {% if voting_round.is_closed %}
+    <div class="row mb-0">
+      <div class="col-12 col-md mt-3">
+        <div hx-get="{% url 'colleges:_hx_nomination_decision_form' round_id=voting_round.id %}"
+             hx-trigger="intersect once"></div>
+      </div>
+      <div class="col-auto mt-3">{% include "colleges/_voting_results_box.html" with voting_round=voting_round %}</div>
     </div>
-    <div class="col-auto mt-3">{% include "colleges/_voting_results_box.html" with voting_round=voting_round %}</div>
-  </div>
-{% elif "edadmin" in user_roles %}
-  <div class="col-12">
-    <h4>Start round</h4>
-    {% crispy round_start_form %}
-  </div>
+  {% else %}
+    <div class="col-12">
+      <h4>Start round</h4>
+      {% crispy round_start_form %}
+    </div>
+  {% endif %}
+
 {% endif %}
diff --git a/scipost_django/colleges/views.py b/scipost_django/colleges/views.py
index df7022cfa..9c0277f94 100644
--- a/scipost_django/colleges/views.py
+++ b/scipost_django/colleges/views.py
@@ -1050,6 +1050,12 @@ def _hx_nomination_vote(request, voting_round_id):
         pk=voting_round_id,
         eligible_to_vote=fellowship,
     )
+    vote_options_with_color = [
+        (FellowshipNominationVote.VOTE_AGREE, "success"),
+        (FellowshipNominationVote.VOTE_ABSTAIN, "warning"),
+        (FellowshipNominationVote.VOTE_DISAGREE, "danger"),
+        (FellowshipNominationVote.VOTE_VETO, "dark"),
+    ]
     if request.method == "POST":
         vote_object, created = FellowshipNominationVote.objects.update_or_create(
             voting_round=voting_round,
@@ -1077,6 +1083,7 @@ def _hx_nomination_vote(request, voting_round_id):
     context = {
         "voting_round": voting_round,
         "vote_object": vote_object,
+        "vote_options": vote_options_with_color,
     }
     return render(request, "colleges/_hx_nomination_vote.html", context)
 
@@ -1252,7 +1259,7 @@ def _hx_fellowship_invitation_update_response(request, invitation_id):
 
 
 @login_required
-@user_passes_test(is_edadmin)
+@user_passes_test(is_edadmin_or_senior_fellow)
 def _hx_nomination_voter_table(request, round_id):
     round = get_object_or_404(FellowshipNominationVotingRound, pk=round_id)
     voters = round.eligible_to_vote.all()
-- 
GitLab