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