# HG changeset patch # User Brian Neal # Date 1273803588 0 # Node ID 28988cce138bddf60b0f193ae1fbb50a486124e0 # Parent 65016249bf35f1a271d73b2c63c2d4c8cb085879 Implement #83: a string filter facility like NukeSeSentinel. It currently isn't hooked up to anything. Will do that in #84. diff -r 65016249bf35 -r 28988cce138b gpp/antispam/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/antispam/__init__.py Fri May 14 02:19:48 2010 +0000 @@ -0,0 +1,1 @@ +SPAM_PHRASE_KEY = "antispam.spam_phrases" diff -r 65016249bf35 -r 28988cce138b gpp/antispam/admin.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/antispam/admin.py Fri May 14 02:19:48 2010 +0000 @@ -0,0 +1,12 @@ +"""Admin definitions for the antispam application.""" + +from django.contrib import admin + +from antispam.models import SpamPhrase + + +class SpamPhraseAdmin(admin.ModelAdmin): + search_fields = ('phrase', ) + + +admin.site.register(SpamPhrase, SpamPhraseAdmin) diff -r 65016249bf35 -r 28988cce138b gpp/antispam/models.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/antispam/models.py Fri May 14 02:19:48 2010 +0000 @@ -0,0 +1,22 @@ +"""Models for the antispam application.""" +from django.db import models +from django.core.cache import cache + +from antispam import SPAM_PHRASE_KEY + + +class SpamPhrase(models.Model): + """A SpamPhrase is a string that is checked for in user input. User input + containing a SpamPhrase should be blocked and flagged. + """ + phrase = models.CharField(max_length=64) + + class Meta: + ordering = ('phrase', ) + + def __unicode__(self): + return self.phrase + + def save(self, *args, **kwargs): + cache.delete(SPAM_PHRASE_KEY) + super(SpamPhrase, self).save(*args, **kwargs) diff -r 65016249bf35 -r 28988cce138b gpp/antispam/tests.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/antispam/tests.py Fri May 14 02:19:48 2010 +0000 @@ -0,0 +1,37 @@ +""" +Tests for the antispam application. +""" +from django.test import TestCase +from django.core.cache import cache + +from antispam import SPAM_PHRASE_KEY +from antispam.models import SpamPhrase +from antispam.utils import contains_spam + + +class AntispamCase(TestCase): + + def test_no_phrases(self): + """ + Tests that an empty spam phrase table works. + """ + cache.delete(SPAM_PHRASE_KEY) + self.assertFalse(contains_spam("Here is some random text.")) + + def test_phrases(self): + """ + Simple test of some phrases. + """ + SpamPhrase.objects.create(phrase="grytner") + SpamPhrase.objects.create(phrase="allday.ru") + SpamPhrase.objects.create(phrase="stefa.pl") + + self.assert_(contains_spam("grytner")) + self.assert_(contains_spam("11grytner")) + self.assert_(contains_spam("11grytner>")) + self.assert_(contains_spam("1djkl jsd stefa.pl")) + self.assert_(contains_spam("1djkl jsd