paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - PostComment action |
paul@0 | 4 | |
paul@0 | 5 | @copyright: 2013 by Paul Boddie <paul@boddie.org.uk> |
paul@0 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@0 | 7 | """ |
paul@0 | 8 | |
paul@0 | 9 | from MoinMoin.action import ActionBase |
paul@0 | 10 | from MoinMoin.PageEditor import PageEditor |
paul@0 | 11 | from MoinMoin.security import Permissions |
paul@0 | 12 | from MoinMoin.wikiutil import escape |
paul@0 | 13 | from MoinSupport import getPagesForSearch, getPagesFromResults, ActionSupport |
paul@0 | 14 | |
paul@3 | 15 | __version__ = "0.1" |
paul@3 | 16 | |
paul@0 | 17 | Dependencies = ['pages'] |
paul@0 | 18 | |
paul@0 | 19 | class SpecialPermissions(Permissions): |
paul@0 | 20 | |
paul@0 | 21 | "Permit saving of ACL-enabled comment pages." |
paul@0 | 22 | |
paul@0 | 23 | def __init__(self, user, pagename): |
paul@0 | 24 | Permissions.__init__(self, user) |
paul@0 | 25 | self.pagename = pagename |
paul@0 | 26 | |
paul@0 | 27 | def admin(self, pagename): |
paul@0 | 28 | return pagename == self.pagename |
paul@0 | 29 | |
paul@0 | 30 | class PostComment(ActionBase, ActionSupport): |
paul@0 | 31 | |
paul@0 | 32 | "Post a comment to the wiki." |
paul@0 | 33 | |
paul@0 | 34 | def get_form_html(self, buttons_html): |
paul@0 | 35 | |
paul@0 | 36 | "Return the action's form incorporating the 'buttons_html'." |
paul@0 | 37 | |
paul@0 | 38 | _ = self._ |
paul@0 | 39 | request = self.request |
paul@0 | 40 | form = self.get_form() |
paul@0 | 41 | |
paul@0 | 42 | comment = form.get("comment", [""])[0] |
paul@0 | 43 | |
paul@0 | 44 | d = { |
paul@0 | 45 | "comment_label" : escape(comment and _("Please review your comment.") or _("Write a comment in the box.")), |
paul@0 | 46 | "comment_default" : escape(comment), |
paul@0 | 47 | "buttons_html" : buttons_html, |
paul@0 | 48 | } |
paul@0 | 49 | |
paul@0 | 50 | return u"""\ |
paul@0 | 51 | <p>%(comment_label)s</p> |
paul@0 | 52 | <textarea name="comment" cols="60" rows="10">%(comment_default)s</textarea> |
paul@0 | 53 | <p>%(buttons_html)s</p> |
paul@0 | 54 | """ % d |
paul@0 | 55 | |
paul@0 | 56 | def do_action(self): |
paul@0 | 57 | |
paul@0 | 58 | "Attempt to post a comment." |
paul@0 | 59 | |
paul@0 | 60 | _ = self._ |
paul@0 | 61 | request = self.request |
paul@0 | 62 | form = self.get_form() |
paul@0 | 63 | |
paul@0 | 64 | comment = form.get("comment", [""])[0] |
paul@0 | 65 | |
paul@0 | 66 | if not comment.strip(): |
paul@0 | 67 | return 0, _("A comment should have some content.") |
paul@0 | 68 | |
paul@0 | 69 | if not request.user.valid or not request.user.may.write(self.pagename): |
paul@0 | 70 | return 0, _("You are not allowed to comment on this page.") |
paul@0 | 71 | |
paul@0 | 72 | # Determine the last comment. |
paul@0 | 73 | |
paul@0 | 74 | comments = get_comment_numbers(self.pagename, request) |
paul@0 | 75 | last_comment_pagename = comments and comments[-1] or -1 |
paul@0 | 76 | |
paul@0 | 77 | # Write the new page. |
paul@0 | 78 | |
paul@0 | 79 | comment_pagename = "%s/%04d" % (self.pagename, last_comment_pagename + 1) |
paul@0 | 80 | new_page = PageEditor(request, comment_pagename) |
paul@0 | 81 | username = request.user.name |
paul@0 | 82 | |
paul@0 | 83 | try: |
paul@0 | 84 | # To add a page with an ACL, a special policy is required. |
paul@0 | 85 | |
paul@0 | 86 | may = request.user.may |
paul@0 | 87 | request.user.may = SpecialPermissions(request.user, comment_pagename) |
paul@0 | 88 | |
paul@0 | 89 | # Save the page, labelling it with the actual username. |
paul@0 | 90 | |
paul@0 | 91 | try: |
paul@0 | 92 | new_page.saveText(comment_template % (username, username, comment), 0) |
paul@0 | 93 | |
paul@0 | 94 | # Restore the superusers. |
paul@0 | 95 | |
paul@0 | 96 | finally: |
paul@0 | 97 | request.user.may = may |
paul@0 | 98 | |
paul@0 | 99 | return 1, _("Comment added.") |
paul@0 | 100 | except new_page.SaveError, exc: |
paul@0 | 101 | return 0, unicode(exc) |
paul@0 | 102 | |
paul@0 | 103 | comment_template = """\ |
paul@0 | 104 | #acl %s:read,write,delete,revert All:read |
paul@0 | 105 | #pragma comment-owner %s |
paul@0 | 106 | %s""" |
paul@0 | 107 | |
paul@0 | 108 | def get_comment_numbers(pagename, request): |
paul@0 | 109 | |
paul@0 | 110 | """ |
paul@0 | 111 | Return a list of comment numbers associated with the given 'pagename', using |
paul@0 | 112 | the 'request' provided. |
paul@0 | 113 | """ |
paul@0 | 114 | |
paul@0 | 115 | pagenames = [] |
paul@0 | 116 | |
paul@0 | 117 | for page in getPagesForSearch("title:regex:^%s/" % pagename, request): |
paul@0 | 118 | basename, number = page.page_name.rsplit("/", 1) |
paul@0 | 119 | if basename == pagename and number.isdigit(): |
paul@0 | 120 | pagenames.append(int(number)) |
paul@0 | 121 | |
paul@0 | 122 | pagenames.sort() |
paul@0 | 123 | return pagenames |
paul@0 | 124 | |
paul@0 | 125 | # Action invocation function. |
paul@0 | 126 | |
paul@0 | 127 | def execute(pagename, request): |
paul@0 | 128 | PostComment(pagename, request).render() |
paul@0 | 129 | |
paul@0 | 130 | # vim: tabstop=4 expandtab shiftwidth=4 |