From 5d30efe2c97d30bf386cd2409fd36ca61f08c88a Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Fri, 22 Aug 2014 14:56:54 +0100 Subject: [PATCH] Refactored create and edit views to use revision.publish() This cuts out lots of duplicated code --- wagtail/wagtailadmin/views/pages.py | 81 ++++++------------- .../commands/publish_scheduled_pages.py | 3 - wagtail/wagtailcore/models.py | 4 + 3 files changed, 29 insertions(+), 59 deletions(-) diff --git a/wagtail/wagtailadmin/views/pages.py b/wagtail/wagtailadmin/views/pages.py index 3de6f7cb5..001df53ee 100644 --- a/wagtail/wagtailadmin/views/pages.py +++ b/wagtail/wagtailadmin/views/pages.py @@ -20,7 +20,7 @@ from wagtail.wagtailadmin import tasks, signals from wagtail.wagtailcore import hooks from wagtail.wagtailcore.models import Page, PageRevision, get_navigation_menu_items -from wagtail.wagtailcore.signals import page_published, page_unpublished +from wagtail.wagtailcore.signals import page_unpublished @permission_required('wagtailadmin.access_admin') @@ -183,39 +183,31 @@ def create(request, content_type_app_name, content_type_model_name, parent_page_ form.clean = clean if form.is_valid(): - page = form.save(commit=False) # don't save yet, as we need treebeard to assign tree params + page = form.save(commit=False) is_publishing = bool(request.POST.get('action-publish')) and parent_page_perms.can_publish_subpage() is_submitting = bool(request.POST.get('action-submit')) - go_live_at = form.cleaned_data.get('go_live_at') - future_go_live = go_live_at and go_live_at > timezone.now() - approved_go_live_at = None - if is_publishing: - page.has_unpublished_changes = False - page.expired = False - if future_go_live: - page.live = False - # Set approved_go_live_at only if is publishing - # and the future_go_live is actually in future - approved_go_live_at = go_live_at - else: - page.live = True - else: + # Set live to False and has_unpublished_changes to True if we are not publishing + if not is_publishing: page.live = False page.has_unpublished_changes = True - parent_page.add_child(instance=page) # assign tree parameters - will cause page to be saved + # Save page + parent_page.add_child(instance=page) - # Pass approved_go_live_at to save_revision - page.save_revision( + # Save revision + revision = page.save_revision( user=request.user, submitted_for_moderation=is_submitting, - approved_go_live_at=approved_go_live_at ) + # Publish + if is_publishing: + revision.publish() + + # Notifications if is_publishing: - page_published.send(sender=page_class, instance=page) messages.success(request, _("Page '{0}' published.").format(page.title)) elif is_submitting: messages.success(request, _("Page '{0}' submitted for moderation.").format(page.title)) @@ -300,54 +292,32 @@ def edit(request, page_id): form.clean = clean if form.is_valid(): + page = form.save(commit=False) + is_publishing = bool(request.POST.get('action-publish')) and page_perms.can_publish() is_submitting = bool(request.POST.get('action-submit')) - go_live_at = form.cleaned_data.get('go_live_at') - future_go_live = go_live_at and go_live_at > timezone.now() - approved_go_live_at = None + # Save revision + revision = page.save_revision( + user=request.user, + submitted_for_moderation=is_submitting, + ) + + # Publish if is_publishing: - page.has_unpublished_changes = False - page.expired = False - if future_go_live: - page.live = False - # Set approved_go_live_at only if publishing - approved_go_live_at = go_live_at - else: - page.live = True - - # We need save the page this way to workaround a bug - # in django-modelcluster causing m2m fields to not - # be committed to the database. See github issue #192 - form.save(commit=False) - page.save() - - # Clear approved_go_live_at for older revisions - page.revisions.update( - submitted_for_moderation=False, - approved_go_live_at=None, - ) + revision.publish() else: - # not publishing the page + # Set has_unpublished_changes flag if page.live: # To avoid overwriting the live version, we only save the page # to the revisions table - form.save(commit=False) Page.objects.filter(id=page.id).update(has_unpublished_changes=True) else: page.has_unpublished_changes = True - form.save(commit=False) page.save() - - page.save_revision( - user=request.user, - submitted_for_moderation=is_submitting, - approved_go_live_at=approved_go_live_at - ) - + # Notifications if is_publishing: - page_published.send(sender=page.__class__, instance=page) messages.success(request, _("Page '{0}' published.").format(page.title)) elif is_submitting: messages.success(request, _("Page '{0}' submitted for moderation.").format(page.title)) @@ -787,7 +757,6 @@ def approve_moderation(request, revision_id): if request.POST: revision.publish() - page_published.send(sender=revision.page.__class__, instance=revision.page.specific) messages.success(request, _("Page '{0}' published.").format(revision.page.title)) tasks.send_notification.delay(revision.id, 'approved', request.user.id) diff --git a/wagtail/wagtailcore/management/commands/publish_scheduled_pages.py b/wagtail/wagtailcore/management/commands/publish_scheduled_pages.py index d1ec5eecb..51f95fff2 100644 --- a/wagtail/wagtailcore/management/commands/publish_scheduled_pages.py +++ b/wagtail/wagtailcore/management/commands/publish_scheduled_pages.py @@ -118,6 +118,3 @@ class Command(BaseCommand): # just run publish for the revision -- since the approved go # live datetime is before now it will make the page live rp.publish() - - # Fire page_published signal - page_published.send(sender=rp.page.specific_class, instance=rp.page.specific) diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index a237e16e6..baa13d75e 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -31,6 +31,7 @@ from wagtail.utils.deprecation import RemovedInWagtail06Warning from wagtail.wagtailcore.utils import camelcase_to_underscore from wagtail.wagtailcore.query import PageQuerySet from wagtail.wagtailcore.url_routing import RouteResult +from wagtail.wagtailcore.signals import page_published from wagtail.wagtailsearch import indexed from wagtail.wagtailsearch.backends import get_search_backend @@ -952,6 +953,9 @@ class PageRevision(models.Model): self.submitted_for_moderation = False page.revisions.update(submitted_for_moderation=False) + if page.live: + page_published.send(sender=page.specific_class, instance=page.specific) + def __str__(self): return '"' + unicode(self.page) + '" at ' + unicode(self.created_at)