Prevent Django newcomments spam with Akismet (reloaded)

This is a rewrite of snippet #1006 to use the moderation features available in Django's comments framework. This is more customizable than the signals approach and works well if other moderation features are being used. If you want to make comments that are flagged as spam become hidden instead of deleted, change the allow() method to moderate(). See the blog post here

fromdjango.contrib.comments.moderationimportCommentModerator,moderatorfromdjango.contrib.sites.modelsimportSitefromdjango.confimportsettingsclassEntryModerator(CommentModerator):defcheck_spam(self,request,comment,key,blog_url=None,base_url=None):try:fromakismetimportAkismetexcept:returnFalseifblog_urlisNone:blog_url='http://%s/'%Site.objects.get_current().domainak=Akismet(key=settings.AKISMET_API_KEY,blog_url=blog_url)ifbase_urlisnotNone:ak.baseurl=base_urlifak.verify_key():data={'user_ip':request.META.get('REMOTE_ADDR','127.0.0.1'),'user_agent':request.META.get('HTTP_USER_AGENT',''),'referrer':request.META.get('HTTP_REFERER',''),'comment_type':'comment','comment_author':comment.user_name.encode('utf-8'),}ifak.comment_check(comment.comment.encode('utf-8'),data=data,build_data=True):returnTruereturnFalsedefallow(self,comment,content_object,request):allow=super(EntryModerator,self).allow(comment,content_object,request)# change this depending on which spam provider you want to usespam=self.check_spam(request,comment,key=settings.AKISMET_API_KEY,)orself.check_spam(request,comment,key=settings.TYPEPAD_ANTISPAM_API_KEY,base_url='api.antispam.typepad.com/1.1/')returnnotspamandallowmoderator.register(Entry,EntryModerator)