annotate antispam/utils.py @ 588:c31841b52d62

Add docutils to requirements.txt.
author Brian Neal <bgneal@gmail.com>
date Thu, 10 May 2012 20:30:46 -0500
parents ee87ea74d46b
children c8b4dfb2d1e5
rev   line source
bgneal@214 1 """Antispam utility functions other apps can use."""
bgneal@215 2 import datetime
bgneal@215 3 import logging
bgneal@215 4 import textwrap
bgneal@215 5
bgneal@214 6 from django.core.cache import cache
bgneal@214 7
bgneal@214 8 from antispam import SPAM_PHRASE_KEY
bgneal@214 9 from antispam.models import SpamPhrase
bgneal@215 10 from core.functions import email_admins
bgneal@563 11 from bio.models import STA_SUSPENDED, STA_SPAMMER
bgneal@563 12 from comments.models import Comment
bgneal@563 13 from forums.tools import delete_user_posts
bgneal@214 14
bgneal@214 15
bgneal@214 16 def contains_spam(s):
bgneal@214 17 """This function returns True if the supplied string s contains any spam
bgneal@214 18 phrases and False otherwise.
bgneal@214 19 """
bgneal@214 20 phrases = _get_spam_phrases()
bgneal@215 21 s = s.lower()
bgneal@214 22 for spam in phrases:
bgneal@214 23 if spam in s:
bgneal@214 24 return True
bgneal@214 25
bgneal@214 26 return False
bgneal@214 27
bgneal@214 28
bgneal@215 29 def spam_check(request, content):
bgneal@215 30 """This function checks the supplied content for spam if the user from the
bgneal@215 31 supplied request is a stranger (new to the site). If spam is found, the
bgneal@215 32 function makes a log entry, emails the admins, suspends the user's account
bgneal@215 33 and returns True. If spam is not found, False is returned.
bgneal@215 34 It is assumed that request.user is an authenticated user and thus has a
bgneal@215 35 user profile.
bgneal@215 36 """
bgneal@215 37 user = request.user
bgneal@215 38 if user.get_profile().is_stranger() and contains_spam(content):
bgneal@215 39
bgneal@215 40 ip = request.META.get('REMOTE_ADDR', "unknown")
bgneal@215 41
bgneal@215 42 msg = textwrap.dedent("""\
bgneal@215 43 SPAM FILTER TRIPPED by %s
bgneal@215 44 PATH: %s
bgneal@215 45 IP: %s
bgneal@215 46 Message:
bgneal@215 47 %s
bgneal@215 48 """ % (user.username, request.path, ip, content))
bgneal@215 49
bgneal@215 50 logging.info(msg)
bgneal@215 51 email_admins("SPAM FILTER TRIPPED BY %s" % user.username, msg)
bgneal@215 52 suspend_user(user)
bgneal@215 53 return True
bgneal@215 54
bgneal@215 55 return False
bgneal@215 56
bgneal@215 57
bgneal@215 58 def suspend_user(user):
bgneal@215 59 """This function marks the user as suspended."""
bgneal@215 60 user.is_active = False
bgneal@215 61 user.save()
bgneal@215 62 profile = user.get_profile()
bgneal@215 63 profile.status = STA_SUSPENDED
bgneal@215 64 profile.status_date = datetime.datetime.now()
bgneal@562 65 profile.save(content_update=False)
bgneal@563 66 logging.info("User suspended: %s", user.username)
bgneal@563 67
bgneal@563 68
bgneal@563 69 def deactivate_spammer(user):
bgneal@563 70 """
bgneal@563 71 This function deactivate's the user, marks them as a spammer, then
bgneal@563 72 deletes the user's comments and forum posts. The spammer's profile is
bgneal@563 73 cleared so any spam links won't show up anymore.
bgneal@563 74
bgneal@563 75 """
bgneal@563 76 user.is_active = False
bgneal@563 77 user.save()
bgneal@563 78
bgneal@563 79 profile = user.get_profile()
bgneal@563 80 profile.status = STA_SPAMMER
bgneal@563 81 profile.status_date = datetime.datetime.now()
bgneal@563 82 profile.reset_text_fields()
bgneal@563 83 profile.save()
bgneal@563 84
bgneal@563 85 Comment.objects.filter(user=user).delete()
bgneal@563 86 delete_user_posts(user)
bgneal@563 87
bgneal@563 88 logging.info("User deactivated for spam: %s", user.username)
bgneal@316 89
bgneal@215 90
bgneal@214 91 def _get_spam_phrases():
bgneal@563 92 """
bgneal@563 93 This function returns the current list of spam phrase strings.
bgneal@214 94 The strings are cached to avoid hitting the database.
bgneal@563 95
bgneal@214 96 """
bgneal@214 97 phrases = cache.get(SPAM_PHRASE_KEY)
bgneal@214 98 if phrases:
bgneal@214 99 return phrases
bgneal@214 100
bgneal@214 101 phrases = SpamPhrase.objects.values_list('phrase', flat=True)
bgneal@214 102 cache.set(SPAM_PHRASE_KEY, phrases)
bgneal@214 103 return phrases