From efc273b49890e8036546fe4faa11f05a0084cf86 Mon Sep 17 00:00:00 2001 From: Jonathan Sundqvist Date: Sat, 12 Oct 2019 10:37:17 +0200 Subject: [PATCH] Create signal for when a collection is published (#159) --- djangocms_moderation/admin.py | 15 +++++++++++---- djangocms_moderation/signals.py | 9 +++++++++ tests/test_admin_actions.py | 23 +++++++++++++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/djangocms_moderation/admin.py b/djangocms_moderation/admin.py index d962cb95..e4acc258 100644 --- a/djangocms_moderation/admin.py +++ b/djangocms_moderation/admin.py @@ -586,12 +586,12 @@ def published_view(self, request): context, ) else: - num_published_requests = 0 + published_moderation_requests = [] for node in treenodes.all(): mr = node.moderation_request if mr.version_can_be_published(): if publish_version(mr.version, request.user): - num_published_requests += 1 + published_moderation_requests.append(mr) mr.update_status( action=constants.ACTION_FINISHED, by_user=request.user ) @@ -604,12 +604,19 @@ def published_view(self, request): ungettext( "%(count)d request successfully published", "%(count)d requests successfully published", - num_published_requests, + len(published_moderation_requests), ) - % {"count": num_published_requests}, + % {"count": len(published_moderation_requests)}, ) post_bulk_actions(collection) + signals.published.send( + sender=self.model, + collection=collection, + moderator=collection.author, + moderation_requests=published_moderation_requests, + workflow=collection.workflow + ) return HttpResponseRedirect(redirect_url) diff --git a/djangocms_moderation/signals.py b/djangocms_moderation/signals.py index e2fd26df..46c28975 100644 --- a/djangocms_moderation/signals.py +++ b/djangocms_moderation/signals.py @@ -8,3 +8,12 @@ submitted_for_review = django.dispatch.Signal( providing_args=["collection", "moderation_requests", "user", "rework"] ) + +published = django.dispatch.Signal( + providing_args=[ + "collection", + "moderator", + "moderation_requests", + "workflow" + ] +) diff --git a/tests/test_admin_actions.py b/tests/test_admin_actions.py index 7855cd62..e45475ce 100644 --- a/tests/test_admin_actions.py +++ b/tests/test_admin_actions.py @@ -7,6 +7,7 @@ from django.urls import reverse from cms.test_utils.testcases import CMSTestCase +from cms.test_utils.util.context_managers import signal_tester from djangocms_versioning.constants import DRAFT, PUBLISHED from djangocms_versioning.models import Version @@ -19,6 +20,7 @@ ModerationRequestTreeNode, Role, ) +from djangocms_moderation.signals import published from .utils import factories @@ -624,6 +626,27 @@ def test_publish_selected_publishes_approved_request(self, messages_mock): self.collection.refresh_from_db() self.assertEqual(self.collection.status, constants.IN_REVIEW) + def test_signal_when_published(self): + """ + A signal should be sent so further action can be taken when a moderation + collection is being published. + """ + data = get_url_data(self, "publish_selected") + response = self.client.post(self.url, data) + + with signal_tester(published) as signal: + # And now go to the view the action redirects to + self.client.post(response.url) + args, kwargs = signal.calls[0] + published_mr = kwargs['moderation_requests'] + self.assertEquals(signal.call_count, 1) + self.assertEquals(kwargs['sender'], ModerationRequest) + self.assertEquals(kwargs['collection'], self.collection) + self.assertEquals(kwargs['moderator'], self.collection.author) + self.assertEquals(len(published_mr), 1) + self.assertEquals(published_mr[0], self.moderation_request1) + self.assertEquals(kwargs['workflow'], self.collection.workflow) + @unittest.skip("Skip until collection status bugs fixed") @mock.patch("django.contrib.messages.success") def test_publish_selected_sets_collection_to_archived_if_all_requests_published(self, messages_mock):