From 0a5e9ef065b1e52c6e603b51e37ba7f60f12467f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jean-S=C3=A9bastien=20Caux?= <git@jscaux.org>
Date: Sun, 29 Jan 2023 15:23:11 +0100
Subject: [PATCH] Accelerate with prefetch, improve listing

---
 .../migrations/0012_alter_meeting_options.py  | 17 ++++++++++++
 scipost_django/forums/models.py               |  5 ++++
 .../forums/templates/forums/forum_as_li.html  |  2 +-
 .../forums/templates/forums/forum_list.html   | 26 +++++++++++++------
 scipost_django/forums/views.py                |  5 +++-
 5 files changed, 45 insertions(+), 10 deletions(-)
 create mode 100644 scipost_django/forums/migrations/0012_alter_meeting_options.py

diff --git a/scipost_django/forums/migrations/0012_alter_meeting_options.py b/scipost_django/forums/migrations/0012_alter_meeting_options.py
new file mode 100644
index 000000000..b9af8685b
--- /dev/null
+++ b/scipost_django/forums/migrations/0012_alter_meeting_options.py
@@ -0,0 +1,17 @@
+# Generated by Django 3.2.16 on 2023-01-29 14:03
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('forums', '0011_alter_motion_accepted'),
+    ]
+
+    operations = [
+        migrations.AlterModelOptions(
+            name='meeting',
+            options={'ordering': ['-date_until']},
+        ),
+    ]
diff --git a/scipost_django/forums/models.py b/scipost_django/forums/models.py
index 240934aec..212406cd6 100644
--- a/scipost_django/forums/models.py
+++ b/scipost_django/forums/models.py
@@ -149,6 +149,11 @@ class Meeting(Forum):
     )
     objects = models.Manager()
 
+    class Meta:
+        ordering = [
+            "-date_until",
+        ]
+
     def __str__(self):
         return "%s, [%s to %s]" % (
             self.forum,
diff --git a/scipost_django/forums/templates/forums/forum_as_li.html b/scipost_django/forums/templates/forums/forum_as_li.html
index 90415ecd2..b3a26a66a 100644
--- a/scipost_django/forums/templates/forums/forum_as_li.html
+++ b/scipost_django/forums/templates/forums/forum_as_li.html
@@ -1,6 +1,6 @@
 <li class="d-flex flex-wrap justify-content-between">
   <a href="{{ forum.get_absolute_url }}">{{ forum }}</a>
-  <span class="badge bg-secondary rounded-pill">{% with nr_posts=forum.nr_posts %}{{ nr_posts }} post{{ nr_posts|pluralize }}{% endwith %}</span>
+  <span class="badge bg-primary rounded-pill">{% with nr_posts=forum.nr_posts %}{{ nr_posts }} post{{ nr_posts|pluralize }}{% endwith %}</span>
   {% if forum.child_forums.all|length > 0 %}
     <ul class="list-unstyled forumList">
       {% for child in forum.child_forums.all %}
diff --git a/scipost_django/forums/templates/forums/forum_list.html b/scipost_django/forums/templates/forums/forum_list.html
index 457eccf5f..d2efcda13 100644
--- a/scipost_django/forums/templates/forums/forum_list.html
+++ b/scipost_django/forums/templates/forums/forum_list.html
@@ -45,7 +45,13 @@
 		{% endif %}
 		<span class="d-flex flex-wrap justify-content-between">
 		  <a href="{{ forum.get_absolute_url }}">{{ forum|truncatechars:30 }}</a>
-		  <span class="badge bg-primary rounded-pill">{% with nr_posts=forum.nr_posts %}{{ nr_posts }} post{{ nr_posts|pluralize }}{% endwith %}</span>
+		  {% with nr_posts=forum.nr_posts %}
+		    {% if nr_posts %}
+		      <span class="badge bg-primary rounded-pill">
+			{{ nr_posts }} post{{ nr_posts|pluralize }}
+		    {% endif %}
+		  {% endwith %}
+		  </span>
 		</span>
 	      </div>
 	      <div class="card-body">
@@ -54,7 +60,7 @@
 		  <hr/>
 		  <p>Descendants:</p>
 		  <ul class="list-unstyled forumList">
-		    {% for child in forum.child_forums.all %}
+		    {% for child in forum.child_forums.all reversed %}
 		      {% include 'forums/forum_as_li.html' with forum=child %}
 		    {% endfor %}
 		  </ul>
@@ -83,12 +89,16 @@
 	</thead>
 	<tbody>
 	  {% for forum in object_list %}
-	    <tr>
-	      <td><a href="{{ forum.get_absolute_url }}">{{ forum }}</a></td>
-	      <td>{{ forum.latest_post }}</td>
-	      <td>{{ forum.latest_post.posted_on|date:"Y-m-d" }}</td>
-	      <td><span class="badge bg-primary rounded-pill">{{ forum.nr_posts }}</span></td>
-	    </tr>
+	    {% with formul.latest_post as latest_post %}
+	      {% if latest_post %}
+		<tr>
+		  <td><a href="{{ forum.get_absolute_url }}">{{ forum }}</a></td>
+		  <td>{{ forum.latest_post }}</td>
+		  <td>{{ forum.latest_post.posted_on|date:"Y-m-d" }}</td>
+		  <td><span class="badge bg-primary rounded-pill">{{ forum.nr_posts }}</span></td>
+		</tr>
+	      {% endif %}
+	    {% endwith %}
 	  {% empty %}
 	    <tr>
 	      <td>No visible Posts found.</td>
diff --git a/scipost_django/forums/views.py b/scipost_django/forums/views.py
index 8dd189e6d..a5c0f4ff2 100644
--- a/scipost_django/forums/views.py
+++ b/scipost_django/forums/views.py
@@ -176,7 +176,10 @@ class ForumListView(LoginRequiredMixin, ListView):
     def get_queryset(self):
         queryset = get_objects_for_user(
             self.request.user, "forums.can_view_forum"
-        ).anchors()
+        ).anchors().select_related("meeting").prefetch_related(
+            "posts" + "__followup_posts" * 3,
+            "child_forums__posts" + "__followup_posts" * 7,
+        )
         return queryset
 
 
-- 
GitLab