Hacking comments without hacking comments
Way back when I first set up this site, and threw together a minimal blog application to power it, I didn’t bother much with enhancing Django’s comment system. But later on, when I started getting a lot of comment spam (trivia: as of this moment, 15,153 comments have been posted here, and 14,406 of them — roughly 95% — have been spam) I realized I needed to do something, and quickly hacked Django’s comments application to add moderation.
At the time it seemed a reasonable thing to do, but it’s been haunting me ever since; having to port a bunch of hacks whenever I want to upgrade Django is a pain. A while back I posted the beginnings of an unobtrusive moderation system using signals, but it wasn’t everything I wanted it to be, and as I worked on rewriting my blog application I found myself writing the same or similar code in multiple models in order to work with it. Any time I’m in that situation, I stop, step back and break out that code into its own module or application, and usually do my best to make it more generic and reusable.
The result, this time around, is comment_utils, an open-source library of useful tools that build on Django’s bundled comments application.
The big feature for this app is a generic, unobtrusive, extensible system for comment moderation which includes the ability to turn on any or all of the following options, on a per-model basis:
- Akismet spam filtering
- Auto-closing comments a certain number of days after an object’s publication
- Auto-moderating comments a certain number of days after an object’s publication
- Auto-moderating all comments
- Auto-moderating a new commenter’s first posts until one of them is approved
-
Automatically enabling/disabling comments for an object via a
BooleanField
on the model - Automatically emailing you whenever a comment is posted on an object
All of this is handled with as little work as possible; Akismet filtering, for example, can be turned on with two lines of code. Also included is the ability to add your own custom moderation methods whenever and wherever you need them. See the documentation for all the gory details, and examples of how the system works.
Also bundled with comment_utils
:
-
The moderation-oriented template tags which began life as part of
template_utils
. - A custom manager which can figure out the “most-commented” objects of any model it’s used on.
I’ve done some basic testing and verified that everything at least works on my local setup with an application I’ve been working on but, this being the first time the code’s seen the public light of day, bug reports would be appreciated.