From 24c02f4638d1fb72078554b7a76ebbe3afe570e2 Mon Sep 17 00:00:00 2001 From: Skia Date: Mon, 10 Apr 2017 15:34:32 +0200 Subject: [PATCH] Fix last message when the real last is deleted --- forum/models.py | 10 +++++++--- forum/templates/forum/macros.jinja | 4 +++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/forum/models.py b/forum/models.py index f7c4a481..3b3d73a1 100644 --- a/forum/models.py +++ b/forum/models.py @@ -116,10 +116,8 @@ class Forum(models.Model): last_msg = None for m in ForumMessage.objects.select_related('topic__forum', 'author').order_by('-id'): forum = m.topic.forum - if self in (forum.parent_list + [forum]): + if self in (forum.parent_list + [forum]) and not m.deleted: return m - last_msg = m - return last_msg class ForumTopic(models.Model): forum = models.ForeignKey(Forum, related_name='topics') @@ -151,6 +149,12 @@ class ForumTopic(models.Model): except: return None + @cached_property + def last_message(self): + for msg in self.messages.order_by('id').select_related('author').order_by('-id').all(): + if not msg.deleted: + return msg + @property def title(self): return self.messages.order_by('date').first().title diff --git a/forum/templates/forum/macros.jinja b/forum/templates/forum/macros.jinja index 88163e9f..a0f48e21 100644 --- a/forum/templates/forum/macros.jinja +++ b/forum/templates/forum/macros.jinja @@ -67,11 +67,13 @@
- {% set last_msg = topic.messages.order_by('id').select_related('author').last() %} + {% set last_msg = topic.last_message %} + {% if last_msg %} {{ user_profile_link(last_msg.author) }}
{{ last_msg.date|date(DATETIME_FORMAT) }} {{ last_msg.date|time(DATETIME_FORMAT) }} + {% endif %}