Skip to content

Instantly share code, notes, and snippets.

@balazs-endresz
Created September 6, 2016 14:58
Show Gist options
  • Save balazs-endresz/b50ccab92666847f3eb025285f25ed3f to your computer and use it in GitHub Desktop.
Save balazs-endresz/b50ccab92666847f3eb025285f25ed3f to your computer and use it in GitHub Desktop.
Decorator to help updating the appropriate wagtail page and/or revision objects
from functools import wraps
def update_page_and_or_revsion(func):
@wraps(func)
def func_wrapper(page):
if page.live:
if page.has_unpublished_changes:
# update live page without creating new revsions
func(page)
page.save()
# create new revision based the latest unpublsihed one but don't publish it
last_revision_as_page = page.revisions.latest('created_at').as_page_object()
func(last_revision_as_page)
last_revision_as_page.save_revision()
else:
# create new revsion and publish it
func(page)
page.save_revision().publish()
else:
# create new revsion and don't publish anything
func(page)
page.save_revision()
return func_wrapper
# usage:
@update_page_and_or_revsion
def update(page):
# page here is either a page or a revision as a page object
page.field = 'value'
update(page)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment