- Author:
- shimonrura
- Posted:
- January 21, 2009
- Language:
- Python
- Version:
- 1.0
- Score:
- 4 (after 4 ratings)
This is the "unobtrusive comments moderation" code from http://www.djangosnippets.org/snippets/112/ , but updated so it works properly with Django 1.0.
There are only a few small changes reflecting changes in the comments, contenttypes, and signals APIs.
For full background, see the original snippet: http://www.djangosnippets.org/snippets/112/
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 | from django.conf import settings
from django.db.models import signals
from django.contrib.comments.models import Comment
from django.contrib.sites.models import Site
def moderate_comments(instance, raw, **kwargs_ignored):
"""
Applies comment moderation to newly-posted comments.
Moderation happens in two phases:
1. If the object the comment is being posted on has a method
named ``comments_open``, it will be called; if the return
value evaluates to ``False``, the comment's ``is_public``
field will be set to ``False`` and no further processing
will be done.
2. If the object did not have a ``comments_open`` method, or
if that method's return value evaluated to ``True``, then
the comment will be submitted to Akismet for a spam check,
and if Akismet thinks the comment is spam, then its
``is_public`` field will be set to ``False``.
"""
if not instance.id: # Only check when the comment is first saved.
content_object = instance.content_object
comments_open = getattr(content_object, 'comments_open', None)
if callable(comments_open) and not comments_open():
instance.is_public = False
elif hasattr(settings, 'AKISMET_API_KEY') and settings.AKISMET_API_KEY:
from akismet import Akismet
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',
'referrer': '',
'user_ip': instance.ip_address,
'user_agent': '' }
if akismet_api.comment_check(instance.comment, data=akismet_data, build_data=True):
instance.is_public = False
signals.pre_save.connect(moderate_comments, sender=Comment)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 8 months ago
Comments
Please login first before commenting.