From e64e691eafcd441fb601ff6001731b3ceb13d4de Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jean-S=C3=A9bastien=20Caux?= <git@jscaux.org>
Date: Thu, 3 Feb 2022 11:54:31 +0100
Subject: [PATCH] Add quick links to anchor, latest followup posts in forums

---
 scipost_django/forums/managers.py             |  7 ++++
 scipost_django/forums/models.py               |  9 +++++
 .../forums/templates/forums/forum_detail.html | 39 ++++++++++++++++++-
 3 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/scipost_django/forums/managers.py b/scipost_django/forums/managers.py
index e7799a8ef..c4aab8b14 100644
--- a/scipost_django/forums/managers.py
+++ b/scipost_django/forums/managers.py
@@ -14,6 +14,13 @@ class ForumQuerySet(models.QuerySet):
 
 class PostQuerySet(models.QuerySet):
 
+    def anchors(self):
+        """Return only the Posts which do not have a Post as a parent."""
+        from django.contrib.contenttypes.models import ContentType
+        type_forum = ContentType.objects.get_by_natural_key('forums', 'forum')
+        type_meeting = ContentType.objects.get_by_natural_key('forums', 'meeting')
+        return self.filter(parent_content_type__in=[type_forum, type_meeting])
+
     def motions_excluded(self):
         """Filter all Motions out of the Post queryset."""
         return self.filter(motion__isnull=True)
diff --git a/scipost_django/forums/models.py b/scipost_django/forums/models.py
index 43823a060..1232aed83 100644
--- a/scipost_django/forums/models.py
+++ b/scipost_django/forums/models.py
@@ -217,12 +217,21 @@ class Post(models.Model):
             nr += self.followup_posts.all().count()
         return nr
 
+    @property
+    def latest_followup(self):
+        return self.followup_posts.last()
+
     def posts_hierarchy_id_list(self):
         id_list = [self.id]
         for post in self.followup_posts.all():
             id_list += post.posts_hierarchy_id_list()
         return id_list
 
+    @property
+    def latest_followup_in_hierarchy(self):
+        id_list = self.posts_hierarchy_id_list()
+        return Post.objects.filter(pk__in=id_list).exclude(pk=self.id).last()
+
     def get_anchor_forum_or_meeting(self):
         """
         Climb back the hierarchy up to the original Forum.
diff --git a/scipost_django/forums/templates/forums/forum_detail.html b/scipost_django/forums/templates/forums/forum_detail.html
index 1a11f8c44..f7737ec32 100644
--- a/scipost_django/forums/templates/forums/forum_detail.html
+++ b/scipost_django/forums/templates/forums/forum_detail.html
@@ -118,7 +118,7 @@
       <div class="card">
 	<div class="card-header">
 	  <button class="btn btn-primary btn-small" data-bs-toggle="collapse" data-bs-target="#recentPostsLinks">
-	    Quick links: posts and motions (click to toggle)
+	    Quick links: all posts and motions (click to toggle)
 	  </button>
 	</div>
 	<div class="card-body collapse" id="recentPostsLinks">
@@ -128,7 +128,7 @@
 	    <li>
 	      <a href="{{ post.get_absolute_url }}">{{ post.subject }}</a> posted by {{ post.posted_by.first_name }} {{ post.posted_by.last_name }} on {{ post.posted_on|date:"Y-m-d H:m" }}
 	      {% if post.parent and not post.motion %}
-	      - regarding <a href="{{ post.parent.get_absolute_url }}">{{ post.parent }}</a>
+		- regarding <a href="{{ post.parent.get_absolute_url }}">{{ post.parent }}</a>
 	      {% endif %}
 	    </li>
 	    {% endfor %}
@@ -138,6 +138,41 @@
     </div>
   </div>
 
+  <div class="row">
+    <div class="col">
+      <div class="card">
+	<div class="card-header">
+	  <button class="btn btn-primary btn-small" data-bs-toggle="collapse" data-bs-target="#anchorPostsLinks">
+	    Quick links: anchor posts, latest followup (click to toggle)
+	  </button>
+	</div>
+	<div class="card-body collapse" id="anchorPostsLinks">
+	  <ul>
+	    {% for post in forum.posts_all.anchors %}
+	      <li>
+		<a href="{{ post.get_absolute_url }}">{{ post.subject }}</a> posted by {{ post.posted_by.first_name }} {{ post.posted_by.last_name }} on {{ post.posted_on|date:"Y-m-d H:m" }}
+		<p>
+		  {% with post.nr_followups as nr_followups %}
+		    {{ nr_followups }} followup{{ nr_followups|pluralize }}
+		  {% endwith %}
+		  {% with post.latest_followup_in_hierarchy as latest_followup %}
+		    {% if latest_followup %}
+		      ,&emsp;latest: <a href="{{ latest_followup.get_absolute_url }}">
+		      {{ latest_followup.subject }}</a>
+		      posted by {{ latest_followup.posted_by.first_name }}
+		      {{ latest_followup.posted_by.last_name }}
+		      on {{ latest_followup.posted_on|date:"Y-m-d H:m" }}
+		    {% endif %}
+		  {% endwith %}
+		</p>
+	      </li>
+	    {% endfor %}
+	  </ul>
+	</div>
+      </div>
+    </div>
+  </div>
+
   <div class="row">
     <div class="col-12">
       <h2 class="highlight" id="Description">Description</h2>
-- 
GitLab