1.1 --- a/actions/PostComment.py Sun Nov 03 23:26:52 2013 +0100
1.2 +++ b/actions/PostComment.py Wed Nov 06 15:13:28 2013 +0100
1.3 @@ -10,7 +10,9 @@
1.4 from MoinMoin.PageEditor import PageEditor
1.5 from MoinMoin.security import Permissions
1.6 from MoinMoin.wikiutil import escape
1.7 -from MoinSupport import getPagesForSearch, getPagesFromResults, ActionSupport
1.8 +from MoinMoin.util import lock
1.9 +from MoinSupport import ActionSupport
1.10 +from SubpageComments import get_comment_numbers
1.11
1.12 __version__ = "0.1"
1.13
1.14 @@ -69,59 +71,53 @@
1.15 if not request.user.valid or not request.user.may.write(self.pagename):
1.16 return 0, _("You are not allowed to comment on this page.")
1.17
1.18 - # Determine the last comment.
1.19 -
1.20 - comments = get_comment_numbers(self.pagename, request)
1.21 - last_comment_pagename = comments and comments[-1] or -1
1.22 + # Obtain a lock to prevent contention over comment numbers.
1.23
1.24 - # Write the new page.
1.25 -
1.26 - comment_pagename = "%s/%04d" % (self.pagename, last_comment_pagename + 1)
1.27 - new_page = PageEditor(request, comment_pagename)
1.28 - username = request.user.name
1.29 + writelock = lock.WriteLock(self.page.getPagePath("comment-lock"))
1.30 + writelock.acquire()
1.31
1.32 try:
1.33 - # To add a page with an ACL, a special policy is required.
1.34 + # Determine the last comment.
1.35 +
1.36 + comments = get_comment_numbers(self.pagename, request)
1.37 + last_comment_pagename = comments and comments[-1] or -1
1.38
1.39 - may = request.user.may
1.40 - request.user.may = SpecialPermissions(request.user, comment_pagename)
1.41 + # Write the new page.
1.42
1.43 - # Save the page, labelling it with the actual username.
1.44 + comment_pagename = "%s/%04d" % (self.pagename, last_comment_pagename + 1)
1.45 + new_page = PageEditor(request, comment_pagename)
1.46 + username = request.user.name
1.47
1.48 try:
1.49 - new_page.saveText(comment_template % (username, username, comment), 0)
1.50 + # To add a page with an ACL, a special policy is required.
1.51 +
1.52 + may = request.user.may
1.53 + request.user.may = SpecialPermissions(request.user, comment_pagename)
1.54
1.55 - # Restore the superusers.
1.56 + # Save the page, labelling it with the actual username.
1.57 +
1.58 + try:
1.59 + new_page.saveText(comment_template % (username, username, comment), 0)
1.60 +
1.61 + # Restore the original policy.
1.62
1.63 - finally:
1.64 - request.user.may = may
1.65 + finally:
1.66 + request.user.may = may
1.67
1.68 - return 1, _("Comment added.")
1.69 - except new_page.SaveError, exc:
1.70 - return 0, unicode(exc)
1.71 + return 1, _("Comment added.")
1.72 + except new_page.SaveError, exc:
1.73 + return 0, unicode(exc)
1.74 +
1.75 + # Release the lock and let others post comments now.
1.76 +
1.77 + finally:
1.78 + writelock.release()
1.79
1.80 comment_template = """\
1.81 #acl %s:read,write,delete,revert All:read
1.82 #pragma comment-owner %s
1.83 %s"""
1.84
1.85 -def get_comment_numbers(pagename, request):
1.86 -
1.87 - """
1.88 - Return a list of comment numbers associated with the given 'pagename', using
1.89 - the 'request' provided.
1.90 - """
1.91 -
1.92 - pagenames = []
1.93 -
1.94 - for page in getPagesForSearch("title:regex:^%s/" % pagename, request):
1.95 - basename, number = page.page_name.rsplit("/", 1)
1.96 - if basename == pagename and number.isdigit():
1.97 - pagenames.append(int(number))
1.98 -
1.99 - pagenames.sort()
1.100 - return pagenames
1.101 -
1.102 # Action invocation function.
1.103
1.104 def execute(pagename, request):