paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - Queue changes for review |
paul@0 | 4 | |
paul@0 | 5 | Queue changed pages edited by unprivileged users for review by the |
paul@0 | 6 | ApproveChanges action. |
paul@0 | 7 | |
paul@0 | 8 | @copyright: 2011 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 9 | @license: GNU GPL, see COPYING for details. |
paul@0 | 10 | """ |
paul@0 | 11 | |
paul@0 | 12 | from MoinMoin.PageEditor import PageEditor |
paul@0 | 13 | from MoinMoin.events import PagePreSaveEvent, Abort |
paul@0 | 14 | import re |
paul@0 | 15 | |
paul@0 | 16 | def match_queue_pages(pagename, queued_changes_area): |
paul@0 | 17 | |
paul@0 | 18 | "Return a filter function which matches subpages of 'pagename'." |
paul@0 | 19 | |
paul@0 | 20 | return re.compile(ur"^%s/%s/.*$" % (re.escape(pagename), re.escape(queued_changes_area)), re.UNICODE).match |
paul@0 | 21 | |
paul@0 | 22 | def handle_presave(event): |
paul@0 | 23 | request = event.request |
paul@0 | 24 | _ = request.getText |
paul@0 | 25 | |
paul@0 | 26 | approved_editors_group = getattr(request.cfg, "approved_editors_group", "ApprovedGroup") |
paul@0 | 27 | queued_changes_area = getattr(request.cfg, "queued_changes_area", "ApprovalQueue") |
paul@0 | 28 | |
paul@0 | 29 | pagename = event.page_editor.page_name |
paul@0 | 30 | parts = pagename.split("/") |
paul@0 | 31 | |
paul@0 | 32 | # Saving into queues has to be permitted or the mechanism will keep trying |
paul@0 | 33 | # to save into a queue of the specified page. |
paul@0 | 34 | |
paul@0 | 35 | if len(parts) > 2 and parts[-2] == queued_changes_area: |
paul@0 | 36 | return None |
paul@0 | 37 | |
paul@0 | 38 | # For normal pages, the user has to be approved. Otherwise, the page will be |
paul@0 | 39 | # saved into a queue. |
paul@0 | 40 | |
paul@0 | 41 | elif not request.user.valid or ( |
paul@0 | 42 | not request.dicts.has_member(approved_editors_group, request.user.name) and \ |
paul@0 | 43 | not request.user.isSuperUser()): |
paul@0 | 44 | |
paul@0 | 45 | # Get a queue number by listing the subpages of the queue area and |
paul@0 | 46 | # finding the highest queue number, then adding one to it. |
paul@0 | 47 | |
paul@0 | 48 | queue_number = -1 |
paul@0 | 49 | |
paul@0 | 50 | for name in request.rootpage.getPageList(exists=1, filter=match_queue_pages(pagename, queued_changes_area)): |
paul@0 | 51 | number = name.split("/")[-1] |
paul@0 | 52 | if number.isdigit(): |
paul@0 | 53 | queue_number = max(queue_number, int(number)) |
paul@0 | 54 | |
paul@0 | 55 | queue_number += 1 |
paul@0 | 56 | |
paul@0 | 57 | # Save the page in the queue. |
paul@0 | 58 | |
paul@0 | 59 | new_page = PageEditor(request, "%s/%s/%s" % (pagename, queued_changes_area, queue_number)) |
paul@0 | 60 | |
paul@0 | 61 | try: |
paul@0 | 62 | new_page.saveText(event.new_text, 0) |
paul@0 | 63 | except PageEditor.Unchanged: |
paul@0 | 64 | pass |
paul@0 | 65 | |
paul@0 | 66 | # Abort the page save operation. |
paul@0 | 67 | |
paul@0 | 68 | return Abort(_("Your changes have been queued for approval.")) |
paul@0 | 69 | |
paul@0 | 70 | def handle(event): |
paul@0 | 71 | if isinstance(event, PagePreSaveEvent): |
paul@0 | 72 | return handle_presave(event) |
paul@0 | 73 | |
paul@0 | 74 | # vim: tabstop=4 expandtab shiftwidth=4 |