Skip to content

Commit

Permalink
add get_main_forum method
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Sep 10, 2024
1 parent b1ba2e6 commit aa9909a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
9 changes: 7 additions & 2 deletions lacommunaute/forum/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
from lacommunaute.utils.validators import validate_image_size


class ForumQuerySet(models.QuerySet):
def get_main_forum(self):
return self.filter(lft=1, level=0).first()


class Forum(AbstractForum):
short_description = models.CharField(
max_length=400, blank=True, null=True, verbose_name="Description courte (SEO)"
Expand All @@ -29,7 +34,7 @@ class Forum(AbstractForum):
tags = TaggableManager()
partner = models.ForeignKey(Partner, on_delete=models.CASCADE, null=True, blank=True)

objects = models.Manager()
objects = ForumQuerySet.as_manager()

def get_absolute_url(self):
return reverse(
Expand Down Expand Up @@ -58,7 +63,7 @@ def is_in_documentation_area(self):

@cached_property
def is_toplevel_discussion_area(self):
return self == Forum.objects.filter(lft=1, level=0).first()
return self == Forum.objects.get_main_forum()

def get_session_rating(self, session_key):
return getattr(ForumRating.objects.filter(forum=self, session_id=session_key).first(), "rating", None)
Expand Down
13 changes: 13 additions & 0 deletions lacommunaute/forum/tests/tests_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.test import TestCase

from lacommunaute.forum.factories import CategoryForumFactory, ForumFactory, ForumRatingFactory
from lacommunaute.forum.models import Forum
from lacommunaute.forum_conversation.factories import TopicFactory
from lacommunaute.users.factories import UserFactory

Expand Down Expand Up @@ -87,3 +88,15 @@ def test_get_average_rating(self):
ForumRatingFactory(forum=forum, rating=5)

self.assertEqual(forum.get_average_rating(), 3)


class TestForumQueryset:
def test_get_main_forum_wo_forum(self, db):
assert Forum.objects.get_main_forum() is None

def test_get_main_forum_w_several_forums(self, db):
# level 0
forums = ForumFactory.create_batch(2)
# level 1
ForumFactory(parent=forums[0])
assert Forum.objects.get_main_forum() == forums[0]
2 changes: 1 addition & 1 deletion lacommunaute/forum_conversation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def get_context_data(self, **kwargs):
)

context["loadmoretopic_suffix"] = "topics"
context["forum"] = Forum.objects.filter(lft=1, level=0).first()
context["forum"] = Forum.objects.get_main_forum()
context = context | self.get_topic_filter_context()

return context
2 changes: 1 addition & 1 deletion lacommunaute/pages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
context = super().get_context_data(**kwargs)
context["topics_public"] = Topic.objects.filter(approved=True).order_by("-created")[:4]
context["forums_category"] = Forum.objects.filter(parent__type=1).order_by("-updated")[:4]
context["forum"] = Forum.objects.filter(lft=1, level=0).first()
context["forum"] = Forum.objects.get_main_forum()
context["upcoming_events"] = Event.objects.filter(date__gte=timezone.now()).order_by("date")[:4]
return context

Expand Down
2 changes: 1 addition & 1 deletion lacommunaute/search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ def get_queryset(self):

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["forum"] = Forum.objects.filter(lft=1, level=0).first()
context["forum"] = Forum.objects.get_main_forum()
return context

0 comments on commit aa9909a

Please sign in to comment.