# vim: expandtab from trac.core import * from trac.wiki.macros import WikiMacroBase import trac.perm from StringIO import StringIO from trac.wiki.formatter import wiki_to_html from trac.wiki.model import WikiPage from trac.util import Markup from trac.util import TracError from trac.web.chrome import add_link from trac.util.text import to_unicode import re, time class AddCommentMacro(WikiMacroBase): """A macro to add comments to a page.""" def render_macro(self, req, name, content): return execute(req.hdf, content, self.env) def _render_macro(self, req, name, content): pass # Fill in a deuglified version here def process_macro_post(self, req): self.log.debug('AddCommentMacro: Got a POST') def execute(hdf, args, env): # prevents from multiple inclusions if hdf.has_key('addcommentmacro'): raise TracError('\'AddComment\' macro cannot be included twice') hdf['addcommentmacro'] = True authname = to_unicode(hdf.getValue("trac.authname", "anonymous")) db = env.get_db_cnx() perm = trac.perm.PermissionCache(env, authname) pagename = to_unicode(hdf.getValue("wiki.page_name", "WikiStart")) page = WikiPage(env, pagename, None, db) wikipreview = hdf.getValue("wiki.preview", "") appendonly = (args == 'appendonly') readonlypage = int(hdf.getValue("wiki.readonly", "0")) # Can this user add a comment to this page? cancomment = not readonlypage # Is this an "append-only" comment or are we an administrator? if perm.has_permission('WIKI_ADMIN') or appendonly: cancomment = True if not cancomment: raise TracError('Error: Insufficient privileges to AddComment') disabled = '' comment = Markup(to_unicode(hdf.getValue("args.addcomment", ""))).unescape() preview = hdf.getValue("args.previewaddcomment", "") cancel = hdf.getValue("args.canceladdcomment", "") submit = hdf.getValue("args.submitaddcomment", "") if not cancel: authname = to_unicode(hdf.getValue("args.authoraddcomment", authname)) # Ensure [[AddComment]] is not present in comment, so that infinite # recursion does not occur. comment = re.sub('(^|[^!])(\[\[AddComment)', '\\1!\\2', comment) out = StringIO() if wikipreview or not (perm.has_permission('WIKI_MODIFY') or appendonly): disabled = ' disabled="disabled"' # If we are submitting or previewing, inject comment as it should look if cancomment and comment and (preview or submit): if preview: out.write("
\n%s\n
\n" % ( authname, to_unicode(time.strftime('%c', time.localtime())), wiki_to_html(comment, env, None))) if preview: out.write("" + hdf.dump() + ""