Skip to content

Commit

Permalink
Supprime notifications forum inaccessible (#6196)
Browse files Browse the repository at this point in the history
* Supprime code inutile

Redondant avec notification/receivers.py remove_group_subscription_on_quitting_groups

* Complète les tests

* Supprime les notifications caduques qui trainent

* Ajout d'un forum réservé au staff dans les fixtures
  • Loading branch information
ChantyTaguan authored Dec 28, 2021
1 parent 44a96f4 commit 915c7d1
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 13 deletions.
8 changes: 8 additions & 0 deletions fixtures/forums.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,11 @@
subtitle: Discutez de tout !
category: 4
slug: discussions-generales
- model: forum.Forum
pk: 13
fields:
title: Staff only
subtitle: Réservé à l'équipe !
category: 4
slug: staff-only
groups: [1]
9 changes: 0 additions & 9 deletions zds/member/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1405,16 +1405,7 @@ def settings_promote(request, user_pk):
request,
_("{0} n'appartient maintenant plus au groupe {1}.").format(user.username, group.name),
)
topics_followed = TopicAnswerSubscription.objects.get_objects_followed_by(user)
for topic in topics_followed:
if isinstance(topic, Topic) and group in topic.forum.groups.all():
TopicAnswerSubscription.objects.toggle_follow(topic, user)
else:
for group in usergroups:
topics_followed = TopicAnswerSubscription.objects.get_objects_followed_by(user)
for topic in topics_followed:
if isinstance(topic, Topic) and group in topic.forum.groups.all():
TopicAnswerSubscription.objects.toggle_follow(topic, user)
user.groups.clear()
messages.warning(request, _("{0} n'appartient (plus ?) à aucun groupe.").format(user.username))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django.db import migrations

from zds.notification.models import NewTopicSubscription
from zds.forum.models import Topic, Forum


def cleanup(apps, *_):
for forum in Forum.objects.filter(groups__isnull=False).all():
for subscription in NewTopicSubscription.objects.get_subscriptions(forum):
if not forum.can_read(subscription.user):
subscription.is_active = False
if subscription.last_notification:
subscription.last_notification.is_read = True
subscription.last_notification.save()
subscription.save()


class Migration(migrations.Migration):

dependencies = [
("notification", "0016_auto_20190114_1301"),
]

operations = [
migrations.RunPython(cleanup),
]
102 changes: 98 additions & 4 deletions zds/notification/tests/tests_tricky.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
from django.core import mail
from django.test import TestCase
from django.test.utils import override_settings
from django.contrib.contenttypes.models import ContentType

