1 # -*- coding: iso-8859-1 -*- 2 """ 3 MoinMoin - PostComment action 4 5 @copyright: 2013 by Paul Boddie <paul@boddie.org.uk> 6 @license: GNU GPL (v2 or later), see COPYING.txt for details. 7 """ 8 9 from MoinMoin.action import ActionBase 10 from MoinMoin.PageEditor import PageEditor 11 from MoinMoin.wikiutil import escape 12 from MoinSupport import getPagesForSearch, getPagesFromResults, ActionSupport 13 14 Dependencies = ['pages'] 15 16 class PostComment(ActionBase, ActionSupport): 17 18 "Post a comment to the wiki." 19 20 def get_form_html(self, buttons_html): 21 22 "Return the action's form incorporating the 'buttons_html'." 23 24 _ = self._ 25 request = self.request 26 form = self.get_form() 27 28 comment = form.get("comment", [""])[0] 29 30 d = { 31 "comment_label" : escape(comment and _("Please review your comment.") or _("Write a comment in the box.")), 32 "comment_default" : escape(comment), 33 "buttons_html" : buttons_html, 34 } 35 36 return u"""\ 37 <p>%(comment_label)s</p> 38 <textarea name="comment" cols="60" rows="10">%(comment_default)s</textarea> 39 <p>%(buttons_html)s</p> 40 """ % d 41 42 def do_action(self): 43 44 "Attempt to post a comment." 45 46 _ = self._ 47 request = self.request 48 form = self.get_form() 49 50 comment = form.get("comment", [""])[0] 51 52 if not comment.strip(): 53 return 0, _("A comment should have some content.") 54 55 if not request.user.valid or not request.user.may.write(self.pagename): 56 return 0, _("You are not allowed to comment on this page.") 57 58 # Determine the last comment. 59 60 comments = get_comment_numbers(self.pagename, request) 61 last_comment_pagename = comments and comments[-1] or -1 62 63 # Write the new page. 64 65 new_page = PageEditor(request, "%s/%04d" % (self.pagename, last_comment_pagename + 1)) 66 67 try: 68 new_page.saveText(comment, 0) 69 return 1, _("Comment added.") 70 except new_page.SaveError, exc: 71 return 0, unicode(exc) 72 73 def get_comment_numbers(pagename, request): 74 75 """ 76 Return a list of comment numbers associated with the given 'pagename', using 77 the 'request' provided. 78 """ 79 80 pagenames = [] 81 82 for page in getPagesForSearch("title:regex:^%s/" % pagename, request): 83 basename, number = page.page_name.rsplit("/", 1) 84 if basename == pagename and number.isdigit(): 85 pagenames.append(int(number)) 86 87 pagenames.sort() 88 return pagenames 89 90 # Action invocation function. 91 92 def execute(pagename, request): 93 PostComment(pagename, request).render() 94 95 # vim: tabstop=4 expandtab shiftwidth=4