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 des communautés privées #768

Merged
merged 1 commit into from
Sep 5, 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
6 changes: 1 addition & 5 deletions lacommunaute/forum/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
class ForumAdmin(BaseForumAdmin):
fieldsets = BaseForumAdmin.fieldsets
fieldsets[0][1]["fields"] += ("short_description", "certified", "tags", "partner")
fieldsets[1][1]["fields"] += (
"members_group",
"invitation_token",
"kind",
)
fieldsets[1][1]["fields"] += ("kind",)


@admin.register(ForumRating)
Expand Down
1 change: 0 additions & 1 deletion lacommunaute/forum/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@

class Kind(models.TextChoices):
PUBLIC_FORUM = "PUBLIC_FORUM", "Espace public"
PRIVATE_FORUM = "PRIVATE_FORUM", "Espace privé"
NEWS = "NEWS", "Actualités"
2 changes: 0 additions & 2 deletions lacommunaute/forum/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@

from lacommunaute.forum.models import Forum, ForumRating
from lacommunaute.forum_upvote.models import UpVote
from lacommunaute.users.factories import GroupFactory
from lacommunaute.utils.perms import add_public_perms_on_forum


class ForumFactory(BaseForumFactory):
type = Forum.FORUM_POST
members_group = factory.SubFactory(GroupFactory, name=factory.SelfAttribute("..name"))
name = factory.Sequence(lambda n: f"Forum {n}")
description = factory.Faker("sentence", nb_words=100)
short_description = factory.Faker("sentence", nb_words=10)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.8 on 2024-09-05 10:02

from django.db import migrations


def delete_private_forums(apps, schema_editor):
Forum = apps.get_model("forum", "Forum")
Forum.objects.filter(kind="PRIVATE_FORUM").delete()


class Migration(migrations.Migration):
dependencies = [
("forum", "0018_forum_partner"),
]

