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