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 14, 2019
1 parent 3971f3f commit 8a35cc6
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 51 deletions.
98 changes: 63 additions & 35 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 All @@ -27,7 +27,6 @@
delete_selected,
post_bulk_actions,
publish_selected,
publish_version,
reject_selected,
resubmit_selected,
)
Expand Down Expand Up @@ -552,6 +551,7 @@ def resubmit_view(self, request):
moderation_requests=resubmitted_requests,
user=request.user,
rework=True,
workflow=collection.workflow
)

messages.success(
Expand All @@ -565,6 +565,49 @@ 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():
mr.version.publish(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')
treenodes = self._get_selected_tree_nodes(request)
Expand All @@ -578,46 +621,30 @@ def published_view(self, request):
if request.user != collection.author:
raise PermissionDenied

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

if request.method == 'GET':
context = self._custom_view_context(request)
return render(
request,
"admin/djangocms_moderation/moderationrequest/publish_confirmation.html",
context,
)
else:
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):
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)},
)

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

if collection.workflow.is_unpublishing:
moderation_requests = self._unpublish_flow(request, treenodes)
else:
moderation_requests = self._publish_flow(request, treenodes)

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)

def rework_view(self, request):
Expand Down Expand Up @@ -955,6 +982,7 @@ class WorkflowAdmin(admin.ModelAdmin):
fields = [
"name",
"is_default",
"is_unpublishing",
"identifier",
"requires_compliance_number",
"compliance_number_backend",
Expand Down
19 changes: 4 additions & 15 deletions djangocms_moderation/admin_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

from djangocms_versioning.models import Version

from django_fsm import TransitionNotAllowed
from djangocms_moderation import constants

from .utils import get_admin_url
Expand Down Expand Up @@ -115,9 +114,9 @@ def convert_queryset_to_version_queryset(queryset):
m
for m in reversed(model.mro())
if (
isinstance(m, tuple(model_bases))
and m not in model_bases
and not m._meta.abstract
isinstance(m, tuple(model_bases)) and
m not in model_bases and not
m._meta.abstract
)
)

Expand Down Expand Up @@ -153,20 +152,10 @@ def add_items_to_collection(modeladmin, request, queryset):
return HttpResponseRedirect(request.META.get("HTTP_REFERER"))


add_items_to_collection.short_description = _(
"Add to moderation collection"
) # noqa: E305
add_items_to_collection.short_description = _("Add to moderation collection")


def post_bulk_actions(collection):
if collection.should_be_archived():
collection.status = constants.ARCHIVED
collection.save(update_fields=["status"])


def publish_version(version, user):
try:
version.publish(user)
except TransitionNotAllowed:
return False
return True
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'),
),
]
9 changes: 9 additions & 0 deletions djangocms_moderation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ 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,
help_text=_("This workflow will unpublish assets at the end")
)
identifier = models.CharField(
verbose_name=_("identifier"),
max_length=128,
Expand Down Expand Up @@ -295,6 +300,7 @@ def submit_for_review(self, by_user, to_user=None):
moderation_requests=list(self.moderation_requests.all()),
user=by_user,
rework=False,
workflow=self.workflow
)

def is_cancellable(self, user):
Expand Down Expand Up @@ -462,6 +468,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 8a35cc6

Please sign in to comment.