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

[Hotfix] Evite les notifications persistantes à la suppression d'un MP. #3663

Merged
merged 1 commit into from
Jun 13, 2016
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
8 changes: 8 additions & 0 deletions update.md
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,14 @@ Il faudra supprimer en SQL:

S'il y a une erreur pour `article_article_subcategory` et `DROP TABLE tutorial_tutorial_subcategory;` c'est que les tables ont déjà été supprimées précédement (ZEP-25).

Actions à faire pour mettre en prod la version 18.2
===================================================

Notifications
-------------

Lancez la commande `python manage.py delete_useless_notif` pour supprimer toutes les notifications inutiles.

---

**Notes auxquelles penser lors de l'édition de ce fichier (à laisser en bas) :**
Expand Down
21 changes: 21 additions & 0 deletions zds/notification/management/commands/delete_useless_notif.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# coding: utf-8
from django.contrib.contenttypes.models import ContentType
from django.core.management import BaseCommand

from zds.member.models import Profile
from zds.notification.models import Notification


class Command(BaseCommand):
help = 'Remove useless notifications for private topics.'

def handle(self, *args, **options):
for profile in Profile.objects.all():
self.stdout.write(u'Remove all useless notifications of {}...'.format(profile.user.username))
content_type = ContentType.objects.get(model='privatepost')
for notification in Notification.objects\
.filter(is_read=False, content_type=content_type, subscription__user=profile.user):
if notification.content_object is None:
notification.is_read = True
notification.save()
self.stdout.write(u'Notification #{} marked as read.'.format(notification.id))
11 changes: 11 additions & 0 deletions zds/notification/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,17 @@ def add_participant_topic_event(sender, **kwargs):
subscription.deactivate()


@receiver(pre_delete, sender=PrivateTopic)
def delete_private_topic_event(sender, instance, **kwargs):
"""
A private topic is deleted when there is nobody in this private topic.
"""
subscriptions = PrivateTopicAnswerSubscription.objects.get_subscriptions(content_object=instance, is_active=True)
for subscription in subscriptions:
subscription.mark_notification_read()
subscription.deactivate()


@receiver(pre_delete, sender=User)
def delete_notifications(sender, instance, **kwargs):
"""
Expand Down
15 changes: 9 additions & 6 deletions zds/notification/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,17 +451,20 @@ def test_remove_notifications_when_a_user_leave_private_topic(self):
self.assertIsNotNone(notifications.first())
self.assertEqual(topic.last_message, notifications.first().content_object)

self.assertIsNotNone(
PrivateTopicAnswerSubscription.objects.get_existing(self.user2, topic, is_active=True))
self.assertIsNotNone(PrivateTopicAnswerSubscription.objects.get_existing(self.user2, topic, is_active=True))

send_message_mp(self.user2, topic, "Test")
topic.participants.remove(self.user2)
topic.save()

notifications = Notification.objects.get_unread_notifications_of(self.user2)
self.assertEqual(0, len(notifications))
self.assertEqual(0, len(Notification.objects.get_unread_notifications_of(self.user2)))
self.assertIsNotNone(PrivateTopicAnswerSubscription.objects.get_existing(self.user2, topic, is_active=False))

self.assertEqual(1, len(Notification.objects.get_unread_notifications_of(self.user1)))

self.assertIsNotNone(
PrivateTopicAnswerSubscription.objects.get_existing(self.user2, topic, is_active=False))
response = self.client.post(reverse('mp-delete', args=[topic.pk, topic.slug]), follow=True)
self.assertEqual(200, response.status_code)
self.assertEqual(0, len(Notification.objects.get_unread_notifications_of(self.user1)))

def test_send_an_email_when_we_specify_it(self):
"""
Expand Down