diff --git a/apps/forums/models.py b/apps/forums/models.py index fb5692ad7..083f65507 100644 --- a/apps/forums/models.py +++ b/apps/forums/models.py @@ -95,6 +95,31 @@ class Post(ModelBase): self.thread.forum.last_post = self self.thread.forum.save() + def delete(self, *args, **kwargs): + """Override delete method to update parent thread info.""" + + thread = self.thread + if thread.last_post and thread.last_post.id == self.id: + try: + thread.last_post = thread.post_set.all() \ + .order_by('-created')[1] + except IndexError: + # The thread has only one Post so let the delete cascade. + pass + thread.replies = thread.post_set.count() - 2 + thread.save() + + forum = thread.forum + if forum.last_post and forum.last_post.id == self.id: + try: + forum.last_post = Post.objects.filter(thread__forum=forum) \ + .order_by('-created')[1] + except IndexError: + forum.last_post = None + forum.save() + + super(Post, self).delete(*args, **kwargs) + @property def page(self): """Get the page of the thread on which this post is found.""" diff --git a/apps/forums/templates/confirm_post_delete.html b/apps/forums/templates/confirm_post_delete.html new file mode 100644 index 000000000..62aff599f --- /dev/null +++ b/apps/forums/templates/confirm_post_delete.html @@ -0,0 +1,26 @@ +{# vim: set ts=2 et sts=2 sw=2: #} +{% extends "common/base.html" %} +{# L10n: {t} is the title of the thread. {f} if the name of the forum. #} +{% set title = _('Delete Post | {t} | {f} | Forums')|f(t=thread.title, f=forum.name) %} +{% set crumbs = [(url('forums.forums'), _('Forums')), + (url('forums.threads', forum.slug), forum.name), + (url('forums.posts', forum.slug, thread.id), thread.title), + (None, _('Delete Post'))] %} + +{% block content %} +