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@28 | 10 | @copyright: 2011, 2013 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@11 | 18 | from MoinMoin.PageEditor import PageEditor, conflict_markers |
paul@11 | 19 | from MoinMoin.util import diff3 |
paul@33 | 20 | from MoinSupport import getPagesForSearch |
paul@1 | 21 | from ApproveChangesSupport import * |
paul@0 | 22 | |
paul@0 | 23 | # Action class and supporting functions. |
paul@0 | 24 | |
paul@0 | 25 | class ApproveChanges(ActionBase): |
paul@0 | 26 | |
paul@0 | 27 | "An action which approves a queued page version." |
paul@0 | 28 | |
paul@28 | 29 | queued_content_directives = ["acl", "parent-revision", "unapproved-user", "unapproved-user-queue"] |
paul@28 | 30 | |
paul@0 | 31 | def __init__(self, pagename, request): |
paul@0 | 32 | ActionBase.__init__(self, pagename, request) |
paul@0 | 33 | _ = self._ |
paul@33 | 34 | self.form_trigger = "execute" |
paul@33 | 35 | self.form_trigger_label = _("Execute the indicated changes") |
paul@0 | 36 | |
paul@15 | 37 | def get_revision(self): |
paul@15 | 38 | request = self.request |
paul@15 | 39 | form = get_form(request) |
paul@15 | 40 | |
paul@15 | 41 | # Get the revision or None. |
paul@15 | 42 | |
paul@15 | 43 | rev = form.get("rev") |
paul@15 | 44 | if rev is None: |
paul@15 | 45 | return self.page.current_rev() |
paul@15 | 46 | else: |
paul@15 | 47 | return int(rev[0]) |
paul@15 | 48 | |
paul@15 | 49 | def get_body(self, rev): |
paul@15 | 50 | request = self.request |
paul@15 | 51 | page = Page(request, self.page.page_name, rev=rev) |
paul@15 | 52 | return page.get_raw_body() |
paul@15 | 53 | |
paul@0 | 54 | def get_form_html(self, buttons_html): |
paul@0 | 55 | _ = self._ |
paul@0 | 56 | request = self.request |
paul@0 | 57 | fmt = request.formatter |
paul@33 | 58 | form = get_form(request) |
paul@18 | 59 | |
paul@18 | 60 | if not is_queued_changes_page(request, self.pagename): |
paul@18 | 61 | return fmt.paragraph(1) + fmt.text(_("This page does not show queued changes.")) + fmt.paragraph(0) |
paul@18 | 62 | |
paul@15 | 63 | rev = self.get_revision() |
paul@15 | 64 | |
paul@15 | 65 | # Get information about the queued changes. |
paul@15 | 66 | |
paul@15 | 67 | body = self.get_body(rev) |
paul@28 | 68 | _body, directives = remove_directives(body, self.queued_content_directives) |
paul@15 | 69 | |
paul@15 | 70 | # Get the target page's parent revision for the queued changes. |
paul@15 | 71 | |
paul@28 | 72 | target_page_name = get_target_page_name(self.page) |
paul@15 | 73 | target_page = PageEditor(request, target_page_name) |
paul@15 | 74 | |
paul@15 | 75 | current_rev = target_page.current_rev() |
paul@15 | 76 | parent_rev = int(directives.get("parent-revision", current_rev)) |
paul@1 | 77 | |
paul@21 | 78 | # Get the user who submitted the changes. |
paul@21 | 79 | |
paul@21 | 80 | username = directives.get("unapproved-user") |
paul@21 | 81 | |
paul@33 | 82 | # Get other edit details, if requested. |
paul@33 | 83 | |
paul@33 | 84 | if form.get("show-edits") and username: |
paul@33 | 85 | queued_edits = getPagesForSearch("title:regex:%s/ApprovalQueue$" % username, request) |
paul@33 | 86 | else: |
paul@33 | 87 | queued_edits = [] |
paul@33 | 88 | |
paul@1 | 89 | d = { |
paul@21 | 90 | "approval_label" : escape(_("Make %s an approved user") % username), |
paul@33 | 91 | "approve_edit_label" : escape(_("Approve the displayed page version")), |
paul@33 | 92 | "block_label" : escape(_("Block user %s by disabling their account") % username), |
paul@1 | 93 | "buttons_html" : buttons_html, |
paul@33 | 94 | "discard_edit_label" : escape(_("Discard the displayed page version")), |
paul@33 | 95 | "editaction_label" : escape(_("Edit action")), |
paul@33 | 96 | "nothing_label" : escape(_("Take no action for now")), |
paul@15 | 97 | "rev" : escattr(rev), |
paul@15 | 98 | "notice" : escape(_("The affected page has been edited since the queued changes were made.")), |
paul@33 | 99 | "show_edits_label" : escattr(_("Show all queued edits by this user.")), |
paul@33 | 100 | "showing_edits_label" : escape(_("Queued edits by this user")), |
paul@33 | 101 | "useraction_label" : escape(_("User action")), |
paul@1 | 102 | } |
paul@1 | 103 | |
paul@33 | 104 | # Make sure that the radio buttons are selected. |
paul@33 | 105 | |
paul@33 | 106 | for value in "nothing", "approve", "block": |
paul@33 | 107 | d["useraction_%s" % value] = form.get("useraction", ["nothing"])[0] == value and "checked='checked'" or "" |
paul@33 | 108 | |
paul@33 | 109 | for value in "approve", "discard": |
paul@33 | 110 | d["editaction_%s" % value] = form.get("editaction", ["approve"])[0] == value and "checked='checked'" or "" |
paul@33 | 111 | |
paul@1 | 112 | # Prepare the output HTML. |
paul@1 | 113 | |
paul@1 | 114 | html = ''' |
paul@33 | 115 | <table>''' |
paul@15 | 116 | |
paul@15 | 117 | if parent_rev != current_rev: |
paul@15 | 118 | html += ''' |
paul@15 | 119 | <tr> |
paul@33 | 120 | <td colspan="2"><strong>%(notice)s</strong></td> |
paul@15 | 121 | </tr>''' % d |
paul@15 | 122 | |
paul@33 | 123 | # Actions to be taken with the edit. |
paul@33 | 124 | |
paul@33 | 125 | html += ''' |
paul@33 | 126 | <tr> |
paul@33 | 127 | <td class="label" rowspan="2"><label>%(editaction_label)s</label></td> |
paul@33 | 128 | <td><input name="editaction" type="radio" value="approve" %(editaction_approve)s /> <label>%(approve_edit_label)s</label></td> |
paul@33 | 129 | </tr> |
paul@33 | 130 | <tr> |
paul@33 | 131 | <td><input name="editaction" type="radio" value="discard" %(editaction_discard)s /> <label>%(discard_edit_label)s</label></td> |
paul@33 | 132 | </tr>''' % d |
paul@33 | 133 | |
paul@33 | 134 | # User information and actions. |
paul@33 | 135 | |
paul@21 | 136 | if username and not user_is_approved(request, username): |
paul@33 | 137 | if not queued_edits: |
paul@33 | 138 | html += ''' |
paul@33 | 139 | <tr> |
paul@33 | 140 | <td></td> |
paul@33 | 141 | <td><input name="show-edits" type="submit" value="%(show_edits_label)s" /></td> |
paul@33 | 142 | </tr>''' % d |
paul@33 | 143 | else: |
paul@33 | 144 | html += ''' |
paul@33 | 145 | <tr> |
paul@33 | 146 | <td class="label"><label>%(showing_edits_label)s</label></td> |
paul@33 | 147 | <td><ul>''' % d |
paul@33 | 148 | |
paul@33 | 149 | for queued_page in queued_edits: |
paul@33 | 150 | html += ''' |
paul@33 | 151 | <li>%s</li>''' % escape(queued_page.page_name) |
paul@33 | 152 | |
paul@33 | 153 | html += ''' |
paul@33 | 154 | </ul></td> |
paul@33 | 155 | </tr>''' |
paul@33 | 156 | |
paul@33 | 157 | # Actions to be taken with the user. |
paul@33 | 158 | |
paul@21 | 159 | html += ''' |
paul@21 | 160 | <tr> |
paul@33 | 161 | <td class="label" rowspan="3"><label>%(useraction_label)s</label></td> |
paul@33 | 162 | <td><input name="useraction" type="radio" value="nothing" %(useraction_nothing)s /> <label>%(nothing_label)s</label></td> |
paul@33 | 163 | </tr> |
paul@33 | 164 | <tr> |
paul@33 | 165 | <td><input name="useraction" type="radio" value="approve" %(useraction_approve)s /> <label>%(approval_label)s</label></td> |
paul@33 | 166 | </tr> |
paul@33 | 167 | <tr> |
paul@33 | 168 | <td><input name="useraction" type="radio" value="block" %(useraction_block)s /> <label>%(block_label)s</label></td> |
paul@21 | 169 | </tr>''' % d |
paul@21 | 170 | |
paul@15 | 171 | html += ''' |
paul@1 | 172 | <tr> |
paul@33 | 173 | <td></td> |
paul@1 | 174 | <td class="buttons"> |
paul@1 | 175 | %(buttons_html)s |
paul@1 | 176 | </td> |
paul@1 | 177 | </tr> |
paul@1 | 178 | </table>''' % d |
paul@1 | 179 | |
paul@7 | 180 | if rev: |
paul@7 | 181 | html += ''' |
paul@15 | 182 | <input name="rev" type="hidden" value="%(rev)s" />''' % d |
paul@7 | 183 | |
paul@1 | 184 | return html |
paul@0 | 185 | |
paul@0 | 186 | def do_action(self): |
paul@0 | 187 | |
paul@0 | 188 | "Approve the page and move it into place." |
paul@0 | 189 | |
paul@0 | 190 | _ = self._ |
paul@0 | 191 | request = self.request |
paul@21 | 192 | form = get_form(request) |
paul@0 | 193 | |
paul@0 | 194 | # Make sure that only suitably privileged users can perform this action. |
paul@0 | 195 | |
paul@3 | 196 | queued_changes_page = get_queued_changes_page(request) |
paul@0 | 197 | |
paul@4 | 198 | if not is_reviewer(request): |
paul@0 | 199 | return 0, _("Only page reviewers can perform this action.") |
paul@0 | 200 | |
paul@0 | 201 | # Edit the target page, using this page's content. |
paul@1 | 202 | # The current page must be a queued page version. |
paul@0 | 203 | |
paul@3 | 204 | if not is_queued_changes_page(request, self.pagename): |
paul@1 | 205 | return 0, _("This page is not queued for approval.") |
paul@1 | 206 | |
paul@11 | 207 | # First, the displayed revision of the queued changes page must be |
paul@11 | 208 | # retrieved. |
paul@4 | 209 | |
paul@15 | 210 | rev = self.get_revision() |
paul@15 | 211 | body = self.get_body(rev) |
paul@10 | 212 | |
paul@11 | 213 | # Remove any introduced directives. |
paul@11 | 214 | |
paul@28 | 215 | body, directives = remove_directives(body, self.queued_content_directives) |
paul@11 | 216 | |
paul@11 | 217 | # Get the target page's parent revision for the queued changes. |
paul@11 | 218 | |
paul@28 | 219 | target_page_name = get_target_page_name(self.page) |
paul@11 | 220 | target_page = PageEditor(request, target_page_name) |
paul@11 | 221 | |
paul@11 | 222 | current_rev = target_page.current_rev() |
paul@11 | 223 | parent_rev = int(directives.get("parent-revision", current_rev)) |
paul@11 | 224 | |
paul@21 | 225 | # Get the user who submitted the changes. |
paul@21 | 226 | |
paul@21 | 227 | username = directives.get("unapproved-user") |
paul@21 | 228 | |
paul@21 | 229 | # Approve the user if requested, regardless of what happens below. |
paul@21 | 230 | |
paul@33 | 231 | if username and form.get("useraction", [""])[0] == "approve": |
paul@21 | 232 | add_to_group_page(request, username, get_approved_editors_group(request)) |
paul@21 | 233 | |
paul@33 | 234 | # NOTE: Add user blocking. |
paul@33 | 235 | |
paul@33 | 236 | # Discard the edit if requested. |
paul@33 | 237 | |
paul@33 | 238 | if form.get("editaction", [""])[0] == "discard": |
paul@33 | 239 | |
paul@33 | 240 | # NOTE: The page could be deleted completely or certain revisions |
paul@33 | 241 | # NOTE: purged. |
paul@33 | 242 | # NOTE: (to-do/proper-queued-page-deletion.txt) |
paul@33 | 243 | |
paul@33 | 244 | current_page = PageEditor(request, self.pagename) |
paul@33 | 245 | current_page.deletePage(_("Changes to page discarded.")) |
paul@33 | 246 | |
paul@33 | 247 | # Redirect to the target page. |
paul@33 | 248 | |
paul@33 | 249 | request.http_redirect(target_page.url(request)) |
paul@33 | 250 | return 1, None |
paul@33 | 251 | |
paul@11 | 252 | # Where the parent revision differs from the current revision of the |
paul@11 | 253 | # page, attempt to merge the changes. |
paul@11 | 254 | |
paul@12 | 255 | conflict = False |
paul@11 | 256 | |
paul@11 | 257 | if parent_rev != current_rev: |
paul@10 | 258 | |
paul@11 | 259 | # The body of the parent revision of the target page, along with the |
paul@11 | 260 | # body of the current revision must be acquired. |
paul@11 | 261 | |
paul@11 | 262 | parent_body = Page(request, target_page_name, rev=parent_rev).get_raw_body() |
paul@11 | 263 | current_body = target_page.get_raw_body() |
paul@11 | 264 | |
paul@11 | 265 | # The parent, current and queued texts must then be merged. |
paul@11 | 266 | |
paul@11 | 267 | body = diff3.text_merge(parent_body, current_body, body, True, *conflict_markers) |
paul@11 | 268 | |
paul@11 | 269 | # Look for conflict markers and redirect to edit mode on the |
paul@11 | 270 | # resulting page if they are present. |
paul@11 | 271 | |
paul@11 | 272 | for marker in conflict_markers: |
paul@11 | 273 | if body.find(marker) != -1: |
paul@12 | 274 | conflict = True |
paul@11 | 275 | break |
paul@11 | 276 | |
paul@11 | 277 | # Delete the queued changes page. |
paul@3 | 278 | # NOTE: The page could be deleted completely or certain revisions |
paul@3 | 279 | # NOTE: purged. |
paul@3 | 280 | # NOTE: (to-do/proper-queued-page-deletion.txt) |
paul@0 | 281 | |
paul@0 | 282 | current_page = PageEditor(request, self.pagename) |
paul@0 | 283 | current_page.deletePage(_("Changes to page approved.")) |
paul@0 | 284 | |
paul@12 | 285 | # Prepare a comment. |
paul@12 | 286 | |
paul@28 | 287 | comment = username and \ |
paul@28 | 288 | _("Changes to page by %s approved from queue revision %d.") % (username, rev) or \ |
paul@28 | 289 | _("Changes to page approved from queue revision %d.") % rev |
paul@12 | 290 | |
paul@12 | 291 | # Save the target page, but only if there is no conflict. |
paul@12 | 292 | |
paul@12 | 293 | if not conflict: |
paul@14 | 294 | |
paul@14 | 295 | # Switch user if a specific user was recorded. |
paul@14 | 296 | |
paul@14 | 297 | if username: |
paul@14 | 298 | new_user = get_user(request, username) |
paul@14 | 299 | else: |
paul@14 | 300 | new_user = None |
paul@14 | 301 | |
paul@14 | 302 | if new_user: |
paul@14 | 303 | user = request.user |
paul@14 | 304 | request.user = new_user |
paul@14 | 305 | |
paul@14 | 306 | # Save the page. |
paul@14 | 307 | |
paul@12 | 308 | try: |
paul@14 | 309 | try: |
paul@14 | 310 | target_page.saveText(body, 0, comment=comment) |
paul@14 | 311 | except PageEditor.Unchanged: |
paul@14 | 312 | pass |
paul@14 | 313 | |
paul@14 | 314 | # Restore the user. |
paul@14 | 315 | |
paul@14 | 316 | finally: |
paul@14 | 317 | if new_user: |
paul@14 | 318 | request.user = user |
paul@12 | 319 | |
paul@12 | 320 | # Redirect to the target page. |
paul@0 | 321 | |
paul@12 | 322 | request.http_redirect(target_page.url(request)) |
paul@12 | 323 | |
paul@12 | 324 | # Otherwise, send the page editor. |
paul@12 | 325 | # NOTE: Replacing the revision in the request to prevent Moin from |
paul@12 | 326 | # NOTE: attempting to use the queued changes page's revision. |
paul@12 | 327 | # NOTE: Replacing the action and page in the request to avoid issues |
paul@12 | 328 | # NOTE: with editing tickets. |
paul@12 | 329 | |
paul@12 | 330 | else: |
paul@12 | 331 | request.rev = current_rev |
paul@12 | 332 | request.action = "edit" |
paul@12 | 333 | request.page = target_page |
paul@12 | 334 | target_page.sendEditor(preview=body, comment=comment, staytop=True) |
paul@12 | 335 | |
paul@0 | 336 | return 1, None |
paul@0 | 337 | |
paul@0 | 338 | def render_success(self, msg, msgtype): |
paul@0 | 339 | |
paul@0 | 340 | """ |
paul@0 | 341 | Render neither 'msg' nor 'msgtype' since redirection should occur |
paul@0 | 342 | instead. |
paul@0 | 343 | NOTE: msgtype is optional because MoinMoin 1.5.x does not support it. |
paul@0 | 344 | """ |
paul@0 | 345 | |
paul@0 | 346 | pass |
paul@0 | 347 | |
paul@0 | 348 | # Action function. |
paul@0 | 349 | |
paul@0 | 350 | def execute(pagename, request): |
paul@0 | 351 | ApproveChanges(pagename, request).render() |
paul@0 | 352 | |
paul@0 | 353 | # vim: tabstop=4 expandtab shiftwidth=4 |