from django.contrib.comments.signals import comment_was_posted def on_comment_was_posted(sender, comment, request, *args, **kwargs): # spam checking can be enabled/disabled per the comment's target Model #if comment.content_type.model_class() != Entry: # return from django.contrib.sites.models import Site from django.conf import settings try: from akismet import Akismet except: return # use TypePad's AntiSpam if the key is specified in settings.py if hasattr(settings, TYPEPAD_ANTISPAM_API_KEY): ak = Akismet( key=settings.TYPEPAD_ANTISPAM_API_KEY, blog_url='http://%s/' % Site.objects.get(pk=settings.SITE_ID).domain ) ak.baseurl = 'api.antispam.typepad.com/1.1/' else: ak = Akismet( key=settings.AKISMET_API_KEY, blog_url='http://%s/' % Site.objects.get(pk=settings.SITE_ID).domain ) if ak.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'), } if ak.comment_check(comment.comment.encode('utf-8'), data=data, build_data=True): comment.flags.create( user=comment.content_object.author, flag='spam' ) comment.is_public = False comment.save() comment_was_posted.connect(on_comment_was_posted)