comparison gpp/forums/views/spam.py @ 232:a46788862737

Implement a forum favorites feature for #82
author Brian Neal <bgneal@gmail.com>
date Sun, 01 Aug 2010 21:26:12 +0000
parents
children 767cedc7d12a
comparison
equal deleted inserted replaced
231:a2d388ed106e 232:a46788862737
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.http import HttpResponseForbidden
12 from django.shortcuts import get_object_or_404
13 from django.shortcuts import render_to_response
14 from django.template import RequestContext
15 from django.contrib.auth.models import User
16
17 from comments.models import Comment
18 from forums.models import Post
19 from forums.tools import delete_user_posts
20 import bio.models
21 from core.functions import email_admins
22
23
24 SPAMMER_NAILED_SUBJECT = "Spammer Nailed: %s"
25 SPAMMER_NAILED_MSG_BODY = """
26 The admin/moderator user %s has just deactivated the account of %s for spam.
27 """
28
29 def deactivate_spammer(user):
30 """This function deactivate's the user, marks them as a spammer, then
31 deletes the user's comments and forum posts.
32 """
33 user.is_active = False
34 user.save()
35
36 profile = user.get_profile()
37 profile.status = bio.models.STA_SPAMMER
38 profile.status_date = datetime.datetime.now()
39 profile.save()
40
41 Comment.objects.filter(user=user).delete()
42 delete_user_posts(user)
43
44
45 def promote_stranger(user):
46 """This function upgrades the user from stranger status to a regular user.
47 """
48 profile = user.get_profile()
49 if user.is_active and profile.status == bio.models.STA_STRANGER:
50 profile.status = bio.models.STA_ACTIVE
51 profile.status_date = datetime.datetime.now()
52 profile.save()
53
54
55 @login_required
56 def spammer(request, post_id):
57 """This view allows moderators to deactivate spammer accounts."""
58
59 post = get_object_or_404(Post.objects.select_related(), pk=post_id)
60 poster = post.user
61 poster_profile = poster.get_profile()
62
63 can_moderate = request.user.is_superuser or (
64 request.user in post.topic.forum.moderators.all())
65
66 can_deactivate = (poster_profile.status == bio.models.STA_STRANGER and not
67 poster.is_superuser)
68
69 if request.method == "POST" and can_moderate and can_deactivate:
70 deactivate_spammer(poster)
71
72 email_admins(SPAMMER_NAILED_SUBJECT % poster.username,
73 SPAMMER_NAILED_MSG_BODY % (
74 request.user.username, poster.username))
75
76 logging.info(textwrap.dedent("""\
77 SPAMMER DEACTIVATED: %s nailed %s for spam.
78 IP: %s
79 Message:
80 %s
81 """ % (request.user.username, poster.username, post.user_ip,
82 post.body)))
83
84 return HttpResponseRedirect(reverse('forums-spammer_nailed', args=[
85 poster.id]))
86
87 return render_to_response('forums/spammer.html', {
88 'can_moderate': can_moderate,
89 'can_deactivate': can_deactivate,
90 'post': post,
91 },
92 context_instance=RequestContext(request))
93
94
95 @login_required
96 def spammer_nailed(request, spammer_id):
97 """This view presents a confirmation screen that the spammer has been
98 deactivated.
99 """
100 user = get_object_or_404(User, pk=spammer_id)
101 profile = user.get_profile()
102
103 success = not user.is_active and profile.status == bio.models.STA_SPAMMER
104
105 return render_to_response('forums/spammer_nailed.html', {
106 'spammer': user,
107 'success': success,
108 },
109 context_instance=RequestContext(request))
110
111
112 @login_required
113 def stranger(request, post_id):
114 """This view allows a forum moderator or super user to promote a user from
115 stranger status to regular user.
116 """
117 post = get_object_or_404(Post.objects.select_related(), pk=post_id)
118 poster = post.user
119 poster_profile = poster.get_profile()
120
121 can_moderate = request.user.is_superuser or (
122 request.user in post.topic.forum.moderators.all())
123
124 can_promote = poster_profile.status == bio.models.STA_STRANGER
125
126 if request.method == "POST" and can_moderate and can_promote:
127 promote_stranger(poster)
128
129 logging.info("STRANGER PROMOTED: %s promoted %s." % (
130 request.user.username, poster.username))
131
132 return HttpResponseRedirect(post.get_absolute_url())
133
134 return render_to_response('forums/stranger.html', {
135 'can_moderate': can_moderate,
136 'can_promote': can_promote,
137 'post': post,
138 },
139 context_instance=RequestContext(request))