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