Skip to content

Commit

Permalink
Supprime les demandes de mise en avant lros de la suppression des con…
Browse files Browse the repository at this point in the history
…tenus (#6626)

* Reproduit un bug liée aux demandes de mise en avant
* Corrige un bug lié aux demandes de mise en avant
* Supprime les demandes de mise en avant lors de la dépublication des contenus
  • Loading branch information
Situphen authored Jul 21, 2024
1 parent ab676f6 commit b7e0f4c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
8 changes: 8 additions & 0 deletions zds/featured/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.apps import AppConfig


class FeaturedConfig(AppConfig):
name = "zds.featured"

def ready(self):
from . import receivers # noqa
22 changes: 22 additions & 0 deletions zds/featured/receivers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django.contrib.contenttypes.models import ContentType
from django.db.models.signals import pre_delete
from django.dispatch import receiver

from zds.featured.models import FeaturedRequested
from zds.forum.models import Topic
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
).delete()
except FeaturedRequested.DoesNotExist:
pass
30 changes: 30 additions & 0 deletions zds/featured/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -474,6 +475,35 @@ def test_filters(self):

self.assertEqual(len(response.context["featured_request_list"]), 1) # it is back!

def test_success_list_with_deleted_content(self):
# create a topic
author = ProfileFactory().user
category = ForumCategoryFactory(position=1)
forum = ForumFactory(category=category, position_in_category=1)
topic = TopicFactory(forum=forum, author=author)

# create a published content
tutorial = PublishedContentFactory(author_list=[author])
tutorial.save()

# 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 the topic and unpublish the content
topic.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()
self.client.force_login(staff.user)
response = self.client.get(reverse("featured:resource-requests"))
self.assertEqual(200, response.status_code)


class FeaturedRequestUpdateViewTest(TestCase):
def test_update(self):
Expand Down

0 comments on commit b7e0f4c

Please sign in to comment.