From ae1ddb7c1e7c5247daf5907f3af3e961ff4db630 Mon Sep 17 00:00:00 2001 From: fdambrine Date: Thu, 8 Oct 2020 13:20:32 +0200 Subject: [PATCH 1/4] =?UTF-8?q?Ajoute=20un=20test=20case=20pour=20les=20pr?= =?UTF-8?q?obl=C3=A8mes=20de=20duplication=20de=20clef?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zds/notification/tests/tests_tricky.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/zds/notification/tests/tests_tricky.py b/zds/notification/tests/tests_tricky.py index 93e9d7ebcd..20d1db37b8 100644 --- a/zds/notification/tests/tests_tricky.py +++ b/zds/notification/tests/tests_tricky.py @@ -440,3 +440,13 @@ def test_no_emails_for_those_who_have_other_things_in_that_place(self): send_message_mp(self.userOAuth2, topic, '', send_by_mail=True) self.assertEqual(1, len(mail.outbox)) + + def test_follow_by_email_after_normal(self): + forum = ForumFactory(category=ForumCategoryFactory()) + user = ProfileFactory() + subsription = NewTopicSubscription.objects.toggle_follow(forum, user) + self.assertIsNotNone(subsription) + new_subsription = NewTopicSubscription.objects.toggle_follow(forum, user, by_email=True) + self.assertIsNotNone(new_subsription) + self.assertEqual(subsription.pk, new_subsription.pk) + self.assertTrue(new_subsription.by_email) From acc27d6f6b6e011fdaa1f8f76d1b5d6665c9e60c Mon Sep 17 00:00:00 2001 From: fdambrine Date: Thu, 8 Oct 2020 14:30:23 +0200 Subject: [PATCH 2/4] =?UTF-8?q?Ajoute=20un=20test=20case=20pour=20les=20pr?= =?UTF-8?q?obl=C3=A8mes=20de=20duplication=20de=20clef?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zds/notification/tests/tests_tricky.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zds/notification/tests/tests_tricky.py b/zds/notification/tests/tests_tricky.py index 20d1db37b8..67b705891d 100644 --- a/zds/notification/tests/tests_tricky.py +++ b/zds/notification/tests/tests_tricky.py @@ -443,7 +443,7 @@ def test_no_emails_for_those_who_have_other_things_in_that_place(self): def test_follow_by_email_after_normal(self): forum = ForumFactory(category=ForumCategoryFactory()) - user = ProfileFactory() + user = ProfileFactory().user subsription = NewTopicSubscription.objects.toggle_follow(forum, user) self.assertIsNotNone(subsription) new_subsription = NewTopicSubscription.objects.toggle_follow(forum, user, by_email=True) From 84f1c73143fb56e05765865fdf9801d8923235ed Mon Sep 17 00:00:00 2001 From: artragis Date: Fri, 9 Oct 2020 18:01:29 +0200 Subject: [PATCH 3/4] Utilise la fonction get_or_create de django qui est plus robuste --- zds/notification/managers.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/zds/notification/managers.py b/zds/notification/managers.py index 1a1a322377..6c3e6506ac 100644 --- a/zds/notification/managers.py +++ b/zds/notification/managers.py @@ -77,16 +77,13 @@ def get_or_create_active(self, user, content_object): :return: subscription """ content_type = ContentType.objects.get_for_model(content_object) - try: - subscription = self.get( - object_id=content_object.pk, - content_type__pk=content_type.pk, - user=user) - if not subscription.is_active: - subscription.activate() - except ObjectDoesNotExist: - subscription = self.model(user=user, content_object=content_object) - subscription.save() + # boolean creation flag is useless for us. Use default method because of its robustness + subscription = self.get_or_create( + object_id=content_object.pk, + content_type__pk=content_type.pk, + user=user) + if not subscription.is_active: + subscription.activate() return subscription From e29f935a6d68691dcc2e3d363113d6f44a5e43f5 Mon Sep 17 00:00:00 2001 From: artragis Date: Fri, 9 Oct 2020 18:53:20 +0200 Subject: [PATCH 4/4] Utilise la fonction get_or_create de django qui est plus robuste --- zds/notification/managers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zds/notification/managers.py b/zds/notification/managers.py index 6c3e6506ac..48e560b684 100644 --- a/zds/notification/managers.py +++ b/zds/notification/managers.py @@ -80,7 +80,7 @@ def get_or_create_active(self, user, content_object): # boolean creation flag is useless for us. Use default method because of its robustness subscription = self.get_or_create( object_id=content_object.pk, - content_type__pk=content_type.pk, + content_type=content_type, user=user) if not subscription.is_active: subscription.activate()