Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(forum): retrait de la notion de kind sur Forum #769

Merged
merged 9 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lacommunaute/forum/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
class ForumAdmin(BaseForumAdmin):
fieldsets = BaseForumAdmin.fieldsets
fieldsets[0][1]["fields"] += ("short_description", "certified", "tags", "partner")
fieldsets[1][1]["fields"] += ("kind",)


@admin.register(ForumRating)
Expand Down
6 changes: 0 additions & 6 deletions lacommunaute/forum/enums.py

This file was deleted.

17 changes: 17 additions & 0 deletions lacommunaute/forum/migrations/0021_remove_forum_kind.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.0.8 on 2024-09-05 14:39

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("forum", "0020_remove_forum_invitation_token_and_more"),
("search", "0002_remove_kind_in_materialized_view"),
]

operations = [
migrations.RemoveField(
model_name="forum",
name="kind",
),
]
14 changes: 3 additions & 11 deletions lacommunaute/forum/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,18 @@
from storages.backends.s3boto3 import S3Boto3Storage
from taggit.managers import TaggableManager

from lacommunaute.forum.enums import Kind as Forum_Kind
from lacommunaute.forum_conversation.models import Topic
from lacommunaute.forum_upvote.models import UpVote
from lacommunaute.partner.models import Partner
from lacommunaute.utils.validators import validate_image_size


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


class Forum(AbstractForum):
kind = models.CharField(
max_length=20, choices=Forum_Kind.choices, default=Forum_Kind.PUBLIC_FORUM, verbose_name="Type"
)
short_description = models.CharField(
max_length=400, blank=True, null=True, verbose_name="Description courte (SEO)"
)
Expand Down Expand Up @@ -67,11 +63,7 @@ def is_in_documentation_area(self):

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

@cached_property
def is_newsfeed(self):
return self.kind == Forum_Kind.NEWS
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
16 changes: 0 additions & 16 deletions lacommunaute/forum/tests/__snapshots__/tests_views.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,6 @@



</ol>
</nav>
'''
# ---
# name: TestBreadcrumb.test_newsfeed_forum[newsfeed_forum]
'''
<nav aria-label="Fil d'ariane" class="c-breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item">Retourner vers</li>
<li class="breadcrumb-item">

<a class="matomo-event" data-matomo-action="view" data-matomo-category="engagement" data-matomo-option="newsfeed_breadcrumb" href="/news/">Actualités</a>

</li>