from zds.forum.factories import ForumCategoryFactory, ForumFactory
from zds.forum.factories import ForumCategoryFactory, ForumFactory, TopicFactory, PostFactory
from zds.forum.models import Topic
from zds.gallery.factories import UserGalleryFactory
from zds.member.factories import StaffProfileFactory, ProfileFactory
from zds.notification.models import (
NewTopicSubscription,
TopicAnswerSubscription,
Notification,
NewPublicationSubscription,
ContentReactionAnswerSubscription,
Expand Down Expand Up @@ -287,7 +289,7 @@ def test_no_dead_ping_notif_on_moving_to_private_forum(self):
self.assertEqual(subscription.last_notification, Notification.objects.filter(sender=self.user2).first())
self.assertTrue(subscription.last_notification.is_read, "As forum is not reachable, notification is read")

def test_no_more_notif_on_losing_all_groups(self):
def test_no_more_new_topic_notif_on_losing_all_groups(self):
NewTopicSubscription.objects.get_or_create_active(self.to_be_changed_staff, self.forum12)
self.client.force_login(self.staff)
self.client.post(
Expand All @@ -302,13 +304,18 @@ def test_no_more_notif_on_losing_all_groups(self):
)
subscription = NewTopicSubscription.objects.get_existing(self.to_be_changed_staff, self.forum12, True)
self.assertIsNotNone(subscription, "There must be an active subscription for now")
self.assertIsNotNone(subscription.last_notification, "There must be a notification.")
self.assertFalse(subscription.last_notification.is_read, "The notification has not been read yet")

self.to_be_changed_staff.groups.clear()
self.to_be_changed_staff.save()

subscription = NewTopicSubscription.objects.get_existing(self.to_be_changed_staff, self.forum12, False)
self.assertIsNotNone(subscription, "There must be an active subscription for now")
self.assertIsNotNone(subscription, "The subscription should now be inactive")
self.assertFalse(subscription.is_active)
self.assertTrue(subscription.last_notification.is_read, "As forum is not reachable, notification is read")

def test_no_more_notif_on_losing_one_group(self):
def test_no_more_new_topic_notif_on_losing_one_group(self):
NewTopicSubscription.objects.get_or_create_active(self.to_be_changed_staff, self.forum12)
self.client.force_login(self.staff)
self.client.post(
Expand All @@ -323,11 +330,98 @@ def test_no_more_notif_on_losing_one_group(self):
)
subscription = NewTopicSubscription.objects.get_existing(self.to_be_changed_staff, self.forum12, True)
self.assertIsNotNone(subscription, "There must be an active subscription for now")
self.assertIsNotNone(subscription.last_notification, "There must be a notification.")
self.assertFalse(subscription.last_notification.is_read, "The notification has not been read yet")

self.to_be_changed_staff.groups.remove(list(self.to_be_changed_staff.groups.all())[0])
self.to_be_changed_staff.save()

subscription = NewTopicSubscription.objects.get_existing(self.to_be_changed_staff, self.forum12, False)
self.assertIsNotNone(subscription, "There must be an inactive subscription now")
self.assertFalse(subscription.is_active)
self.assertTrue(subscription.last_notification.is_read, "As forum is not reachable, notification is read")

def test_no_more_topic_answer_notif_on_losing_all_groups(self):
self.client.force_login(self.to_be_changed_staff)
self.client.post(
reverse("topic-new") + f"?forum={self.forum12.pk}",
{
"title": "Super sujet",
"subtitle": "Pour tester les notifs",
"text": "En tout cas l'un abonnement",
"tags": "",
},
follow=False,
)
topic = Topic.objects.filter(title="Super sujet").first()

self.client.force_login(self.staff)
self.client.post(
reverse("post-new") + f"?sujet={topic.pk}",
{
"last_post": topic.last_message.pk,
"text": "C'est tout simplement l'histoire de la ville de Paris que je voudrais vous conter ",
},
follow=False,
)

subscription = TopicAnswerSubscription.objects.get_existing(
content_object=topic, user=self.to_be_changed_staff, is_active=True
)
self.assertIsNotNone(subscription, "There must be an active subscription for now")
self.assertIsNotNone(subscription.last_notification, "There must be a notification.")
self.assertFalse(subscription.last_notification.is_read, "The notification has not been read yet")

self.to_be_changed_staff.groups.clear()
self.to_be_changed_staff.save()

subscription = TopicAnswerSubscription.objects.get_existing(
content_object=topic, user=self.to_be_changed_staff, is_active=False
)
self.assertIsNotNone(subscription, "The subscription must now be inactive")
self.assertFalse(subscription.is_active)
self.assertTrue(subscription.last_notification.is_read, "As forum is not reachable, notification is read")

def test_no_more_topic_answer_notif_on_losing_one_group(self):
self.client.force_login(self.to_be_changed_staff)
self.client.post(
reverse("topic-new") + f"?forum={self.forum12.pk}",
{
"title": "Super sujet",
"subtitle": "Pour tester les notifs",
"text": "En tout cas l'un abonnement",
"tags": "",
},
follow=False,
)
topic = Topic.objects.filter(title="Super sujet").first()

self.client.force_login(self.staff)
self.client.post(
reverse("post-new") + f"?sujet={topic.pk}",
{
"last_post": topic.last_message.pk,
"text": "C'est tout simplement l'histoire de la ville de Paris que je voudrais vous conter ",
},
follow=False,
)

subscription = TopicAnswerSubscription.objects.get_existing(
content_object=topic, user=self.to_be_changed_staff, is_active=True
)
self.assertIsNotNone(subscription, "There must be an active subscription for now")
self.assertIsNotNone(subscription.last_notification, "There must be a notification.")
self.assertFalse(subscription.last_notification.is_read, "The notification has not been read yet")

self.to_be_changed_staff.groups.remove(list(self.to_be_changed_staff.groups.all())[0])
self.to_be_changed_staff.save()

subscription = TopicAnswerSubscription.objects.get_existing(
content_object=topic, user=self.to_be_changed_staff, is_active=False
)
self.assertIsNotNone(subscription, "The subscription must now be inactive")
self.assertFalse(subscription.is_active)
self.assertTrue(subscription.last_notification.is_read, "As forum is not reachable, notification is read")


@override_for_contents()
Expand Down

0 comments on commit 915c7d1

Please sign in to comment.