operations = [
migrations.RunPython(delete_private_forums),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 5.0.8 on 2024-09-05 10:28

from django.db import migrations, models


def delete_empty_groups(apps, schema_editor):
Group = apps.get_model("auth", "Group")
Group.objects.filter(user__isnull=True).delete()


class Migration(migrations.Migration):
dependencies = [
("forum", "0019_del_private_forum_20240905_1202"),
]

operations = [
migrations.RemoveField(
model_name="forum",
name="invitation_token",
),
migrations.RemoveField(
model_name="forum",
name="members_group",
),
migrations.AlterField(
model_name="forum",
name="kind",
field=models.CharField(
choices=[("PUBLIC_FORUM", "Espace public"), ("NEWS", "Actualités")],
default="PUBLIC_FORUM",
max_length=20,
verbose_name="Type",
),
),
migrations.RunPython(delete_empty_groups),
]
7 changes: 0 additions & 7 deletions lacommunaute/forum/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import uuid

from django.conf import settings
from django.contrib.auth.models import Group
from django.contrib.contenttypes.fields import GenericRelation
from django.db import models
from django.urls import reverse
Expand All @@ -24,10 +21,6 @@ def public(self):


class Forum(AbstractForum):
members_group = models.ForeignKey(
Group, blank=True, null=True, on_delete=models.CASCADE, verbose_name=("Members Group")
)
invitation_token = models.UUIDField(default=uuid.uuid4, unique=True)
kind = models.CharField(
max_length=20, choices=Forum_Kind.choices, default=Forum_Kind.PUBLIC_FORUM, verbose_name="Type"
)
Expand Down
2 changes: 0 additions & 2 deletions lacommunaute/forum/tests/__snapshots__/tests_views.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@
</span>
</a>


</div>
'''
# ---
Expand Down Expand Up @@ -401,7 +400,6 @@
<a class="matomo-event h4 d-block mb-0" data-matomo-action="view" data-matomo-category="engagement" data-matomo-option="forum" href="/forum/test-child-10001/">Test-Child</a>
<small class="text-muted">


0 Sujet

-
Expand Down
2 changes: 0 additions & 2 deletions lacommunaute/forum/tests/test_categoryforum_listview.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from django.urls import reverse
from pytest_django.asserts import assertContains, assertNotContains

from lacommunaute.forum.enums import Kind as ForumKind
from lacommunaute.forum.factories import CategoryForumFactory, ForumFactory
from lacommunaute.forum.models import Forum
from lacommunaute.users.factories import UserFactory
Expand All @@ -19,7 +18,6 @@ def test_queryset(client, db):
forum = ForumFactory(type=Forum.FORUM_CAT)
unvisible_forums = (
ForumFactory(type=Forum.FORUM_CAT, parent=forum),
ForumFactory(type=Forum.FORUM_CAT, kind=ForumKind.PRIVATE_FORUM),
ForumFactory(),
ForumFactory(type=Forum.FORUM_LINK),
)
Expand Down
15 changes: 1 addition & 14 deletions lacommunaute/forum/tests/tests_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.conf import settings
from django.db import IntegrityError
from django.test import TestCase

from lacommunaute.forum.enums import Kind as ForumKind
Expand All @@ -13,18 +12,10 @@ class ForumManagerTest(TestCase):
def test_public_method(self):
forum = ForumFactory(kind=ForumKind.PUBLIC_FORUM)
ForumFactory(kind=ForumKind.NEWS)
ForumFactory(kind=ForumKind.PRIVATE_FORUM)
self.assertEqual(forum, Forum.objects.public().get())


class ForumModelTest(TestCase):
def test_invitation_token_is_unique(self):
forum = ForumFactory()

with self.assertRaises(IntegrityError):
forum.id = None
forum.save()

def test_get_unanswered_topics(self):
topic1 = TopicFactory(forum=ForumFactory(), posts_count=1)
topic2 = TopicFactory(forum=ForumFactory(parent=topic1.forum), posts_count=1)
Expand All @@ -43,7 +34,7 @@ def test_count_unanswered_topics(self):
def test_kind(self):
self.assertEqual(
Forum.kind.field.flatchoices,
[("PUBLIC_FORUM", "Espace public"), ("PRIVATE_FORUM", "Espace privé"), ("NEWS", "Actualités")],
[("PUBLIC_FORUM", "Espace public"), ("NEWS", "Actualités")],
)

def test_get_absolute_url(self):
Expand Down Expand Up @@ -89,24 +80,20 @@ def test_is_toplevel_discussion_area(self):
sub_discussion_area_forum = ForumFactory(parent=discussion_area_forum)
forum = ForumFactory()
sub_forum = ForumFactory(parent=forum)
private_forum = ForumFactory(kind=ForumKind.PRIVATE_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(private_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()
private_forum = ForumFactory(kind=ForumKind.PRIVATE_FORUM)

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

def test_get_session_rating(self):
forum = ForumFactory()
Expand Down
42 changes: 0 additions & 42 deletions lacommunaute/forum/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from pytest_django.asserts import assertContains
from taggit.models import Tag

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.views import ForumView
Expand Down Expand Up @@ -163,42 +162,6 @@ def test_show_more_content(self):
self.assertContains(response, "+ voir la suite")
self.assertEqual(response.context_data["loadmoretopic_suffix"], "topicsinforum")

def test_moderator_links(self):
self.client.force_login(self.user)

# no permission
response = self.client.get(self.url)
self.assertNotContains(
response,
reverse(
"members:forum_profiles",
kwargs={"pk": self.forum.pk, "slug": self.forum.slug},
),
)

# permission
assign_perm("can_approve_posts", self.user, self.forum)
response = self.client.get(self.url)
self.assertContains(
response,
reverse(
"members:forum_profiles",
kwargs={"pk": self.forum.pk, "slug": self.forum.slug},
),
)

# permission but no members group
self.forum.members_group = None
self.forum.save()
response = self.client.get(self.url)
self.assertNotContains(
response,
reverse(
"members:forum_profiles",
kwargs={"pk": self.forum.pk, "slug": self.forum.slug},
),
)

def test_poll_form(self):
topic = TopicFactory(forum=self.forum, poster=self.user, with_post=True, with_poll_vote=True)
poll_option = topic.poll.options.first()
Expand Down Expand Up @@ -425,11 +388,6 @@ def test_queryset_for_certified_topics(self):
self.assertEqual(response.context_data["active_filter_name"], Filters.CERTIFIED.label)

certified_topic = TopicFactory(with_post=True, with_certified_post=True, forum=self.forum)
TopicFactory(
with_post=True,
with_certified_post=True,
forum=ForumFactory(kind=ForumKind.PRIVATE_FORUM, with_public_perms=True),
)

response = self.client.get(self.url + f"?filter={Filters.CERTIFIED.value}")
self.assertEqual(response.context_data["paginator"].count, 1)
Expand Down
6 changes: 1 addition & 5 deletions lacommunaute/forum_conversation/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,4 @@ def get_posts_of_a_topic_except_first_one(topic: Topic, user: User) -> QuerySet[


def can_certify_post(forum, user):
return (
user.is_authenticated
and forum.kind == Forum_Kind.PUBLIC_FORUM
and (user.groups.filter(forum=forum).exists() or user.is_staff)
)
return user.is_authenticated and forum.kind == Forum_Kind.PUBLIC_FORUM and user.is_staff
13 changes: 3 additions & 10 deletions lacommunaute/forum_conversation/tests/tests_shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,12 @@ def setUpTestData(cls):
def test_user_is_not_authenticated(self):
self.assertFalse(can_certify_post(self.forum, AnonymousUser()))

def test_forum_is_private(self):
self.assertFalse(can_certify_post(ForumFactory.create(kind=ForumKind.PRIVATE_FORUM), self.user))

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

def test_user_is_not_in_the_forum_members_group(self):
self.assertFalse(can_certify_post(self.forum, self.user))

def test_user_is_in_the_forum_members_group(self):
self.forum.members_group.user_set.add(self.user)
self.assertTrue(can_certify_post(self.forum, self.user))

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

def test_user_is_not_staff(self):
self.assertFalse(can_certify_post(self.forum, self.user))
10 changes: 1 addition & 9 deletions lacommunaute/forum_conversation/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ def test_numqueries(self):
self.client.force_login(self.poster)

# note vincentporte : to be optimized
with self.assertNumQueries(40):
with self.assertNumQueries(39):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)

Expand Down Expand Up @@ -912,7 +912,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.PRIVATE_FORUM, with_public_perms=True))
TopicFactory(with_post=True, forum=ForumFactory(kind=ForumKind.NEWS, with_public_perms=True))

response = self.client.get(self.url)
Expand All @@ -929,7 +928,6 @@ def test_queryset(self):
self.assertContains(response, topic.subject)

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

response = self.client.get(self.url + "?filter=NEW")
Expand All @@ -944,11 +942,6 @@ def test_queryset_for_certified_topic(self):
certified_topic = TopicFactory(
with_post=True, with_certified_post=True, forum=ForumFactory(with_public_perms=True)
)
TopicFactory(
with_post=True,
with_certified_post=True,
forum=ForumFactory(kind=ForumKind.PRIVATE_FORUM, with_public_perms=True),
)

response = self.client.get(self.url + "?filter=CERTIFIED")
self.assertEqual(response.context_data["paginator"].count, 1)
Expand Down Expand Up @@ -1099,7 +1092,6 @@ 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.PRIVATE_FORUM, with_public_perms=True))
TopicFactory(with_post=True, forum=ForumFactory(kind=ForumKind.PUBLIC_FORUM, with_public_perms=True))

response = self.client.get(self.url)
Expand Down
13 changes: 7 additions & 6 deletions lacommunaute/forum_conversation/tests/tests_views_htmx.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ def test_create_post_with_blocked_domain_name(self):
assert blocked_post.block_reason == BlockedPostReason.BLOCKED_DOMAIN


# vincentporte : not to futur self, rewrite it in pytest style
class CertifiedPostViewTest(TestCase):
@classmethod
def setUpTestData(cls):
Expand Down Expand Up @@ -457,8 +458,8 @@ def test_certify_without_permission(self):
self.assertEqual(response.status_code, 403)

def test_certify_with_permission(self):
self.topic.forum.members_group.user_set.add(self.user)
self.topic.forum.members_group.save()
self.user.is_staff = True
self.user.save()
self.client.force_login(self.user)
response = self.client.post(self.url, data=self.form_data)

Expand All @@ -471,8 +472,8 @@ def test_certify_with_permission(self):
self.assertEqual(ForumReadTrack.objects.count(), 1)

def test_uncertify_with_permission(self):
self.topic.forum.members_group.user_set.add(self.user)
self.topic.forum.members_group.save()
self.user.is_staff = True
self.user.save()
CertifiedPost(topic=self.topic, post=self.topic.last_post, user=self.user).save()
self.client.force_login(self.user)
response = self.client.post(self.url, data=self.form_data)
Expand All @@ -482,8 +483,8 @@ def test_uncertify_with_permission(self):
self.assertEqual(ForumReadTrack.objects.count(), 1)

def test_rendered_content(self):
self.topic.forum.members_group.user_set.add(self.user)
self.topic.forum.members_group.save()
self.user.is_staff = True
self.user.save()
self.client.force_login(self.user)
response = self.client.post(self.url, data=self.form_data)

Expand Down
Loading
Loading