Mercurial > public > sg101
annotate gpp/antispam/utils.py @ 214:28988cce138b
Implement #83: a string filter facility like NukeSeSentinel. It currently isn't hooked up to anything. Will do that in #84.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Fri, 14 May 2010 02:19:48 +0000 |
parents | |
children | 8c1832b9d815 |
rev | line source |
---|---|
bgneal@214 | 1 """Antispam utility functions other apps can use.""" |
bgneal@214 | 2 from django.core.cache import cache |
bgneal@214 | 3 |
bgneal@214 | 4 from antispam import SPAM_PHRASE_KEY |
bgneal@214 | 5 from antispam.models import SpamPhrase |
bgneal@214 | 6 |
bgneal@214 | 7 |
bgneal@214 | 8 def contains_spam(s): |
bgneal@214 | 9 """This function returns True if the supplied string s contains any spam |
bgneal@214 | 10 phrases and False otherwise. |
bgneal@214 | 11 """ |
bgneal@214 | 12 phrases = _get_spam_phrases() |
bgneal@214 | 13 for spam in phrases: |
bgneal@214 | 14 if spam in s: |
bgneal@214 | 15 return True |
bgneal@214 | 16 |
bgneal@214 | 17 return False |
bgneal@214 | 18 |
bgneal@214 | 19 |
bgneal@214 | 20 def _get_spam_phrases(): |
bgneal@214 | 21 """This function returns the current list of spam phrase strings. |
bgneal@214 | 22 The strings are cached to avoid hitting the database. |
bgneal@214 | 23 """ |
bgneal@214 | 24 phrases = cache.get(SPAM_PHRASE_KEY) |
bgneal@214 | 25 if phrases: |
bgneal@214 | 26 return phrases |
bgneal@214 | 27 |
bgneal@214 | 28 phrases = SpamPhrase.objects.values_list('phrase', flat=True) |
bgneal@214 | 29 cache.set(SPAM_PHRASE_KEY, phrases) |
bgneal@214 | 30 return phrases |
bgneal@214 | 31 |