Changeset 21

Show
Ignore:
Timestamp:
03/03/05 23:49:25 (4 years ago)
Author:
athomas
Message:

Closes #7, #8, #9

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/macros/AddComment.py

    r9 r21  
    1515    page = WikiPage(pagename, None, perm, db) 
    1616    wikipreview = hdf.getValue("args.preview", "") 
    17     readonly = int(hdf.getValue("wiki.readonly", "0")) 
     17    appendonly = (args == 'appendonly') 
     18    readonlypage = int(hdf.getValue("wiki.readonly", "0")) 
     19    # Can this user add a comment to this page? 
     20    cancomment = not readonlypage 
     21    # Is this an "append-only" comment or are we an administrator? 
     22    if perm.has_permission(trac.perm.WIKI_ADMIN) or appendonly: 
     23        cancomment = True 
    1824 
    19     if readonly and not perm.has_permission(trac.perm.WIKI_ADMIN)
     25    if not cancomment
    2026        raise TracError('Error: Insufficient privileges to AddComment') 
    2127 
     
    2834    # Ensure [[AddComment]] is not present in comment, so that infinite 
    2935    # recursion does not occur. 
    30     comment = re.sub('(^|[^!])(\[\[AddComment\]\])', '\\1!\\2', comment) 
     36    comment = re.sub('(^|[^!])(\[\[AddComment)', '\\1!\\2', comment) 
    3137 
    3238    out = StringIO() 
    3339    if wikipreview or not perm.has_permission(trac.perm.WIKI_MODIFY): 
    34         disabled = ' disabled
     40        disabled = ' disabled="disabled"
    3541 
    3642    # If we are submitting or previewing, inject comment as it should look 
    37     if comment and (preview or submit): 
     43    if cancomment and comment and (preview or submit): 
    3844        if preview: 
    3945            out.write("<div class='wikipage' id='preview'>\n") 
    40         out.write("<h4>Comment by %s on %s</h4>\n<p>\n%s\n</p>\n" % (authname, time.strftime('%c', time.localtime()), wiki_to_html(comment, hdf, env, db))) 
     46        out.write("<h4 id='commentpreview'>Comment by %s on %s</h4>\n<p>\n%s\n</p>\n" % (authname, time.strftime('%c', time.localtime()), wiki_to_html(comment, hdf, env, db))) 
    4147        if preview: 
    4248            out.write("</div>\n") 
     
    4450    # When submitting, inject comment before macro 
    4551    if comment and submit: 
     52        submitted = False 
    4653        newtext = StringIO() 
    4754        for line in page.text.splitlines(): 
    4855            if line.find('[[AddComment') == 0: 
    4956                newtext.write("==== Comment by %s on %s ====\n%s\n\n" % (authname, time.strftime('%c', time.localtime()), comment)) 
     57                submitted = True 
    5058            newtext.write(line + "\n") 
    51         page.set_content(newtext.getvalue()) 
    52         # TODO: How do we get remote_addr from a macro? 
    53         page.commit(authname, 'Comment added', None) 
    54         comment = "" 
     59        if submitted: 
     60            # XXX Is this the dodigest hack ever? This is needed in  
     61            # "appendonly" mode when the page is readonly. XXX 
     62            if appendonly: 
     63                perm.expand_meta_permission('WIKI_ADMIN'); 
     64            page.set_content(newtext.getvalue()) 
     65            # TODO: How do we get remote_addr from a macro? 
     66            page.commit(authname, 'Comment added', None) 
     67            comment = "" 
     68        else: 
     69            out.write("<div class='system-message'><strong>ERROR: [[AddComment]] macro call must be the only content on its line. Could not add comment.</strong></div>\n") 
    5570 
    56     out.write("<form name='addcomment' action='%s' method='post'>\n" % env.href.wiki(pagename)) 
     71    out.write("<form action='%s#commentpreview' method='post'>\n" % env.href.wiki(pagename)) 
    5772    out.write("<fieldset>\n<legend>Add comment</legend>\n") 
    58     out.write("<textarea id='addcomment' name='addcomment' cols='80' rows='5' wrap='soft'%s>" % disabled) 
     73    out.write("<textarea id='addcomment' name='addcomment' cols='80' rows='5'%s>" % disabled) 
    5974    if wikipreview: 
    60         out.write("Preview...") 
     75        out.write("Page preview...") 
    6176    elif not cancel: 
    6277        out.write(comment) 
    6378    out.write("</textarea>\n") 
    64     out.write("<br>\n") 
    65     out.write("<input type='submit' name='submitaddcomment' value='Add comment'%s>\n" % disabled) 
    66     out.write("<input type='submit' name='previewaddcomment' value='Preview comment'%s>\n" % disabled) 
    67     out.write("<input type='submit' name='canceladdcomment' value='Cancel'%s>\n" % disabled) 
     79    out.write("<br/>\n") 
     80    out.write("<input type='submit' name='submitaddcomment' value='Add comment'%s/>\n" % disabled) 
     81    out.write("<input type='submit' name='previewaddcomment' value='Preview comment'%s/>\n" % disabled) 
     82    out.write("<input type='submit' name='canceladdcomment' value='Cancel'%s/>\n" % disabled) 
    6883    out.write("<script type='text/javascript'>\naddWikiFormattingToolbar(document.getElementById('addcomment'));\n</script>\n") 
    6984    out.write("</fieldset>\n</form>\n")