</ol>
</nav>
'''
Expand Down
2 changes: 0 additions & 2 deletions lacommunaute/forum/tests/test_categoryforum_createview.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from machina.core.db.models import get_model
from pytest_django.asserts import assertContains
from lacommunaute.forum.enums import Kind as ForumKind
from lacommunaute.forum.models import Forum
from lacommunaute.users.factories import UserFactory

Expand Down Expand Up @@ -56,7 +55,6 @@ def test_create_category_with_perms(client, db):

forum = Forum.objects.get()
assert forum.type == Forum.FORUM_CAT
assert forum.kind == ForumKind.PUBLIC_FORUM
assert forum.parent is None

assert UserForumPermission.objects.filter(forum=forum).count() == 14
2 changes: 0 additions & 2 deletions lacommunaute/forum/tests/test_subcategoryforum_createview.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from machina.core.db.models import get_model
from pytest_django.asserts import assertContains

from lacommunaute.forum.enums import Kind as ForumKind
from lacommunaute.forum.models import Forum
from lacommunaute.users.factories import UserFactory
from lacommunaute.forum.factories import CategoryForumFactory
Expand Down Expand Up @@ -65,7 +64,6 @@ def test_create_subcategory_with_perms(client, db):

forum = category_forum.children.get()
assert forum.type == Forum.FORUM_POST
assert forum.kind == ForumKind.PUBLIC_FORUM
assert forum.parent == category_forum

assert UserForumPermission.objects.filter(forum=forum).count() == 14
35 changes: 12 additions & 23 deletions lacommunaute/forum/tests/tests_model.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
from django.conf import settings
from django.test import TestCase

from lacommunaute.forum.enums import Kind as ForumKind
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


class ForumManagerTest(TestCase):
def test_public_method(self):
forum = ForumFactory(kind=ForumKind.PUBLIC_FORUM)
ForumFactory(kind=ForumKind.NEWS)
self.assertEqual(forum, Forum.objects.public().get())


class ForumModelTest(TestCase):
def test_get_unanswered_topics(self):
topic1 = TopicFactory(forum=ForumFactory(), posts_count=1)
Expand All @@ -31,12 +23,6 @@ def test_count_unanswered_topics(self):
TopicFactory(forum=ForumFactory(parent=topic.forum), posts_count=1)
self.assertEqual(topic.forum.count_unanswered_topics, 2)

def test_kind(self):
self.assertEqual(
Forum.kind.field.flatchoices,
[("PUBLIC_FORUM", "Espace public"), ("NEWS", "Actualités")],
)

def test_get_absolute_url(self):
forum = ForumFactory()
self.assertEqual(
Expand Down Expand Up @@ -80,20 +66,11 @@ def test_is_toplevel_discussion_area(self):
sub_discussion_area_forum = ForumFactory(parent=discussion_area_forum)
forum = ForumFactory()
sub_forum = ForumFactory(parent=forum)
news_forum = ForumFactory(kind=ForumKind.NEWS)

self.assertTrue(discussion_area_forum.is_toplevel_discussion_area)
self.assertFalse(sub_discussion_area_forum.is_toplevel_discussion_area)
self.assertFalse(forum.is_toplevel_discussion_area)
self.assertFalse(sub_forum.is_toplevel_discussion_area)
self.assertFalse(news_forum.is_toplevel_discussion_area)

def test_is_newsfeed(self):
news_forum = ForumFactory(kind=ForumKind.NEWS)
discussion_area_forum = ForumFactory()

self.assertTrue(news_forum.is_newsfeed)
self.assertFalse(discussion_area_forum.is_newsfeed)

def test_get_session_rating(self):
forum = ForumFactory()
Expand All @@ -111,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]
7 changes: 0 additions & 7 deletions lacommunaute/forum/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,10 +748,3 @@ def test_grandchild_category_forum(self, client, db, snapshot, discussion_area_f
replace_in_href=[parent_forum, parent_forum.get_children().first()],
)
assert str(content) == snapshot(name="grandchild_category_forum")

def test_newsfeed_forum(self, client, db, snapshot, discussion_area_forum):
forum = ForumFactory(kind="NEWS", with_public_perms=True)
response = client.get(reverse("forum_extension:forum", kwargs={"pk": forum.pk, "slug": forum.slug}))
assert response.status_code == 200
content = parse_response_to_soup(response, selector="nav.c-breadcrumb")
assert str(content) == snapshot(name="newsfeed_forum")
4 changes: 1 addition & 3 deletions lacommunaute/forum/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from machina.core.loading import get_class
from taggit.models import Tag

from lacommunaute.forum.enums import Kind as ForumKind
from lacommunaute.forum.forms import ForumForm
from lacommunaute.forum.models import Forum, ForumRating
from lacommunaute.forum_conversation.forms import PostForm
Expand Down Expand Up @@ -140,7 +139,7 @@ class CategoryForumListView(ListView):
context_object_name = "forums"

def get_queryset(self) -> QuerySet[Forum]:
return Forum.objects.filter(type=Forum.FORUM_CAT, kind=ForumKind.PUBLIC_FORUM, level=0)
return Forum.objects.filter(type=Forum.FORUM_CAT, level=0)


class BaseCategoryForumCreateView(UserPassesTestMixin, CreateView):
Expand All @@ -151,7 +150,6 @@ def test_func(self):
return self.request.user.is_superuser

def form_valid(self, form):
form.instance.kind = ForumKind.PUBLIC_FORUM
response = super().form_valid(form)
add_public_perms_on_forum(form.instance)
return response
Expand Down
5 changes: 2 additions & 3 deletions lacommunaute/forum_conversation/shortcuts.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.contrib.contenttypes.models import ContentType
from django.db.models import Count, Exists, OuterRef, Prefetch, Q, QuerySet

from lacommunaute.forum.enums import Kind as Forum_Kind
from lacommunaute.forum_conversation.models import Post, Topic
from lacommunaute.forum_upvote.models import UpVote
from lacommunaute.users.models import User
Expand Down Expand Up @@ -37,5 +36,5 @@ def get_posts_of_a_topic_except_first_one(topic: Topic, user: User) -> QuerySet[
return qs.order_by("created")


def can_certify_post(forum, user):
return user.is_authenticated and forum.kind == Forum_Kind.PUBLIC_FORUM and user.is_staff
def can_certify_post(user):
return user.is_authenticated and user.is_staff
Original file line number Diff line number Diff line change
Expand Up @@ -320,22 +320,6 @@
</nav>
'''
# ---
# name: test_breadcrumbs_on_topic_view[newsfeed_topic]
'''
<nav aria-label="Fil d'ariane" class="c-breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item">Retourner vers</li>
<li class="breadcrumb-item">

<a class="matomo-event" data-matomo-action="view" data-matomo-category="engagement" data-matomo-option="newsfeed_breadcrumb" href="/news/">Actualités</a>

</li>


</ol>
</nav>
'''
# ---
# name: test_queryset_for_tagged_topic[tagged_topic]
'''
<div class="flex-grow-1" id="topic-list-filter-header">
Expand Down
10 changes: 3 additions & 7 deletions lacommunaute/forum_conversation/tests/tests_shortcuts.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.contrib.auth.models import AnonymousUser
from django.test import TestCase

from lacommunaute.forum.enums import Kind as ForumKind
from lacommunaute.forum.factories import ForumFactory
from lacommunaute.forum_conversation.factories import PostFactory, TopicFactory
from lacommunaute.forum_conversation.shortcuts import can_certify_post, get_posts_of_a_topic_except_first_one
Expand Down Expand Up @@ -78,14 +77,11 @@ def setUpTestData(cls):
cls.forum = ForumFactory.create()

def test_user_is_not_authenticated(self):
self.assertFalse(can_certify_post(self.forum, AnonymousUser()))

def test_forum_is_newsfeed(self):
self.assertFalse(can_certify_post(ForumFactory.create(kind=ForumKind.NEWS), self.user))
self.assertFalse(can_certify_post(AnonymousUser()))

def test_user_is_staff(self):
self.user.is_staff = True
self.assertTrue(can_certify_post(self.forum, self.user))
self.assertTrue(can_certify_post(self.user))

def test_user_is_not_staff(self):
self.assertFalse(can_certify_post(self.forum, self.user))
self.assertFalse(can_certify_post(self.user))
53 changes: 3 additions & 50 deletions lacommunaute/forum_conversation/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from faker import Faker
from machina.core.db.models import get_model
from machina.core.loading import get_class

from pytest_django.asserts import assertContains, assertNotContains
from taggit.models import Tag

from lacommunaute.forum.enums import Kind as ForumKind
from lacommunaute.forum.factories import CategoryForumFactory, ForumFactory
from lacommunaute.forum_conversation.enums import Filters
from lacommunaute.forum_conversation.factories import (
Expand Down Expand Up @@ -808,7 +808,6 @@ def test_breadcrumbs_on_topic_view(client, db, snapshot):
discussion_area_topic = TopicFactory(
with_post=True, forum=ForumFactory(with_public_perms=True, parent=discussion_area_forum, name="Forum B")
)
newsfeed_topic = TopicFactory(with_post=True, forum=ForumFactory(kind=ForumKind.NEWS, with_public_perms=True))

response = client.get(
reverse(
Expand Down Expand Up @@ -859,21 +858,6 @@ def test_breadcrumbs_on_topic_view(client, db, snapshot):
)
assert str(content) == snapshot(name="discussion_area_topic")

response = client.get(
reverse(
"forum_conversation:topic",
kwargs={
"forum_pk": newsfeed_topic.forum.pk,
"forum_slug": newsfeed_topic.forum.slug,
"pk": newsfeed_topic.pk,
"slug": newsfeed_topic.slug,
},
)
)
assert response.status_code == 200
content = parse_response_to_soup(response, selector="nav.c-breadcrumb")
assert str(content) == snapshot(name="newsfeed_topic")


class TopicListViewTest(TestCase):
@classmethod
Expand Down Expand Up @@ -912,8 +896,6 @@ def test_context_with_tag(self):
self.assertEqual(response.context_data["active_tags_label"], " ou ".join([tag.name for tag in tags]))

def test_queryset(self):
TopicFactory(with_post=True, forum=ForumFactory(kind=ForumKind.NEWS, with_public_perms=True))

response = self.client.get(self.url)

self.assertEqual(response.status_code, 200)
Expand All @@ -928,8 +910,8 @@ def test_queryset(self):
self.assertContains(response, topic.subject)

def test_queryset_for_unanswered_topic(self):
TopicFactory(with_post=True, forum=ForumFactory(kind=ForumKind.NEWS, with_public_perms=True))

# answered topic
PostFactory(topic=TopicFactory(with_post=True, forum=self.forum))
response = self.client.get(self.url + "?filter=NEW")
self.assertEqual(response.context_data["paginator"].count, 1)
self.assertContains(response, self.topic.subject, status_code=200)
Expand Down Expand Up @@ -1076,35 +1058,6 @@ def test_topic_in_its_own_public_forum(self, client, db, snapshot):
assert str(soup) == snapshot(name="topic_in_its_own_public_forum")


class NewsFeedTopicListViewTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.url = reverse("forum_conversation_extension:newsfeed")

def test_template_name(self):
response = self.client.get(self.url)
self.assertTemplateUsed(response, "forum_conversation/topics_newsfeed.html")

response = self.client.get(self.url, **{"HTTP_HX_REQUEST": "true"})
self.assertTemplateUsed(response, "forum_conversation/topic_list_newsfeed.html")

def test_queryset(self):
news_topics = TopicFactory.create_batch(
2, with_post=True, forum=ForumFactory(kind=ForumKind.NEWS, with_public_perms=True)
)
TopicFactory(with_post=True, forum=ForumFactory(kind=ForumKind.PUBLIC_FORUM, with_public_perms=True))

response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertQuerySetEqual(response.context_data["topics"], [topic for topic in news_topics[::-1]])

def test_context_data(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context_data["forum"], None)
self.assertEqual(response.context_data["loadmoretopic_url"], reverse("forum_conversation_extension:newsfeed"))


class TestTopicCreateCheckView:
def test_get_method(self, client, db, snapshot):
forum = ForumFactory(name="forum")
Expand Down
Loading
Loading