Login

Clean spam from comments by running management command and akismet

Author:
iElectric
Posted:
October 20, 2012
Language:
Python
Version:
Not specified
Tags:
akismet spam comments command
Score:
3 (after 3 ratings)

..

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from akismet import Akismet
from django.core.management.base import BaseCommand
from django.conf import settings
from django.contrib.sites.models import Site
from django.contrib.comments.models import Comment



class Command(BaseCommand):
    args = "<date> in format dd.mm.yyyy"
    help = "Sends daily email repot about intranet changes"

    def handle(self, *args, **options):
        akismet = Akismet(key=settings.SPAMINSPECTOR_AKISMET_KEY,
                          blog_url="http://%s/" % Site.objects.get(pk=settings.SITE_ID).domain)
        if not akismet.verify_key():
            raise ValueError('no key set')

        for comment in Comment.objects.all():
            data = {
            'user_ip': comment.ip_address,
            'user_agent': '',
            'referrer': '',
            'comment_type': 'comment',
            'comment_author': comment.user_name.encode('utf-8'),
            'comment_email': comment.user_email.encode('utf-8'),
            'comment_url': comment.user_url.encode('utf-8'),
            }
            if akismet.comment_check(comment.comment.encode('utf-8'), data, build_data=True):
                 comment.delete()

More like this

  1. Using Akismet/TypePad AntiSpam with Django's new comments framework by sciyoshi 7 years ago
  2. Akismet Webservice by sneeu 8 years, 5 months ago
  3. Prevent Django newcomments spam with Akismet (reloaded) by sciyoshi 6 years, 1 month ago
  4. Django Akismet by yeago 6 years, 8 months ago
  5. Unobtrusive comment moderation by ubernostrum 8 years, 5 months ago

Comments

vdboor (on November 12, 2012):

Neat idea! :-)

#

Please login first before commenting.