Mercurial > public > sg101
comparison 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 |
comparison
equal
deleted
inserted
replaced
214:28988cce138b | 215:8c1832b9d815 |
---|---|
1 """Antispam utility functions other apps can use.""" | 1 """Antispam utility functions other apps can use.""" |
2 import datetime | |
3 import logging | |
4 import textwrap | |
5 | |
2 from django.core.cache import cache | 6 from django.core.cache import cache |
3 | 7 |
4 from antispam import SPAM_PHRASE_KEY | 8 from antispam import SPAM_PHRASE_KEY |
5 from antispam.models import SpamPhrase | 9 from antispam.models import SpamPhrase |
10 from core.functions import email_admins | |
11 from bio.models import STA_SUSPENDED | |
6 | 12 |
7 | 13 |
8 def contains_spam(s): | 14 def contains_spam(s): |
9 """This function returns True if the supplied string s contains any spam | 15 """This function returns True if the supplied string s contains any spam |
10 phrases and False otherwise. | 16 phrases and False otherwise. |
11 """ | 17 """ |
12 phrases = _get_spam_phrases() | 18 phrases = _get_spam_phrases() |
19 s = s.lower() | |
13 for spam in phrases: | 20 for spam in phrases: |
14 if spam in s: | 21 if spam in s: |
15 return True | 22 return True |
16 | 23 |
17 return False | 24 return False |
18 | 25 |
26 | |
27 def spam_check(request, content): | |
28 """This function checks the supplied content for spam if the user from the | |
29 supplied request is a stranger (new to the site). If spam is found, the | |
30 function makes a log entry, emails the admins, suspends the user's account | |
31 and returns True. If spam is not found, False is returned. | |
32 It is assumed that request.user is an authenticated user and thus has a | |
33 user profile. | |
34 """ | |
35 user = request.user | |
36 if user.get_profile().is_stranger() and contains_spam(content): | |
37 | |
38 ip = request.META.get('REMOTE_ADDR', "unknown") | |
39 | |
40 msg = textwrap.dedent("""\ | |
41 SPAM FILTER TRIPPED by %s | |
42 PATH: %s | |
43 IP: %s | |
44 Message: | |
45 %s | |
46 """ % (user.username, request.path, ip, content)) | |
47 | |
48 logging.info(msg) | |
49 email_admins("SPAM FILTER TRIPPED BY %s" % user.username, msg) | |
50 suspend_user(user) | |
51 return True | |
52 | |
53 return False | |
54 | |
55 | |
56 def suspend_user(user): | |
57 """This function marks the user as suspended.""" | |
58 user.is_active = False | |
59 user.save() | |
60 profile = user.get_profile() | |
61 profile.status = STA_SUSPENDED | |
62 profile.status_date = datetime.datetime.now() | |
63 profile.save() | |
64 | |
19 | 65 |
20 def _get_spam_phrases(): | 66 def _get_spam_phrases(): |
21 """This function returns the current list of spam phrase strings. | 67 """This function returns the current list of spam phrase strings. |
22 The strings are cached to avoid hitting the database. | 68 The strings are cached to avoid hitting the database. |
23 """ | 69 """ |
26 return phrases | 72 return phrases |
27 | 73 |
28 phrases = SpamPhrase.objects.values_list('phrase', flat=True) | 74 phrases = SpamPhrase.objects.values_list('phrase', flat=True) |
29 cache.set(SPAM_PHRASE_KEY, phrases) | 75 cache.set(SPAM_PHRASE_KEY, phrases) |
30 return phrases | 76 return phrases |
31 |