bfa2c0882ce6

Added Akismet spam filtering for comments.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Fri, 23 Jan 2009 23:51:07 -0500
parents 9a43a0ef2602
children 4ee184bf2f39
branches/tags (none)
files blog/views.py deploy-template.py projects/views.py

Changes

--- a/blog/views.py	Fri Jan 23 23:02:53 2009 -0500
+++ b/blog/views.py	Fri Jan 23 23:51:07 2009 -0500
@@ -3,8 +3,10 @@
 from django.shortcuts import get_object_or_404, render_to_response
 from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect
 from django.core.urlresolvers import reverse
+from akismet import Akismet
+import deploy
 
-
+ak = Akismet(deploy.AKISMET_API_KEY, blog_url='http://stevelosh.com/')
 ENTRIES_PER_PAGE = 10
 
 def entry(request, year, month, day, slug):
@@ -34,11 +36,25 @@
     fields = request.POST
     entry = Entry.objects.get(pk=fields['entry-id'])
     
-    if ( fields.has_key('name') and (not fields['name'].strip() == '') and
-         fields.has_key('body') and not fields['body'].strip() == ''):
+    if ( fields.has_key('name') and 
+         not fields['name'].strip() == '' and
+         not len(fields['name']) < 3 and
+         fields.has_key('body') and 
+         not fields['body'].strip() == '' and
+         not len(fields['body']) < 3 and
+         not len(fields['body']) > 15000):
+        
+        akismet_data = {}
+        akismet_data['user_ip'] = request.META['REMOTE_ADDR']
+        akismet_data['user_agent'] = request.META['HTTP_USER_AGENT']
+        akismet_data['comment_author'] = fields['name']
+        akismet_data['comment_type'] ='comment'
+        spam = ak.comment_check(fields['body'], akismet_data)
+        
         new_comment = Comment(name=fields['name'], 
                               body=fields['body'], 
-                              entry=entry)
+                              entry=entry,
+                              spam=spam)
         new_comment.save()
     
     return HttpResponseRedirect(reverse('stevelosh.blog.views.entry',
--- a/deploy-template.py	Fri Jan 23 23:02:53 2009 -0500
+++ b/deploy-template.py	Fri Jan 23 23:51:07 2009 -0500
@@ -18,3 +18,5 @@
 
 # This is used for the Mint (http://haveamint.com/) Bird Feeder plugin hack.
 FEEDER_PASSWORD = ''
+
+AKISMET_API_KEY = ''
--- a/projects/views.py	Fri Jan 23 23:02:53 2009 -0500
+++ b/projects/views.py	Fri Jan 23 23:51:07 2009 -0500
@@ -3,8 +3,12 @@
 from django.shortcuts import render_to_response, get_object_or_404
 from django.http import HttpResponseRedirect
 from django.core.urlresolvers import reverse
+from akismet import Akismet
+import deploy
 
 
+ak = Akismet(deploy.AKISMET_API_KEY, blog_url='http://stevelosh.com/')
+
 def project(request, slug):
     project = get_object_or_404(Project, slug=slug)
     comments = project.comment_set.filter(spam=False).order_by('submitted')
@@ -27,9 +31,18 @@
          not fields['body'].strip() == '' and
          not len(fields['body']) < 3 and
          not len(fields['body']) > 15000):
+        
+        akismet_data = {}
+        akismet_data['user_ip'] = request.META['REMOTE_ADDR']
+        akismet_data['user_agent'] = request.META['HTTP_USER_AGENT']
+        akismet_data['comment_author'] = fields['name']
+        akismet_data['comment_type'] ='comment'
+        spam = ak.comment_check(fields['body'], akismet_data)
+        
         new_comment = Comment(name=fields['name'], 
                               body=fields['body'], 
-                              project=project)
+                              project=project,
+                              spam=spam)
         new_comment.save()
     
     return HttpResponseRedirect(reverse('stevelosh.projects.views.project',