Skip to content

Commit

Permalink
Create signal for when a collection is published (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-s authored and Aiky30 committed Oct 12, 2019
1 parent 06e7481 commit efc273b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
15 changes: 11 additions & 4 deletions djangocms_moderation/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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)

Expand Down
9 changes: 9 additions & 0 deletions djangocms_moderation/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
)
23 changes: 23 additions & 0 deletions tests/test_admin_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,6 +20,7 @@
ModerationRequestTreeNode,
Role,
)
from djangocms_moderation.signals import published

from .utils import factories

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit efc273b

Please sign in to comment.