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@3 | 6 | unprivileged user attempted to save a page. Since queued changes are placed |
paul@3 | 7 | in a subpage, 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@7 | 17 | from MoinMoin.Page import Page |
paul@0 | 18 | from MoinMoin.PageEditor import PageEditor |
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@7 | 37 | form = get_form(request) |
paul@7 | 38 | rev = form.get("rev") |
paul@1 | 39 | |
paul@1 | 40 | d = { |
paul@1 | 41 | "buttons_html" : buttons_html, |
paul@1 | 42 | "prompt" : escape(_("Approve the displayed page version?")), |
paul@1 | 43 | } |
paul@1 | 44 | |
paul@1 | 45 | # Prepare the output HTML. |
paul@1 | 46 | |
paul@1 | 47 | html = ''' |
paul@1 | 48 | <table> |
paul@1 | 49 | <tr> |
paul@4 | 50 | <td>%(prompt)s</td> |
paul@1 | 51 | </tr> |
paul@1 | 52 | <tr> |
paul@1 | 53 | <td class="buttons"> |
paul@1 | 54 | %(buttons_html)s |
paul@1 | 55 | </td> |
paul@1 | 56 | </tr> |
paul@1 | 57 | </table>''' % d |
paul@1 | 58 | |
paul@7 | 59 | if rev: |
paul@7 | 60 | html += ''' |
paul@7 | 61 | <input name="rev" type="hidden" value="%(rev)s" />''' % {"rev" : escattr(rev[0])} |
paul@7 | 62 | |
paul@1 | 63 | return html |
paul@0 | 64 | |
paul@0 | 65 | def do_action(self): |
paul@0 | 66 | |
paul@0 | 67 | "Approve the page and move it into place." |
paul@0 | 68 | |
paul@0 | 69 | _ = self._ |
paul@0 | 70 | request = self.request |
paul@0 | 71 | |
paul@0 | 72 | # Make sure that only suitably privileged users can perform this action. |
paul@0 | 73 | |
paul@3 | 74 | queued_changes_page = get_queued_changes_page(request) |
paul@0 | 75 | |
paul@4 | 76 | if not is_reviewer(request): |
paul@0 | 77 | return 0, _("Only page reviewers can perform this action.") |
paul@0 | 78 | |
paul@0 | 79 | # Edit the target page, using this page's content. |
paul@1 | 80 | # The current page must be a queued page version. |
paul@0 | 81 | |
paul@3 | 82 | if not is_queued_changes_page(request, self.pagename): |
paul@1 | 83 | return 0, _("This page is not queued for approval.") |
paul@1 | 84 | |
paul@1 | 85 | target_page_name = get_target_page_name(self.pagename) |
paul@0 | 86 | target_page = PageEditor(request, target_page_name) |
paul@0 | 87 | |
paul@7 | 88 | # First, the displayed revision must be retrieved. |
paul@4 | 89 | |
paul@7 | 90 | form = get_form(request) |
paul@7 | 91 | |
paul@7 | 92 | # Get the revision or None. |
paul@7 | 93 | |
paul@7 | 94 | rev = form.get("rev") |
paul@7 | 95 | if rev is None: |
paul@7 | 96 | rev = self.page.current_rev() |
paul@7 | 97 | else: |
paul@7 | 98 | rev = int(rev[0]) |
paul@7 | 99 | |
paul@7 | 100 | page = Page(request, self.page.page_name, rev=rev) |
paul@7 | 101 | body = page.get_raw_body() |
paul@10 | 102 | |
paul@10 | 103 | # Save the target page, removing any introduced directives. |
paul@10 | 104 | |
paul@10 | 105 | body = remove_directives(body, ["acl", "parent-revision"]) |
paul@0 | 106 | |
paul@0 | 107 | try: |
paul@7 | 108 | target_page.saveText(body, 0, comment=_("Changes to page approved from queue revision %d.") % rev) |
paul@0 | 109 | except PageEditor.Unchanged: |
paul@0 | 110 | pass |
paul@0 | 111 | |
paul@0 | 112 | # Delete the current page. |
paul@3 | 113 | # NOTE: The page could be deleted completely or certain revisions |
paul@3 | 114 | # NOTE: purged. |
paul@3 | 115 | # NOTE: (to-do/proper-queued-page-deletion.txt) |
paul@0 | 116 | |
paul@0 | 117 | current_page = PageEditor(request, self.pagename) |
paul@0 | 118 | current_page.deletePage(_("Changes to page approved.")) |
paul@0 | 119 | |
paul@0 | 120 | # Redirect to the target page. |
paul@0 | 121 | |
paul@0 | 122 | request.http_redirect(target_page.url(request)) |
paul@0 | 123 | return 1, None |
paul@0 | 124 | |
paul@0 | 125 | def render_success(self, msg, msgtype): |
paul@0 | 126 | |
paul@0 | 127 | """ |
paul@0 | 128 | Render neither 'msg' nor 'msgtype' since redirection should occur |
paul@0 | 129 | instead. |
paul@0 | 130 | NOTE: msgtype is optional because MoinMoin 1.5.x does not support it. |
paul@0 | 131 | """ |
paul@0 | 132 | |
paul@0 | 133 | pass |
paul@0 | 134 | |
paul@0 | 135 | # Action function. |
paul@0 | 136 | |
paul@0 | 137 | def execute(pagename, request): |
paul@0 | 138 | ApproveChanges(pagename, request).render() |
paul@0 | 139 | |
paul@0 | 140 | # vim: tabstop=4 expandtab shiftwidth=4 |