Permalink
Browse files

adding 'long_enough' built-in spam-checker

  • Loading branch information...
1 parent 55d249a commit eb5e6ed785754073dd324332f817a48a8915da77 @Fantomas42 committed May 20, 2012
Showing with 54 additions and 1 deletion.
  1. +2 −0 zinnia/settings.py
  2. +14 −0 zinnia/spam_checker/backends/long_enough.py
  3. +2 −1 zinnia/tests/__init__.py
  4. +36 −0 zinnia/tests/long_enough.py
View
@@ -43,6 +43,8 @@
settings, 'ZINNIA_MAIL_COMMENT_NOTIFICATION_RECIPIENTS',
[manager_tuple[1] for manager_tuple in settings.MANAGERS])
+COMMENT_MIN_WORDS = getattr(settings, 'ZINNIA_MIN_WORDS', 4)
+
UPLOAD_TO = getattr(settings, 'ZINNIA_UPLOAD_TO', 'uploads/zinnia')
PROTOCOL = getattr(settings, 'ZINNIA_PROTOCOL', 'http')
@@ -0,0 +1,14 @@
+"""Long enough spam checker backend for Zinnia"""
+from zinnia.settings import COMMENT_MIN_WORDS
+
+
+def backend(comment, content_object, request):
+ """Backend checking if the comment posted is long enough to be public.
+ Generally a comments with few words is useless. The will avoid
+ comments like this :
+
+ - First !
+ - I don't like.
+ - Check http://spam-ads.com/
+ """
+ return len(comment.comment.split()) < COMMENT_MIN_WORDS
@@ -25,6 +25,7 @@
from zinnia.tests.moderator import EntryCommentModeratorTestCase
from zinnia.tests.spam_checker import SpamCheckerTestCase
from zinnia.tests.url_shortener import URLShortenerTestCase
+from zinnia.tests.long_enough import LongEnoughTestCase
from zinnia.tests.mixins import MixinTestCase
from zinnia.signals import disconnect_zinnia_signals
@@ -44,7 +45,7 @@ def suite():
URLShortenerTestCase, EntryCommentModeratorTestCase,
ZinniaCustomDetailViews, SpamCheckerTestCase,
EntryAdminTestCase, CategoryAdminTestCase,
- MixinTestCase)
+ MixinTestCase, LongEnoughTestCase)
if 'django_xmlrpc' in settings.INSTALLED_APPS:
test_cases += (PingBackTestCase, MetaWeblogTestCase)
@@ -0,0 +1,36 @@
+"""Test cases for Zinnia's long_enought spam checker"""
+from django.test import TestCase
+from django.contrib import comments
+from django.contrib.auth.models import User
+from django.contrib.sites.models import Site
+
+from zinnia.models import Entry
+from zinnia.managers import PUBLISHED
+from zinnia.spam_checker.backends.long_enough import backend
+
+
+class LongEnoughTestCase(TestCase):
+ """Test cases for zinnia.spam_checker.long_enough"""
+
+ def setUp(self):
+ self.site = Site.objects.get_current()
+ self.author = User.objects.create(username='admin',
+ email='admin@example.com')
+
+ params = {'title': 'My test entry',
+ 'content': 'My test entry',
+ 'slug': 'my-test-entry',
+ 'status': PUBLISHED}
+ self.entry = Entry.objects.create(**params)
+ self.entry.sites.add(self.site)
+ self.entry.authors.add(self.author)
+
+ def test_long_enough(self):
+ comment = comments.get_model().objects.create(
+ comment='My Comment', user=self.author, is_public=True,
+ content_object=self.entry, site=self.site)
+ self.assertEquals(backend(comment, self.entry, {}), True)
+
+ comment.comment = 'Hello I just wanted to thank for great article'
+ comment.save()
+ self.assertEquals(backend(comment, self.entry, {}), False)

0 comments on commit eb5e6ed

Please sign in to comment.