Trac Wiki Comments

I really enjoy hacking Trac. I’ve just recently added commenting functionality to Trac wiki pages. I have two tabs at the top of every wiki page which I added in the wiki.cs template. They are similar to how Wikipedia does it. At the bottom of ever wiki page I have a “Add Comment” buttom which just triggers a JavaScript function that toggles the display of a hidden comment form and hides the original “Add Comment” button using onClick. You type your comment in and hit the newly displayed “Submit Comment” button which does something similar to the “Add to DS” button I added previously. It adds the comment along with the date and username to the Comments page. Here’s the Python code:


if addComment:
            self.perm.assert_permission (perm.WIKI_MODIFY)
            self.req.hdf.setValue('wiki.action', 'addComment')
            self.CommentPage = WikiPage(self.page.name + "Comments", version, self.perm, self.db)

            if self.CommentPage.text.startswith("describe"):
               self.CommentPage.set_content("\n== Comments ==\n\n" + " * " +
                             datetime.datetime.now().strftime("(%d/%m/%Y %H:%M:%S)") +
                             " " + author + " - " + comment + "\n")
            else:
               self.CommentPage.set_content(self.CommentPage.text + " * " +
                              datetime.datetime.now().strftime("(%d/%m/%Y %H:%M:%S)") +
                              " " + author + " - " + comment + "\n")
            self.CommentPage.commit(author, comment, self.req.remote_addr)
            self.req.redirect(self.env.href.wiki(self.page.name + "Comments"))

I’m not 100% sure this will work as is because of the line wrapping I had to do to make this post display nicely.

Leave a Reply

You must be logged in to post a comment.