From 49e9122d042dfa7af6cc4d2fd7bdb1d3e4a0dd87 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jean-S=C3=A9bastien=20Caux?= <git@jscaux.org>
Date: Sat, 4 Feb 2023 07:50:22 +0100
Subject: [PATCH] Improve display of motions based on `open_for_voting`
 property

---
 scipost_django/forums/models.py               |  7 +++++-
 .../templates/forums/_hx_motion_voting.html   | 25 +++++++++++--------
 2 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/scipost_django/forums/models.py b/scipost_django/forums/models.py
index d0c891de0..33cc7ac95 100644
--- a/scipost_django/forums/models.py
+++ b/scipost_django/forums/models.py
@@ -6,9 +6,10 @@ import datetime
 
 from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
 from django.contrib.contenttypes.models import ContentType
-from django.urls import reverse
 from django.db import models
+from django.urls import reverse
 from django.utils import timezone
+from django.utils.functional import cached_property
 
 from .managers import ForumQuerySet, PostQuerySet
 
@@ -385,3 +386,7 @@ class Motion(Post):
     accepted = models.BooleanField(null=True)
 
     objects = models.Manager()
+
+    @cached_property
+    def open_for_voting(self):
+        return datetime.date.today() <= self.voting_deadline
diff --git a/scipost_django/forums/templates/forums/_hx_motion_voting.html b/scipost_django/forums/templates/forums/_hx_motion_voting.html
index a8799e228..ac367a3fa 100644
--- a/scipost_django/forums/templates/forums/_hx_motion_voting.html
+++ b/scipost_django/forums/templates/forums/_hx_motion_voting.html
@@ -86,7 +86,7 @@
       You have voted: <strong class="text-danger">Disagree</strong>
     {% elif request.user in motion.in_abstain.all %}
       You have <strong class="text-white">Abstained</strong>
-    {% elif request.user in motion.eligible_for_voting.all %}
+    {% elif request.user in motion.eligible_for_voting.all and motion.open_for_voting %}
       [click to vote]
     {% endif %}
   </span>
@@ -117,8 +117,8 @@
   <div class="btn-group" role="group" aria-label="motion voting buttons">
     <input type="radio" class="btn-check" name="vote"
 	   id="vote-Y" value="Y"
-	   {% if request.user in motion.in_agreement.all %}checked
-	   {% elif request.user not in motion.eligible_for_voting.all %}disabled
+	   {% if request.user in motion.in_agreement.all %}checked{% endif %}
+	   {% if not motion.open_for_voting or request.user not in motion.eligible_for_voting.all %}disabled
 	   {% endif %}
     >
     {% if request.user in motion.in_agreement.all %}
@@ -135,8 +135,8 @@
 
     <input type="radio" class="btn-check" name="vote"
 	   id="vote-M" value="M"
-	   {% if request.user in motion.in_doubt.all %}checked
-	   {% elif request.user not in motion.eligible_for_voting.all %}disabled
+	   {% if request.user in motion.in_doubt.all %}checked{% endif %}
+	   {%  if not motion.open_for_voting or request.user not in motion.eligible_for_voting.all %}disabled
 	   {% endif %}
     >
     {% if request.user in motion.in_doubt.all %}
@@ -153,8 +153,8 @@
 
     <input type="radio" class="btn-check" name="vote"
 	   id="vote-N" value="N"
-	   {% if request.user in motion.in_disagreement.all %}checked
-	   {% elif request.user not in motion.eligible_for_voting.all %}disabled
+	   {% if request.user in motion.in_disagreement.all %}checked{% endif %}
+	   {%  if not motion.open_for_voting or request.user not in motion.eligible_for_voting.all %}disabled
 	   {% endif %}
     >
     {% if request.user in motion.in_disagreement.all %}
@@ -171,8 +171,8 @@
 
     <input type="radio" class="btn-check" name="vote"
 	   id="vote-A" value="A"
-	   {% if request.user in motion.in_abstain.all %}checked
-	   {% elif request.user not in motion.eligible_for_voting.all %}disabled
+	   {% if request.user in motion.in_abstain.all %}checked{% endif %}
+	   {%  if not motion.open_for_voting or request.user not in motion.eligible_for_voting.all %}disabled
 	   {% endif %}
     >
     <label class="btn btn-secondary" for="vote-A">
@@ -188,5 +188,10 @@
 </form>
 
 <div class="align-self-center px-2 py-1">
-  Voting deadline:&emsp;{{ motion.voting_deadline|date:'Y-m-d' }}
+  Voting deadline:&emsp;{{ motion.voting_deadline|date:'Y-m-d' }}&emsp;
+  {% if motion.open_for_voting %}
+    <span class="badge rounded-pill text-bg-success text-white">open for voting</span>
+  {% else %}
+    <span class="badge rounded-pill text-bg-danger">voting closed</span>
+  {% endif %}
 </div>
-- 
GitLab