paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - ApproveChanges |
paul@0 | 4 | |
paul@0 | 5 | Permit the approval of an edited page queued by the page editor when an |
paul@0 | 6 | unprivileged user attempted to save a page. Since queued pages are placed in |
paul@0 | 7 | a subpage area, this action just moves the queued page content into the |
paul@0 | 8 | existing page when approving the changes. |
paul@0 | 9 | |
paul@0 | 10 | @copyright: 2011 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 11 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@0 | 12 | """ |
paul@0 | 13 | |
paul@0 | 14 | Dependencies = ['pages'] |
paul@0 | 15 | |
paul@0 | 16 | from MoinMoin.action import ActionBase |
paul@0 | 17 | from MoinMoin.PageEditor import PageEditor |
paul@1 | 18 | from MoinMoin.wikiutil import escape |
paul@1 | 19 | from ApproveChangesSupport import * |
paul@0 | 20 | |
paul@0 | 21 | # Action class and supporting functions. |
paul@0 | 22 | |
paul@0 | 23 | class ApproveChanges(ActionBase): |
paul@0 | 24 | |
paul@0 | 25 | "An action which approves a queued page version." |
paul@0 | 26 | |
paul@0 | 27 | def __init__(self, pagename, request): |
paul@0 | 28 | ActionBase.__init__(self, pagename, request) |
paul@0 | 29 | _ = self._ |
paul@0 | 30 | self.form_trigger = "approve" |
paul@0 | 31 | self.form_trigger_label = _("Approve changes") |
paul@0 | 32 | |
paul@0 | 33 | def get_form_html(self, buttons_html): |
paul@0 | 34 | _ = self._ |
paul@0 | 35 | request = self.request |
paul@0 | 36 | fmt = request.formatter |
paul@1 | 37 | |
paul@1 | 38 | d = { |
paul@1 | 39 | "buttons_html" : buttons_html, |
paul@1 | 40 | "prompt" : escape(_("Approve the displayed page version?")), |
paul@1 | 41 | "purge_label" : escape(_("Purge all other queued versions")) |
paul@1 | 42 | } |
paul@1 | 43 | |
paul@1 | 44 | # Prepare the output HTML. |
paul@1 | 45 | |
paul@1 | 46 | html = ''' |
paul@1 | 47 | <table> |
paul@1 | 48 | <tr> |
paul@1 | 49 | <td colspan="2">%(prompt)s</td> |
paul@1 | 50 | </tr> |
paul@1 | 51 | <tr> |
paul@1 | 52 | <td class="label"><label>%(purge_label)s</label></td> |
paul@1 | 53 | <td> |
paul@1 | 54 | <input name="purge" type="checkbox" value="yes" /> |
paul@1 | 55 | </td> |
paul@1 | 56 | </tr> |
paul@1 | 57 | <tr> |
paul@1 | 58 | <td></td> |
paul@1 | 59 | <td class="buttons"> |
paul@1 | 60 | %(buttons_html)s |
paul@1 | 61 | </td> |
paul@1 | 62 | </tr> |
paul@1 | 63 | </table>''' % d |
paul@1 | 64 | |
paul@1 | 65 | return html |
paul@0 | 66 | |
paul@0 | 67 | def do_action(self): |
paul@0 | 68 | |
paul@0 | 69 | "Approve the page and move it into place." |
paul@0 | 70 | |
paul@0 | 71 | _ = self._ |
paul@0 | 72 | request = self.request |
paul@0 | 73 | |
paul@0 | 74 | # Make sure that only suitably privileged users can perform this action. |
paul@0 | 75 | |
paul@1 | 76 | queued_changes_area = get_queued_changes_area(request) |
paul@0 | 77 | reviewers_group = getattr(request.cfg, "reviewers_group", "PageReviewersGroup") |
paul@0 | 78 | |
paul@0 | 79 | if not request.user.valid or ( |
paul@0 | 80 | not request.dicts.has_member(reviewers_group, request.user.name) and \ |
paul@0 | 81 | not request.user.isSuperUser()): |
paul@0 | 82 | |
paul@0 | 83 | return 0, _("Only page reviewers can perform this action.") |
paul@0 | 84 | |
paul@0 | 85 | # Edit the target page, using this page's content. |
paul@1 | 86 | # The current page must be a queued page version. |
paul@0 | 87 | |
paul@1 | 88 | if not is_queued_page(self.pagename, queued_changes_area): |
paul@1 | 89 | return 0, _("This page is not queued for approval.") |
paul@1 | 90 | |
paul@1 | 91 | target_page_name = get_target_page_name(self.pagename) |
paul@0 | 92 | target_page = PageEditor(request, target_page_name) |
paul@0 | 93 | |
paul@0 | 94 | # Save the target page. |
paul@0 | 95 | |
paul@0 | 96 | try: |
paul@0 | 97 | target_page.saveText(self.page.get_raw_body(), 0) |
paul@0 | 98 | except PageEditor.Unchanged: |
paul@0 | 99 | pass |
paul@0 | 100 | |
paul@0 | 101 | # Delete the current page. |
paul@0 | 102 | |
paul@0 | 103 | current_page = PageEditor(request, self.pagename) |
paul@0 | 104 | current_page.deletePage(_("Changes to page approved.")) |
paul@0 | 105 | |
paul@1 | 106 | # Delete the rest of the queue if requested. |
paul@1 | 107 | |
paul@1 | 108 | form = get_form(request) |
paul@1 | 109 | |
paul@1 | 110 | if form.get("purge"): |
paul@1 | 111 | for name in get_queue_pages(request, target_page_name, queued_changes_area): |
paul@1 | 112 | queue_page = PageEditor(request, name) |
paul@1 | 113 | queue_page.deletePage(_("Changes to page rejected.")) |
paul@1 | 114 | |
paul@0 | 115 | # Redirect to the target page. |
paul@0 | 116 | |
paul@0 | 117 | request.http_redirect(target_page.url(request)) |
paul@0 | 118 | return 1, None |
paul@0 | 119 | |
paul@0 | 120 | def render_success(self, msg, msgtype): |
paul@0 | 121 | |
paul@0 | 122 | """ |
paul@0 | 123 | Render neither 'msg' nor 'msgtype' since redirection should occur |
paul@0 | 124 | instead. |
paul@0 | 125 | NOTE: msgtype is optional because MoinMoin 1.5.x does not support it. |
paul@0 | 126 | """ |
paul@0 | 127 | |
paul@0 | 128 | pass |
paul@0 | 129 | |
paul@0 | 130 | # Action function. |
paul@0 | 131 | |
paul@0 | 132 | def execute(pagename, request): |
paul@0 | 133 | ApproveChanges(pagename, request).render() |
paul@0 | 134 | |
paul@0 | 135 | # vim: tabstop=4 expandtab shiftwidth=4 |