From cb91ab479c832e50bb398d61fbf8420e9b123972 Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Mon, 4 Sep 2023 14:20:12 +0200
Subject: [PATCH] add removal of voter from nomination round

---
 .../_hx_nomination_eligible_voters.html       | 27 ++++++++++++-------
 .../_hx_nomination_eligible_voters_table.html | 14 +++++++---
 scipost_django/colleges/urls.py               | 10 +++----
 scipost_django/colleges/views.py              | 17 ++++++++++++
 4 files changed, 48 insertions(+), 20 deletions(-)

diff --git a/scipost_django/colleges/templates/colleges/_hx_nomination_eligible_voters.html b/scipost_django/colleges/templates/colleges/_hx_nomination_eligible_voters.html
index 256c3de0e..08fbd3cfc 100644
--- a/scipost_django/colleges/templates/colleges/_hx_nomination_eligible_voters.html
+++ b/scipost_django/colleges/templates/colleges/_hx_nomination_eligible_voters.html
@@ -1,12 +1,19 @@
 <h4>Eligible Voters</h4>
 
-{% include 'colleges/_hx_nomination_eligible_voters_table.html'  with eligible_voters=eligible_voters %}
-
-{% if eligible_voters|length > 0 %}
-  <button id="nomination-{{ nomination.id }}-start-round-btn" 
-          hx-get="{% url 'colleges:_hx_nomination_round_start' nomination_id=nomination.id %}" 
-          hx-target="#nomination-{{ nomination.id }}-eligible-voters" 
-          class="nomination-start-round-btn btn btn-{% if eligible_voters|length < 5 %}warning{% else %}primary{% endif %}"
-          {% if eligible_voters|length < 5 %}hx-confirm="Are you sure you want to start a round with fewer than 5 voters?"{% endif %}
-    >Start round</button>
-{% endif %}
+{% include 'colleges/_hx_nomination_eligible_voters_table.html'  with round=round %}
+
+{% with round.eligible_to_vote.all|length as nr_voters %}
+
+  {% if nr_voters > 0 %}
+    <div class="d-flex justify-content-between">
+      <button id="nomination-{{ nomination.id }}-start-round-btn" 
+        class="nomination-start-round-btn btn btn-{% if nr_voters < 5 %}warning{% else %}primary{% endif %}" 
+        hx-get="{% url 'colleges:_hx_nomination_round_start' nomination_id=nomination.id %}" hx-target="#nomination-{{ nomination.id }}-eligible-voters" 
+        {% if nr_voters < 5 %}hx-confirm="Are you sure you want to start a round with fewer than 5 voters?"{% endif %}
+        >Start round</button>
+ 
+      <span class="fs-5 border-top border-dark pt-2">Total: {{ nr_voters }}</span>
+    </div>
+  {% endif %}
+
+{% endwith %}
diff --git a/scipost_django/colleges/templates/colleges/_hx_nomination_eligible_voters_table.html b/scipost_django/colleges/templates/colleges/_hx_nomination_eligible_voters_table.html
index 009a96b26..113fff51a 100644
--- a/scipost_django/colleges/templates/colleges/_hx_nomination_eligible_voters_table.html
+++ b/scipost_django/colleges/templates/colleges/_hx_nomination_eligible_voters_table.html
@@ -1,4 +1,4 @@
-{% if eligible_voters %}
+{% if round.eligible_to_vote.all %}
   <table class="table">
     <thead class="table-light">
       <tr>
@@ -6,13 +6,14 @@
         <th>College</th>
         <th>Specialties</th>
         <th>Type</th>
+        <th></th>
       </tr>
     </thead>
 
     <tbody>
 
-      {% for voter in eligible_voters %}
-        <tr>
+      {% for voter in round.eligible_to_vote.all %}
+        <tr class="align-middle">
           <td>{{ voter.contributor }}</td>
           <td>{{ voter.college.name }}</td>
           <td>
@@ -28,6 +29,13 @@
 
           </td>
           <td>{{ voter.get_status_display }}</td>
+          {% comment %} Actions {% endcomment %}
+          <td class="text-end">
+            <a class="btn btn-sm btn-danger"
+               role="button"
+               hx-get="{% url 'colleges:_hx_nomination_round_remove_voter' round_id=round.id voter_id=voter.id %}"
+               hx-target="closest tr"><small>{% include 'bi/trash-fill.html' %}</small></a>
+          </td>
         </tr>
       {% endfor %}
 
diff --git a/scipost_django/colleges/urls.py b/scipost_django/colleges/urls.py
index 01027c691..80cb911c3 100644
--- a/scipost_django/colleges/urls.py
+++ b/scipost_django/colleges/urls.py
@@ -195,14 +195,10 @@ urlpatterns = [
         name="_hx_nominations_no_round_started",
     ),
     path(
-        "_hx_nomination_eligible_voters/<int:nomination_id>",
-        views._hx_nomination_eligible_voters,
-        name="_hx_nomination_eligible_voters",
-    ),
     path(
-        "_hx_nomination_round_start/<int:nomination_id>",
-        views._hx_nomination_round_start,
-        name="_hx_nomination_round_start",
+        "<int:round_id>/_hx_nomination_round_remove_voter/<int:voter_id>",
+        views._hx_nomination_round_remove_voter,
+        name="_hx_nomination_round_remove_voter",
     ),
     path(
         "_hx_voting_rounds",
diff --git a/scipost_django/colleges/views.py b/scipost_django/colleges/views.py
index 1593db62e..a8b86fb7d 100644
--- a/scipost_django/colleges/views.py
+++ b/scipost_django/colleges/views.py
@@ -788,6 +788,7 @@ def _hx_nomination_eligible_voters(request, nomination_id):
         "nomination": nomination,
         "eligible_voters": eligible_voters,
         "nominee_specialties": nomination.profile.specialties.all(),
+        "round": nomination.voting_rounds.first(),
     }
     return render(
         request,
@@ -796,6 +797,22 @@ def _hx_nomination_eligible_voters(request, nomination_id):
     )
 
 
+def _hx_nomination_round_remove_voter(request, round_id, voter_id):
+    """Remove a voter from a nomination's voting round."""
+    round = get_object_or_404(FellowshipNominationVotingRound, pk=round_id)
+
+    voter = get_object_or_404(Fellowship, pk=voter_id)
+    if voter in round.eligible_to_vote.all():
+        round.eligible_to_vote.remove(voter)
+        round.save()
+        messages.success(
+            request, f"Removed {voter} from the voters list of this round."
+        )
+    else:
+        messages.error(request, f"{voter} was not in the voters list of this round.")
+    return HttpResponse("")
+
+
 def _hx_nomination_round_start(request, nomination_id):
     """Create a voting round for the specified nomination using the default eligible voters."""
     nomination = get_object_or_404(FellowshipNomination, pk=nomination_id)
-- 
GitLab