1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/actions/PostComment.py Sun Nov 03 14:43:31 2013 +0100
1.3 @@ -0,0 +1,128 @@
1.4 +# -*- coding: iso-8859-1 -*-
1.5 +"""
1.6 + MoinMoin - PostComment action
1.7 +
1.8 + @copyright: 2013 by Paul Boddie <paul@boddie.org.uk>
1.9 + @license: GNU GPL (v2 or later), see COPYING.txt for details.
1.10 +"""
1.11 +
1.12 +from MoinMoin.action import ActionBase
1.13 +from MoinMoin.PageEditor import PageEditor
1.14 +from MoinMoin.security import Permissions
1.15 +from MoinMoin.wikiutil import escape
1.16 +from MoinSupport import getPagesForSearch, getPagesFromResults, ActionSupport
1.17 +
1.18 +Dependencies = ['pages']
1.19 +
1.20 +class SpecialPermissions(Permissions):
1.21 +
1.22 + "Permit saving of ACL-enabled comment pages."
1.23 +
1.24 + def __init__(self, user, pagename):
1.25 + Permissions.__init__(self, user)
1.26 + self.pagename = pagename
1.27 +
1.28 + def admin(self, pagename):
1.29 + return pagename == self.pagename
1.30 +
1.31 +class PostComment(ActionBase, ActionSupport):
1.32 +
1.33 + "Post a comment to the wiki."
1.34 +
1.35 + def get_form_html(self, buttons_html):
1.36 +
1.37 + "Return the action's form incorporating the 'buttons_html'."
1.38 +
1.39 + _ = self._
1.40 + request = self.request
1.41 + form = self.get_form()
1.42 +
1.43 + comment = form.get("comment", [""])[0]
1.44 +
1.45 + d = {
1.46 + "comment_label" : escape(comment and _("Please review your comment.") or _("Write a comment in the box.")),
1.47 + "comment_default" : escape(comment),
1.48 + "buttons_html" : buttons_html,
1.49 + }
1.50 +
1.51 + return u"""\
1.52 +<p>%(comment_label)s</p>
1.53 +<textarea name="comment" cols="60" rows="10">%(comment_default)s</textarea>
1.54 +<p>%(buttons_html)s</p>
1.55 +""" % d
1.56 +
1.57 + def do_action(self):
1.58 +
1.59 + "Attempt to post a comment."
1.60 +
1.61 + _ = self._
1.62 + request = self.request
1.63 + form = self.get_form()
1.64 +
1.65 + comment = form.get("comment", [""])[0]
1.66 +
1.67 + if not comment.strip():
1.68 + return 0, _("A comment should have some content.")
1.69 +
1.70 + if not request.user.valid or not request.user.may.write(self.pagename):
1.71 + return 0, _("You are not allowed to comment on this page.")
1.72 +
1.73 + # Determine the last comment.
1.74 +
1.75 + comments = get_comment_numbers(self.pagename, request)
1.76 + last_comment_pagename = comments and comments[-1] or -1
1.77 +
1.78 + # Write the new page.
1.79 +
1.80 + comment_pagename = "%s/%04d" % (self.pagename, last_comment_pagename + 1)
1.81 + new_page = PageEditor(request, comment_pagename)
1.82 + username = request.user.name
1.83 +
1.84 + try:
1.85 + # To add a page with an ACL, a special policy is required.
1.86 +
1.87 + may = request.user.may
1.88 + request.user.may = SpecialPermissions(request.user, comment_pagename)
1.89 +
1.90 + # Save the page, labelling it with the actual username.
1.91 +
1.92 + try:
1.93 + new_page.saveText(comment_template % (username, username, comment), 0)
1.94 +
1.95 + # Restore the superusers.
1.96 +
1.97 + finally:
1.98 + request.user.may = may
1.99 +
1.100 + return 1, _("Comment added.")
1.101 + except new_page.SaveError, exc:
1.102 + return 0, unicode(exc)
1.103 +
1.104 +comment_template = """\
1.105 +#acl %s:read,write,delete,revert All:read
1.106 +#pragma comment-owner %s
1.107 +%s"""
1.108 +
1.109 +def get_comment_numbers(pagename, request):
1.110 +
1.111 + """
1.112 + Return a list of comment numbers associated with the given 'pagename', using
1.113 + the 'request' provided.
1.114 + """
1.115 +
1.116 + pagenames = []
1.117 +
1.118 + for page in getPagesForSearch("title:regex:^%s/" % pagename, request):
1.119 + basename, number = page.page_name.rsplit("/", 1)
1.120 + if basename == pagename and number.isdigit():
1.121 + pagenames.append(int(number))
1.122 +
1.123 + pagenames.sort()
1.124 + return pagenames
1.125 +
1.126 +# Action invocation function.
1.127 +
1.128 +def execute(pagename, request):
1.129 + PostComment(pagename, request).render()
1.130 +
1.131 +# vim: tabstop=4 expandtab shiftwidth=4