Mercurial > public > sg101
comparison forums/views/spam.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/forums/views/spam.py@93f049a241ff |
children | 9e803323a0d0 |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """ | |
2 This module contains views for dealing with spam and spammers. | |
3 """ | |
4 import datetime | |
5 import logging | |
6 import textwrap | |
7 | |
8 from django.contrib.auth.decorators import login_required | |
9 from django.core.urlresolvers import reverse | |
10 from django.http import HttpResponseRedirect | |
11 from django.shortcuts import get_object_or_404 | |
12 from django.shortcuts import render_to_response | |
13 from django.template import RequestContext | |
14 from django.contrib.auth.models import User | |
15 | |
16 from forums.models import Post | |
17 import forums.permissions as perms | |
18 import bio.models | |
19 from core.functions import email_admins | |
20 from antispam.utils import deactivate_spammer | |
21 | |
22 | |
23 SPAMMER_NAILED_SUBJECT = "Spammer Nailed: %s" | |
24 SPAMMER_NAILED_MSG_BODY = """ | |
25 The admin/moderator user %s has just deactivated the account of %s for spam. | |
26 """ | |
27 | |
28 | |
29 def promote_stranger(user): | |
30 """This function upgrades the user from stranger status to a regular user. | |
31 """ | |
32 profile = user.get_profile() | |
33 if user.is_active and profile.status == bio.models.STA_STRANGER: | |
34 profile.status = bio.models.STA_ACTIVE | |
35 profile.status_date = datetime.datetime.now() | |
36 profile.save(content_update=False) | |
37 | |
38 | |
39 @login_required | |
40 def spammer(request, post_id): | |
41 """This view allows moderators to deactivate spammer accounts.""" | |
42 | |
43 post = get_object_or_404(Post.objects.select_related(), pk=post_id) | |
44 poster = post.user | |
45 poster_profile = poster.get_profile() | |
46 | |
47 can_moderate = perms.can_moderate(post.topic.forum, request.user) | |
48 can_deactivate = (poster_profile.status == bio.models.STA_STRANGER and not | |
49 poster.is_superuser) | |
50 | |
51 if request.method == "POST" and can_moderate and can_deactivate: | |
52 deactivate_spammer(poster) | |
53 | |
54 email_admins(SPAMMER_NAILED_SUBJECT % poster.username, | |
55 SPAMMER_NAILED_MSG_BODY % ( | |
56 request.user.username, poster.username)) | |
57 | |
58 logging.info(textwrap.dedent("""\ | |
59 SPAMMER DEACTIVATED: %s nailed %s for spam. | |
60 IP: %s | |
61 Message: | |
62 %s | |
63 """), | |
64 request.user.username, poster.username, post.user_ip, post.body) | |
65 | |
66 return HttpResponseRedirect(reverse('forums-spammer_nailed', args=[ | |
67 poster.id])) | |
68 | |
69 return render_to_response('forums/spammer.html', { | |
70 'can_moderate': can_moderate, | |
71 'can_deactivate': can_deactivate, | |
72 'post': post, | |
73 }, | |
74 context_instance=RequestContext(request)) | |
75 | |
76 | |
77 @login_required | |
78 def spammer_nailed(request, spammer_id): | |
79 """This view presents a confirmation screen that the spammer has been | |
80 deactivated. | |
81 """ | |
82 user = get_object_or_404(User, pk=spammer_id) | |
83 profile = user.get_profile() | |
84 | |
85 success = not user.is_active and profile.status == bio.models.STA_SPAMMER | |
86 | |
87 return render_to_response('forums/spammer_nailed.html', { | |
88 'spammer': user, | |
89 'success': success, | |
90 }, | |
91 context_instance=RequestContext(request)) | |
92 | |
93 | |
94 @login_required | |
95 def stranger(request, post_id): | |
96 """This view allows a forum moderator or super user to promote a user from | |
97 stranger status to regular user. | |
98 """ | |
99 post = get_object_or_404(Post.objects.select_related(), pk=post_id) | |
100 poster = post.user | |
101 poster_profile = poster.get_profile() | |
102 | |
103 can_moderate = perms.can_moderate(post.topic.forum, request.user) | |
104 can_promote = poster_profile.status == bio.models.STA_STRANGER | |
105 | |
106 if request.method == "POST" and can_moderate and can_promote: | |
107 promote_stranger(poster) | |
108 | |
109 logging.info("STRANGER PROMOTED: %s promoted %s.", | |
110 request.user.username, poster.username) | |
111 | |
112 return HttpResponseRedirect(post.get_absolute_url()) | |
113 | |
114 return render_to_response('forums/stranger.html', { | |
115 'can_moderate': can_moderate, | |
116 'can_promote': can_promote, | |
117 'post': post, | |
118 }, | |
119 context_instance=RequestContext(request)) |