mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-05-09 07:54:44 +00:00
Refactored create and edit views to use revision.publish()
This cuts out lots of duplicated code
This commit is contained in:
parent
f5c57607ed
commit
5d30efe2c9
3 changed files with 29 additions and 59 deletions
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue