Mercurial > public > sg101
diff gpp/antispam/utils.py @ 215:8c1832b9d815
Implement #84; additional checks on spammers; implement stranger status.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 29 May 2010 04:51:28 +0000 |
parents | 28988cce138b |
children | 767cedc7d12a |
line wrap: on
line diff
--- a/gpp/antispam/utils.py Fri May 14 02:19:48 2010 +0000 +++ b/gpp/antispam/utils.py Sat May 29 04:51:28 2010 +0000 @@ -1,8 +1,14 @@ """Antispam utility functions other apps can use.""" +import datetime +import logging +import textwrap + from django.core.cache import cache from antispam import SPAM_PHRASE_KEY from antispam.models import SpamPhrase +from core.functions import email_admins +from bio.models import STA_SUSPENDED def contains_spam(s): @@ -10,6 +16,7 @@ phrases and False otherwise. """ phrases = _get_spam_phrases() + s = s.lower() for spam in phrases: if spam in s: return True @@ -17,6 +24,45 @@ return False +def spam_check(request, content): + """This function checks the supplied content for spam if the user from the + supplied request is a stranger (new to the site). If spam is found, the + function makes a log entry, emails the admins, suspends the user's account + and returns True. If spam is not found, False is returned. + It is assumed that request.user is an authenticated user and thus has a + user profile. + """ + user = request.user + if user.get_profile().is_stranger() and contains_spam(content): + + ip = request.META.get('REMOTE_ADDR', "unknown") + + msg = textwrap.dedent("""\ + SPAM FILTER TRIPPED by %s + PATH: %s + IP: %s + Message: + %s + """ % (user.username, request.path, ip, content)) + + logging.info(msg) + email_admins("SPAM FILTER TRIPPED BY %s" % user.username, msg) + suspend_user(user) + return True + + return False + + +def suspend_user(user): + """This function marks the user as suspended.""" + user.is_active = False + user.save() + profile = user.get_profile() + profile.status = STA_SUSPENDED + profile.status_date = datetime.datetime.now() + profile.save() + + def _get_spam_phrases(): """This function returns the current list of spam phrase strings. The strings are cached to avoid hitting the database. @@ -28,4 +74,3 @@ phrases = SpamPhrase.objects.values_list('phrase', flat=True) cache.set(SPAM_PHRASE_KEY, phrases) return phrases -