From 9618201c405604ccf3a2baf42896dd7d2d51967b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jean-S=C3=A9bastien=20Caux?= <git@jscaux.org>
Date: Fri, 3 Feb 2023 12:22:28 +0100
Subject: [PATCH] Add motion voting results details (admin view) in forum

---
 .../templates/forums/_hx_motion_voting.html   | 75 ++++++++++++++++++-
 scipost_django/forums/urls.py                 |  5 --
 scipost_django/forums/views.py                |  2 +-
 3 files changed, 75 insertions(+), 7 deletions(-)

diff --git a/scipost_django/forums/templates/forums/_hx_motion_voting.html b/scipost_django/forums/templates/forums/_hx_motion_voting.html
index c09e063f6..21656fe6f 100644
--- a/scipost_django/forums/templates/forums/_hx_motion_voting.html
+++ b/scipost_django/forums/templates/forums/_hx_motion_voting.html
@@ -1,6 +1,79 @@
 {% load bootstrap %}
+{% load guardian_tags %}
 
-{% load crispy_forms_tags %}
+{% get_obj_perms request.user for forum as "forum_user_perms"  %}
+
+{% if "can_administer_forum" in forum_user_perms %}
+  <details class="bg-white">
+    <summary class="bg-danger bg-opacity-10 border border-danger text-dark p-2 my-1">
+      <strong>Voting results (forum admin view)</strong>
+      <button class="btn btn-sm btn-warning ms-4"
+	      hx-get="{% url 'forums:_hx_motion_voting' slug=forum.slug motion_id=motion.id %}"
+	      hx-target="#motion-{{ motion.id }}-voting"
+      >{% include "bi/arrow-clockwise.html" %}&nbsp;refresh</button>
+    </summary>
+    <table class="table table-bordered">
+      <thead>
+	<tr>
+	  <th>Eligible to vote</th>
+	  <th>Agreed</th>
+	  <th>Doubt</th>
+	  <th>Disagreed</th>
+	  <th>Abstained</th>
+	</tr>
+      </thead>
+      <tbody>
+	<tr>
+	  <td>
+	    <ul class="list list-unstyled">
+	      {% for voter in motion.eligible_to_vote.all %}
+		<li>{{ voter.contributor }}</li>
+	      {% empty %}
+		<li>None</li>
+	      {% endfor %}
+	    </ul>
+	  </td>
+	  <td>
+	    <ul class="list list-unstyled">
+	      {% for voter in motion.in_agreement.all %}
+		<li>{{ voter.contributor }}</li>
+	      {% empty %}
+		<li>None</li>
+	      {% endfor %}
+	    </ul>
+	  </td>
+	  <td>
+	    <ul class="list list-unstyled">
+	      {% for voter in motion.in_doubt.all %}
+		<li>{{ voter.contributor }}</li>
+	      {% empty %}
+		<li>None</li>
+	      {% endfor %}
+	    </ul>
+	  </td>
+	  <td>
+	    <ul class="list list-unstyled">
+	      {% for voter in motion.in_disagreement.all %}
+		<li>{{ voter.contributor }}</li>
+	      {% empty %}
+		<li>None</li>
+	      {% endfor %}
+	    </ul>
+	  </td>
+	  <td>
+	    <ul class="list list-unstyled">
+	      {% for voter in motion.in_abstain.all %}
+		<li>{{ voter.contributor }}</li>
+	      {% empty %}
+		<li>None</li>
+	      {% endfor %}
+	    </ul>
+	  </td>
+	</tr>
+      </tbody>
+    </table>
+  </details>
+{% endif %}
 
 <div class="align-self-center px-2 py-1">
   Voting results&emsp;
diff --git a/scipost_django/forums/urls.py b/scipost_django/forums/urls.py
index 8cc146d32..fbd05527c 100644
--- a/scipost_django/forums/urls.py
+++ b/scipost_django/forums/urls.py
@@ -115,11 +115,6 @@ urlpatterns = [
     path(
         "<slug:slug>/motion/<int:motion_id>/",
         include([
-            path(
-                "<str:vote>/",
-                views._hx_motion_voting,
-                name="_hx_motion_voting",
-            ),
             path(
                 "",
                 views._hx_motion_voting,
diff --git a/scipost_django/forums/views.py b/scipost_django/forums/views.py
index af2ef4892..67ffe00a5 100644
--- a/scipost_django/forums/views.py
+++ b/scipost_django/forums/views.py
@@ -402,7 +402,7 @@ def _hx_thread_from_post(request, slug, post_id):
 
 
 @permission_required_or_403("forums.can_post_to_forum", (Forum, "slug", "slug"))
-def _hx_motion_voting(request, slug, motion_id, vote=None):
+def _hx_motion_voting(request, slug, motion_id):
     forum = get_object_or_404(Forum, slug=slug)
     motion = get_object_or_404(Motion, pk=motion_id)
     initial = {
-- 
GitLab