Login

Clean spam from comments by running management command and akismet

Author:
iElectric
Posted:
October 20, 2012
Language:
Python
Version:
Not specified
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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 3 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

vdboor (on November 12, 2012):

Neat idea! :-)

#

Please login first before commenting.