Skip to content

Commit

Permalink
Add a way to execute unpublishing
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-s committed Oct 11, 2019
1 parent 5b09f22 commit 22e0b96
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 37 deletions.
102 changes: 66 additions & 36 deletions djangocms_moderation/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import PermissionDenied
from django.db import transaction
from django.http import Http404, HttpResponseRedirect
from django.http import Http404, HttpResponseRedirect, HttpResponseNotAllowed
from django.shortcuts import get_object_or_404, render
from django.template.loader import render_to_string
from django.urls import reverse
Expand Down Expand Up @@ -533,6 +533,7 @@ def resubmit_view(self, request):
moderation_requests=resubmitted_requests,
user=request.user,
rework=True,
workflow=collection.workflow
)

messages.success(
Expand All @@ -546,12 +547,58 @@ def resubmit_view(self, request):
)
return HttpResponseRedirect(redirect_url)

def _publish_flow(self, request, queryset):
"""Handles the published workflow"""
published_moderation_requests = []
for mr in queryset.all():
if mr.version_can_be_published():
if publish_version(mr.version, request.user):
published_moderation_requests.append(mr)
mr.update_status(
action=constants.ACTION_FINISHED, by_user=request.user
)

messages.success(
request,
ungettext(
"%(count)d request successfully published",
"%(count)d requests successfully published",
len(published_moderation_requests),
)
% {"count": len(published_moderation_requests)},
)
return published_moderation_requests

def _unpublish_flow(self, request, queryset):
unpublished_moderation_requests = []
for mr in queryset.all():
if mr.version_can_be_unpublished():
mr.version.unpublish(request.user)
unpublished_moderation_requests.append(mr)
mr.update_status(
action=constants.ACTION_FINISHED, by_user=request.user
)

messages.success(
request,
ungettext(
"%(count)d request successfully unpublished",
"%(count)d requests successfully unpublished",
len(unpublished_moderation_requests),
)
% {"count": len(unpublished_moderation_requests)},
)
return unpublished_moderation_requests

def published_view(self, request):
collection_id = request.GET.get('collection_id')
queryset = ModerationRequest.objects.filter(pk__in=request.GET.get('ids', '').split(','))
redirect_url = self._redirect_to_changeview_url(collection_id)

if request.method != 'POST':
if request.method not in ['POST', 'GET']:
return HttpResponseNotAllowed

if request.method == 'GET':
context = dict(
ids=request.GET.getlist("ids"),
back_url=redirect_url,
Expand All @@ -562,42 +609,25 @@ def published_view(self, request):
"admin/djangocms_moderation/moderationrequest/publish_confirmation.html",
context,
)
else:
try:
collection = ModerationCollection.objects.get(id=int(collection_id))
except (ValueError, ModerationCollection.DoesNotExist):
raise Http404

published_moderation_requests = []
for mr in queryset.all():
if mr.version_can_be_published():
if publish_version(mr.version, request.user):
published_moderation_requests.append(mr)
mr.update_status(
action=constants.ACTION_FINISHED, by_user=request.user
)
else:
# TODO provide some feedback back to the user?
pass

messages.success(
request,
ungettext(
"%(count)d request successfully published",
"%(count)d requests successfully published",
len(published_moderation_requests),
)
% {"count": len(published_moderation_requests)},
)
try:
collection = ModerationCollection.objects.get(id=int(collection_id))
except (ValueError, ModerationCollection.DoesNotExist):
raise Http404

post_bulk_actions(collection)
signals.published.send(
sender=self.model,
collection=collection,
moderator=collection.author,
published_moderation_requests=published_moderation_requests,
workflow=collection.workflow
)
if collection.workflow.is_unpublishing:
moderation_requests = self._unpublish_flow(request, queryset)
else:
moderation_requests = self._publish_flow(request, queryset)

post_bulk_actions(collection)
signals.published.send(
sender=self.model,
collection=collection,
moderator=collection.author,
moderation_requests=moderation_requests,
workflow=collection.workflow
)

return HttpResponseRedirect(redirect_url)

Expand Down
20 changes: 20 additions & 0 deletions djangocms_moderation/migrations/0017_workflow_is_unpublishing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.20 on 2019-10-11 09:23
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('djangocms_moderation', '0016_moderationrequesttreenode'),
]

operations = [
migrations.AddField(
model_name='workflow',
name='is_unpublishing',
field=models.BooleanField(default=False, verbose_name='unpublishing workflow'),
),
]
5 changes: 5 additions & 0 deletions djangocms_moderation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def get_users_queryset(self):
class Workflow(models.Model):
name = models.CharField(verbose_name=_("name"), max_length=120, unique=True)
is_default = models.BooleanField(verbose_name=_("is default"), default=False)
is_unpublishing = models.BooleanField(verbose_name=_("unpublishing workflow"), default=False)
identifier = models.CharField(
verbose_name=_("identifier"),
max_length=128,
Expand Down Expand Up @@ -304,6 +305,7 @@ def submit_for_review(self, by_user, to_user=None):
moderation_requests=list(self.moderation_requests.all()),
user=by_user,
rework=False,
workflow=collection.workflow
)

def is_cancellable(self, user):
Expand Down Expand Up @@ -471,6 +473,9 @@ def is_approved(self):
def version_can_be_published(self):
return self.is_approved() and self.version.can_be_published()

def version_can_be_unpublished(self):
return self.is_approved() and self.version.can_be_unpublished()

def is_rejected(self):
last_action = self.get_last_action()
return last_action and last_action.action == constants.ACTION_REJECTED
Expand Down
8 changes: 7 additions & 1 deletion djangocms_moderation/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
)

submitted_for_review = django.dispatch.Signal(
providing_args=["collection", "moderation_requests", "user", "rework"]
providing_args=[
"collection",
"moderation_requests",
"user",
"rework",
"workflow"
]
)

published = django.dispatch.Signal(
Expand Down

0 comments on commit 22e0b96

Please sign in to comment.