diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/antispam/utils.py	Fri May 14 02:19:48 2010 +0000
@@ -0,0 +1,31 @@
+"""Antispam utility functions other apps can use."""
+from django.core.cache import cache
+
+from antispam import SPAM_PHRASE_KEY
+from antispam.models import SpamPhrase
+
+
+def contains_spam(s):
+    """This function returns True if the supplied string s contains any spam
+    phrases and False otherwise.
+    """
+    phrases = _get_spam_phrases()
+    for spam in phrases:
+        if spam in s:
+            return True
+
+    return False
+
+
+def _get_spam_phrases():
+    """This function returns the current list of spam phrase strings.
+    The strings are cached to avoid hitting the database.
+    """
+    phrases = cache.get(SPAM_PHRASE_KEY)
+    if phrases:
+        return phrases
+
+    phrases = SpamPhrase.objects.values_list('phrase', flat=True)
+    cache.set(SPAM_PHRASE_KEY, phrases)
+    return phrases
+