1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/actions/PostComment.py Fri Jul 19 19:56:39 2013 +0200
1.3 @@ -0,0 +1,95 @@
1.4 +# -*- coding: iso-8859-1 -*-
1.5 +"""
1.6 + MoinMoin - IncludeComments Macro
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.wikiutil import escape
1.15 +from MoinSupport import getPagesForSearch, getPagesFromResults, ActionSupport
1.16 +
1.17 +Dependencies = ['pages']
1.18 +
1.19 +class PostComment(ActionBase, ActionSupport):
1.20 +
1.21 + "Post a comment to the wiki."
1.22 +
1.23 + def get_form_html(self, buttons_html):
1.24 +
1.25 + "Return the action's form incorporating the 'buttons_html'."
1.26 +
1.27 + _ = self._
1.28 + request = self.request
1.29 + form = self.get_form()
1.30 +
1.31 + comment = form.get("comment", [""])[0]
1.32 +
1.33 + d = {
1.34 + "comment_label" : escape(_("Write a comment in the box.")),
1.35 + "comment_default" : escape(comment),
1.36 + "buttons_html" : buttons_html,
1.37 + }
1.38 +
1.39 + return u"""\
1.40 +<p>%(comment_label)s</p>
1.41 +<textarea name="comment" cols="60" rows="10">%(comment_default)s</textarea>
1.42 +<p>%(buttons_html)s</p>
1.43 +""" % d
1.44 +
1.45 + def do_action(self):
1.46 +
1.47 + "Attempt to post a comment."
1.48 +
1.49 + _ = self._
1.50 + request = self.request
1.51 + form = self.get_form()
1.52 +
1.53 + comment = form.get("comment", [""])[0]
1.54 +
1.55 + if not comment.strip():
1.56 + return 0, _("A comment should have some content.")
1.57 +
1.58 + if not request.user.valid or not request.user.may.write(self.pagename):
1.59 + return 0, _("You are not allowed to comment on this page.")
1.60 +
1.61 + # Determine the last comment.
1.62 +
1.63 + comments = get_comment_numbers(self.pagename, request)
1.64 + last_comment_pagename = comments and comments[-1] or -1
1.65 +
1.66 + # Write the new page.
1.67 +
1.68 + new_page = PageEditor(request, "%s/%04d" % (self.pagename, last_comment_pagename + 1))
1.69 +
1.70 + try:
1.71 + new_page.saveText(comment, 0)
1.72 + return 1, _("Comment added.")
1.73 + except new_page.SaveError, exc:
1.74 + return 0, unicode(exc)
1.75 +
1.76 +def get_comment_numbers(pagename, request):
1.77 +
1.78 + """
1.79 + Return a list of comment numbers associated with the given 'pagename', using
1.80 + the 'request' provided.
1.81 + """
1.82 +
1.83 + pagenames = []
1.84 +
1.85 + for page in getPagesForSearch("title:regex:^%s/" % pagename, request):
1.86 + basename, number = page.page_name.rsplit("/", 1)
1.87 + if basename == pagename and number.isdigit():
1.88 + pagenames.append(int(number))
1.89 +
1.90 + pagenames.sort()
1.91 + return pagenames
1.92 +
1.93 +# Action invocation function.
1.94 +
1.95 +def execute(pagename, request):
1.96 + PostComment(pagename, request).render()
1.97 +
1.98 +# vim: tabstop=4 expandtab shiftwidth=4