bgneal@214: """Antispam utility functions other apps can use.""" bgneal@214: from django.core.cache import cache bgneal@214: bgneal@214: from antispam import SPAM_PHRASE_KEY bgneal@214: from antispam.models import SpamPhrase bgneal@214: bgneal@214: bgneal@214: def contains_spam(s): bgneal@214: """This function returns True if the supplied string s contains any spam bgneal@214: phrases and False otherwise. bgneal@214: """ bgneal@214: phrases = _get_spam_phrases() bgneal@214: for spam in phrases: bgneal@214: if spam in s: bgneal@214: return True bgneal@214: bgneal@214: return False bgneal@214: bgneal@214: bgneal@214: def _get_spam_phrases(): bgneal@214: """This function returns the current list of spam phrase strings. bgneal@214: The strings are cached to avoid hitting the database. bgneal@214: """ bgneal@214: phrases = cache.get(SPAM_PHRASE_KEY) bgneal@214: if phrases: bgneal@214: return phrases bgneal@214: bgneal@214: phrases = SpamPhrase.objects.values_list('phrase', flat=True) bgneal@214: cache.set(SPAM_PHRASE_KEY, phrases) bgneal@214: return phrases bgneal@214: