1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/events/queue_for_review.py Sun Oct 09 20:37:21 2011 +0200
1.3 @@ -0,0 +1,74 @@
1.4 +# -*- coding: iso-8859-1 -*-
1.5 +"""
1.6 + MoinMoin - Queue changes for review
1.7 +
1.8 + Queue changed pages edited by unprivileged users for review by the
1.9 + ApproveChanges action.
1.10 +
1.11 + @copyright: 2011 Paul Boddie <paul@boddie.org.uk>
1.12 + @license: GNU GPL, see COPYING for details.
1.13 +"""
1.14 +
1.15 +from MoinMoin.PageEditor import PageEditor
1.16 +from MoinMoin.events import PagePreSaveEvent, Abort
1.17 +import re
1.18 +
1.19 +def match_queue_pages(pagename, queued_changes_area):
1.20 +
1.21 + "Return a filter function which matches subpages of 'pagename'."
1.22 +
1.23 + return re.compile(ur"^%s/%s/.*$" % (re.escape(pagename), re.escape(queued_changes_area)), re.UNICODE).match
1.24 +
1.25 +def handle_presave(event):
1.26 + request = event.request
1.27 + _ = request.getText
1.28 +
1.29 + approved_editors_group = getattr(request.cfg, "approved_editors_group", "ApprovedGroup")
1.30 + queued_changes_area = getattr(request.cfg, "queued_changes_area", "ApprovalQueue")
1.31 +
1.32 + pagename = event.page_editor.page_name
1.33 + parts = pagename.split("/")
1.34 +
1.35 + # Saving into queues has to be permitted or the mechanism will keep trying
1.36 + # to save into a queue of the specified page.
1.37 +
1.38 + if len(parts) > 2 and parts[-2] == queued_changes_area:
1.39 + return None
1.40 +
1.41 + # For normal pages, the user has to be approved. Otherwise, the page will be
1.42 + # saved into a queue.
1.43 +
1.44 + elif not request.user.valid or (
1.45 + not request.dicts.has_member(approved_editors_group, request.user.name) and \
1.46 + not request.user.isSuperUser()):
1.47 +
1.48 + # Get a queue number by listing the subpages of the queue area and
1.49 + # finding the highest queue number, then adding one to it.
1.50 +
1.51 + queue_number = -1
1.52 +
1.53 + for name in request.rootpage.getPageList(exists=1, filter=match_queue_pages(pagename, queued_changes_area)):
1.54 + number = name.split("/")[-1]
1.55 + if number.isdigit():
1.56 + queue_number = max(queue_number, int(number))
1.57 +
1.58 + queue_number += 1
1.59 +
1.60 + # Save the page in the queue.
1.61 +
1.62 + new_page = PageEditor(request, "%s/%s/%s" % (pagename, queued_changes_area, queue_number))
1.63 +
1.64 + try:
1.65 + new_page.saveText(event.new_text, 0)
1.66 + except PageEditor.Unchanged:
1.67 + pass
1.68 +
1.69 + # Abort the page save operation.
1.70 +
1.71 + return Abort(_("Your changes have been queued for approval."))
1.72 +
1.73 +def handle(event):
1.74 + if isinstance(event, PagePreSaveEvent):
1.75 + return handle_presave(event)
1.76 +
1.77 +# vim: tabstop=4 expandtab shiftwidth=4