Login

Django Akismet

Author:
yeago
Posted:
December 19, 2008
Language:
Python
Version:
1.0
Tags:
akismet
Score:
0 (after 0 ratings)

http://sciyoshi.com/blog/2008/aug/27/using-akismet-djangos-new-comments-framework/

 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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):
                        if hasattr(comment.content_object,'author'):
                                user = comment.content_object.author
                        else:
                                from django.contrib.auth.models import User
                                user = User.objects.filter(is_superuser=True)[0]

                        comment.flags.create(
                                user=user,
                                flag='spam'
                        )
                        comment.is_public = False
                        comment.save()

comment_was_posted.connect(on_comment_was_posted)

More like this

  1. Using Akismet/TypePad AntiSpam with Django's new comments framework by sciyoshi 6 years, 11 months ago
  2. Clean spam from comments by running management command and akismet by iElectric 2 years, 9 months ago
  3. Prevent Django newcomments spam with Akismet (reloaded) by sciyoshi 6 years ago
  4. LinkSleeve comment moderation by zgoda 7 years, 3 months ago
  5. Unobtrusive comment moderation by ubernostrum 8 years, 4 months ago

Comments

Please login first before commenting.