Skip to content

Commit

Permalink
prevent crash when deleting last post from a topic when topic posts_c…
Browse files Browse the repository at this point in the history
…ount is not updated
  • Loading branch information
vincentporte committed Dec 17, 2024
1 parent e3d41b2 commit 7056bb7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
26 changes: 14 additions & 12 deletions lacommunaute/forum_conversation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.contrib.contenttypes.fields import GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.db.models import Q
from django.db.models import Count, Q
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from machina.apps.forum_conversation.abstract_models import AbstractPost, AbstractTopic
Expand All @@ -16,12 +16,12 @@


class TopicQuerySet(models.QuerySet):
def unanswered(self):
def _for_topics_list(self):
# prevent errors when Topic posts_count is not up to date
return (
self.exclude(approved=False)
.exclude(status=Topic.TOPIC_LOCKED)
.exclude(type=Topic.TOPIC_ANNOUNCE)
.filter(posts_count=1)
.annotate(p_count=Count("posts"))
.exclude(p_count=0)
.select_related(
"forum",
"poster",
Expand All @@ -32,16 +32,19 @@ def unanswered(self):
.order_by("-last_post_on")
)

def unanswered(self):
return (
self._for_topics_list()
.exclude(status=Topic.TOPIC_LOCKED)
.exclude(type=Topic.TOPIC_ANNOUNCE)
.filter(posts_count=1)
)

def optimized_for_topics_list(self, user_id):
return (
self.exclude(approved=False)
self._for_topics_list()
.filter(type__in=[Topic.TOPIC_POST, Topic.TOPIC_STICKY, Topic.TOPIC_ANNOUNCE])
.select_related(
"forum",
"poster",
"poster__forum_profile",
"first_post",
"first_post__poster",
"certified_post",
"certified_post__post",
"certified_post__post__poster",
Expand All @@ -54,7 +57,6 @@ def optimized_for_topics_list(self, user_id):
"certified_post__post__attachments",
"tags",
)
.order_by("-last_post_on")
)


Expand Down
7 changes: 7 additions & 0 deletions lacommunaute/forum_conversation/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,13 @@ def test_anonymous_content(self, client, db, topics_url, snapshot):
content = parse_response_to_soup(response, selector="#action-box")
assert str(content) == snapshot(name="anonymous_action_box")

def test_batcase_on_topic_posts_count(self, client, db, topics_url):
topic = TopicFactory()
topic.posts_count = 1
topic.save()
response = client.get(topics_url)
assert response.status_code == 200


class TestPosterTemplate:
def test_topic_in_topics_view(self, client, db, topics_url, snapshot):
Expand Down
11 changes: 11 additions & 0 deletions lacommunaute/pages/tests/test_homepage.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ def test_unsanswered_topics(db, client, nb_topics, nb_expected, snapshot):
assert ('id="unanswered_topics"' in str(response.content)) == (nb_expected > 0)


def test_batcase_on_topic_posts_count(db, client):
topic = TopicFactory()
# vincentporte - 2024-12-17
# topic posts_count is updated when post is created or deleted.
# a case of message deletion occurred where posts_count had not been updated and caused a display crash.
topic.posts_count = 1
topic.save()
response = client.get(reverse("pages:home"))
assert response.status_code == 200


def test_numqueries(db, client, django_assert_num_queries):
savepoint_queries = 4
session_queries = 2
Expand Down

0 comments on commit 7056bb7

Please sign in to comment.