From 6839a7474ab32873bfffb2b48237d500538ca7c7 Mon Sep 17 00:00:00 2001 From: Serafeim Papastefanos Date: Mon, 21 Apr 2014 21:48:23 +0300 Subject: [PATCH] Add management command for scheduled pages The publish_scheduled_pages management command does three actions: * Gets live pages which have an expiry_datetime that has passed and set expired = True and live = False * Gets all revisions on the moderation queue which have an expiry_datetime that has passed and remove them from the moderation queue * Gets all revisions that have an approved_go_live_datetime that has passed. For each one of them the publish() method of the revision is called which will perform the required actions for making live this version of the page. Finally, a dryrun parameter has been added to the management command. If this parameter is used then the pages that pass the tests for each of the above lists will be printed. --- .../commands/publish_scheduled_pages.py | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 wagtail/wagtailcore/management/commands/publish_scheduled_pages.py diff --git a/wagtail/wagtailcore/management/commands/publish_scheduled_pages.py b/wagtail/wagtailcore/management/commands/publish_scheduled_pages.py new file mode 100644 index 000000000..0abf3c744 --- /dev/null +++ b/wagtail/wagtailcore/management/commands/publish_scheduled_pages.py @@ -0,0 +1,109 @@ +import datetime +import json +from optparse import make_option + +from django.core.management.base import BaseCommand +from django.utils import dateparse, timezone +from wagtail.wagtailcore.models import Page, PageRevision + + +def revision_date_expired(r): + expiry_str = json.loads(r.content_json).get('expiry_datetime') + if not expiry_str: + return False + expiry_datetime = dateparse.parse_datetime(expiry_str) + if expiry_datetime < timezone.now(): + return True + else: + return False + + +class Command(BaseCommand): + option_list = BaseCommand.option_list + ( + make_option( + '--dryrun', + action='store_true', + dest='dryrun', + default=False, + help='Dry run -- don\'t change anything.'), + ) + + def handle(self, *args, **options): + dryrun = False + if options['dryrun']: + print "Will do a dry run." + dryrun = True + + # 1. get all expired pages with live = True + expired_pages = Page.objects.filter( + live=True, + expiry_datetime__lt=timezone.now() + ) + if dryrun: + if expired_pages: + print "Expired pages to be deactivated:" + print "Expiry datetime\t\tSlug\t\tName" + print "---------------\t\t----\t\t----" + for ep in expired_pages: + print "{0}\t{1}\t{2}".format( + ep.expiry_datetime.strftime("%Y-%m-%d %H:%M"), + ep.slug, + ep.title + ) + else: + print "No expired pages to be deactivated found." + else: + expired_pages.update(expired=True, live=False) + + # 2. get all page revisions for moderation that have been expired + expired_revs = [ + r for r in PageRevision.objects.filter( + submitted_for_moderation=True + ) if revision_date_expired(r) + ] + if dryrun: + print "---------------------------------" + if expired_revs: + print "Expired revisions to be dropped from moderation queue:" + print "Expiry datetime\t\tSlug\t\tName" + print "---------------\t\t----\t\t----" + for er in expired_revs: + rev_data = json.loads(er.content_json) + print "{0}\t{1}\t{2}".format( + dateparse.parse_datetime( + rev_data.get('expiry_datetime') + ).strftime("%Y-%m-%d %H:%M"), + rev_data.get('slug'), + rev_data.get('title') + ) + else: + print "No expired revision to be dropped from moderation." + else: + for er in expired_revs: + er.submitted_for_moderation = False + er.save() + + # 3. get all revisions that need to be published + revs_for_publishing = PageRevision.objects.filter( + approved_go_live_datetime__lt=timezone.now() + ) + if dryrun: + print "---------------------------------" + if revs_for_publishing: + print "Revisions to be published:" + print "Go live datetime\t\tSlug\t\tName" + print "---------------\t\t\t----\t\t----" + for rp in revs_for_publishing: + rev_data = json.loads(rp.content_json) + print "{0}\t\t{1}\t{2}".format( + rp.approved_go_live_datetime.strftime("%Y-%m-%d %H:%M"), + rev_data.get('slug'), + rev_data.get('title') + ) + else: + print "No pages to go live." + else: + for rp in revs_for_publishing: + # just run publish for the revision -- since the approved go + # live datetime is before now it will make the page live + rp.publish()