summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2010-07-27 18:50:31 -0500
committerDan McGee <dan@archlinux.org>2010-07-27 18:50:31 -0500
commit90b79e3bb6284b897054cf2b3d3f7dd7435fe077 (patch)
tree9f0726f052468e796949beb336fd09851a25018d
parent2dbd802c325ae9333a06c325aeb2c7dcb0d9cab1 (diff)
downloadwebsite-90b79e3bb6284b897054cf2b3d3f7dd7435fe077.tar.gz
website-90b79e3bb6284b897054cf2b3d3f7dd7435fe077.zip
Move bulk of akismet prep into a new module
This will allow me to add some ham/spam marker methods as admin actions without duplicating a ton of code. Signed-off-by: Dan McGee <dan@archlinux.org>
-rw-r--r--mycomments/signals.py24
-rw-r--r--mycomments/utils.py50
2 files changed, 55 insertions, 19 deletions
diff --git a/mycomments/signals.py b/mycomments/signals.py
index ff58fd6..6fe7fac 100644
--- a/mycomments/signals.py
+++ b/mycomments/signals.py
@@ -1,10 +1,9 @@
-from django.conf import settings
from django.contrib.sites.models import Site
from django.template import Context, Template
-from django.utils.encoding import smart_str
from blog.models import Post
from mycomments.models import MyComment, BannedIP, BannedUserName
+from mycomments.utils import akismet_check
SUBJECT_TEMPLATE = Template(
'New comment for post \
@@ -70,22 +69,9 @@ def check_spam_comment(sender, **kwargs):
return False
# run our Akismet comment checking for things that passed initial check
- akismet_api = Akismet(key=settings.AKISMET_API_KEY,
- blog_url='http://%s/' % Site.objects.get_current().domain)
- if akismet_api.verify_key():
- akismet_data = {
- 'comment_type': 'comment',
- 'comment_author': smart_str(comment.user_name),
- 'comment_author_email': smart_str(comment.user_email),
- 'comment_author_url': smart_str(comment.user_url),
- 'referrer': request.META['HTTP_REFERER'],
- 'user_agent': request.META['HTTP_USER_AGENT'],
- 'user_ip': request.META['REMOTE_ADDR'],
- }
- spam = akismet_api.comment_check(smart_str(comment.comment),
- data=akismet_data, build_data=True)
- if spam:
- # mark the comment as non-public/bogus
- comment.is_public = False
+ spam = akismet_check(request, comment)
+ if spam:
+ # mark the comment as non-public/bogus
+ comment.is_public = False
# vim: set ts=4 sw=4 et:
diff --git a/mycomments/utils.py b/mycomments/utils.py
new file mode 100644
index 0000000..7234ee7
--- /dev/null
+++ b/mycomments/utils.py
@@ -0,0 +1,50 @@
+'''
+This module contains utility methods for comments. Most deal with Akismet
+interaction so it is imported unconditionally when this module is loaded.
+'''
+from django.conf import settings
+from django.contrib.sites.models import Site
+from django.utils.encoding import smart_str
+
+from akismet import Akismet
+
+def prepare_akismet(request, comment):
+ akismet_api = Akismet(key=settings.AKISMET_API_KEY,
+ blog_url='http://%s/' % Site.objects.get_current().domain)
+ if akismet_api.verify_key():
+ akismet_data = {
+ 'comment_type': 'comment',
+ 'comment_author': smart_str(comment.user_name),
+ 'comment_author_email': smart_str(comment.user_email),
+ 'comment_author_url': smart_str(comment.user_url),
+ 'referrer': request.META['HTTP_REFERER'],
+ 'user_agent': request.META['HTTP_USER_AGENT'],
+ 'user_ip': request.META['REMOTE_ADDR'],
+ }
+ return (akismet_api, akismet_data)
+ return (None, None)
+
+def akismet_check(request, comment):
+ '''
+ Check a comment to see if it is likely spam. Returns True
+ if so. If your Akismet key didn't validate, return False.
+ '''
+ api, data = prepare_akismet(request, comment)
+ if api:
+ return api.comment_check(smart_str(comment.comment),
+ data=data, build_data=True)
+ return False
+
+def akismet_ham(request, comment):
+ api, data = prepare_akismet(request, comment)
+ if api:
+ api.submit_ham(smart_str(comment.comment),
+ data=data, build_data=True)
+
+def akismet_spam(request, comment):
+ api, data = prepare_akismet(request, comment)
+ if api:
+ api.submit_spam(smart_str(comment.comment),
+ data=data, build_data=True)
+
+# vim: set ts=4 sw=4 et: