Skip to content

Commit

Permalink
adding 'long_enough' built-in spam-checker
Browse files Browse the repository at this point in the history
  • Loading branch information
Fantomas42 committed May 20, 2012
1 parent 55d249a commit eb5e6ed
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
2 changes: 2 additions & 0 deletions zinnia/settings.py
Expand Up @@ -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')
Expand Down
14 changes: 14 additions & 0 deletions zinnia/spam_checker/backends/long_enough.py
@@ -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
3 changes: 2 additions & 1 deletion zinnia/tests/__init__.py
Expand Up @@ -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

Expand All @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions zinnia/tests/long_enough.py
@@ -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.