diff --git a/zds/featured/receivers.py b/zds/featured/receivers.py index 934c3c5548..d10fe8f71c 100644 --- a/zds/featured/receivers.py +++ b/zds/featured/receivers.py @@ -4,12 +4,16 @@ from zds.featured.models import FeaturedRequested from zds.forum.models import Topic -from zds.tutorialv2.models.database import PublishableContent +from zds.tutorialv2.models.database import PublishableContent, PublishedContent @receiver(pre_delete, sender=PublishableContent) +@receiver(pre_delete, sender=PublishedContent) @receiver(pre_delete, sender=Topic) def remove_requested_featured_at_content_object_deletion(sender, instance, **kwargs): + if isinstance(instance, PublishedContent): + sender = PublishableContent + instance = instance.content try: FeaturedRequested.objects.get( content_type=ContentType.objects.get_for_model(sender), object_id=instance.pk diff --git a/zds/featured/tests/tests.py b/zds/featured/tests/tests.py index dda3a694cf..4d75036a0f 100644 --- a/zds/featured/tests/tests.py +++ b/zds/featured/tests/tests.py @@ -9,6 +9,7 @@ from zds.featured.models import FeaturedResource, FeaturedMessage, FeaturedRequested from zds.forum.tests.factories import ForumCategoryFactory, ForumFactory, TopicFactory from zds.gallery.tests.factories import GalleryFactory, ImageFactory +from zds.tutorialv2.publication_utils import unpublish_content from zds.tutorialv2.tests.factories import PublishedContentFactory from zds.tutorialv2.tests import TutorialTestMixin, override_for_contents @@ -488,10 +489,14 @@ def test_success_list_with_deleted_content(self): # request for the topic and the content to be featured FeaturedRequested.objects.toogle_request(topic, author) FeaturedRequested.objects.toogle_request(tutorial, author) + count = FeaturedRequested.objects.count() - # delete both this topic and this content + # delete the topic and unpublish the content topic.delete() - tutorial.delete() + unpublish_content(tutorial) + + # check that the FeaturedRequested objects have been deleted + self.assertEqual(FeaturedRequested.objects.count(), count - 2) # check that the page listing the requests still works staff = StaffProfileFactory()