def moderate(self, comment, content_object, request): # return True means "moderated", i.e. mark non-public. # return False means not "moderated", i.e. let through and the public can now see it. # For now, assume that this is not akismet spam. comment.is_akismet_spam = False already_moderated = super(MaybeLessSpamCommentModerator, self).moderate(comment, content_object, request) if already_moderated: return True comment_string = comment.comment comment_string = comment_string.replace("\n", " ") comment_string = comment_string.replace("\r", " ") comment_string = " " + comment_string + " " bad_words_file_name = settings.BAD_WORDS_FILE bad_words_template = loader.get_template(bad_words_file_name) bad_words_string = bad_words_template.nodelist[0].render("") # Remove \n's from bad_words_string string bad_words_string = re.sub("\n", "", bad_words_string) bad_words_re = re.compile(bad_words_string, re.IGNORECASE) if bad_words_re.search(comment_string): return True the_current_domain = Site.objects.get_current().domain the_project_url = "http://%s" % the_current_domain akismet_api = Akismet(settings.AKISMET_API_KEY, the_project_url) if akismet_api.verify_key(): comment_author = request.REQUEST["name"] placeholder_comment_email = "*****@*****.**" # this forces Akimset to be more conservative akismet_data = { "blog": the_project_url, "comment_type": "comment", "referrer": request.META["HTTP_REFERER"], "user_ip": comment.ip_address, "user_agent": request.META["HTTP_USER_AGENT"], "comment_author": comment_author, "comment_author_email": placeholder_comment_email, } akismet_response = akismet_api.comment_check(smart_str(comment_string), data=akismet_data, build_data=True) comment.is_akismet_spam = akismet_response return akismet_response # If reached here, the Akismet API has failed to verify the key, so to be safe, assume that this *IS* spam. comment.is_akismet_spam = True return True
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 try: from akismet.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/' elif hasattr(settings, 'AKISMET_API_KEY'): ak = Akismet( key=settings.AKISMET_API_KEY, blog_url='http://%s/' % Site.objects.get(pk=settings.SITE_ID).domain ) else: ak = None if ak: 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'), 'comment_author_email': comment.user_email.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()