comparison gpp/forums/spam.py @ 212:fad7548b7f6e

Fix #80: give moderators the ability to deactivate users for spam.
author Brian Neal <bgneal@gmail.com>
date Sun, 09 May 2010 20:53:34 +0000
parents
children 8c1832b9d815
comparison
equal deleted inserted replaced
211:3a626c48e9ae 212:fad7548b7f6e
1 """
2 This module contains views for dealing with spam and spammers.
3 """
4 import datetime
5
6 from django.contrib.auth.decorators import login_required
7 from django.core.urlresolvers import reverse
8 from django.http import HttpResponseRedirect
9 from django.shortcuts import get_object_or_404
10 from django.shortcuts import render_to_response
11 from django.template import RequestContext
12 from django.contrib.auth.models import User
13
14 from comments.models import Comment
15 from forums.models import Post
16 from forums.tools import delete_user_posts
17 import bio.models
18 from core.functions import email_admins
19
20
21 SPAMMER_NAILED_SUBJECT = "Spammer Nailed: %s"
22 SPAMMER_NAILED_MSG_BODY = """
23 The admin/moderator user %s has just deactivated the account of %s for spam.
24 """
25
26 def deactivate_spammer(user):
27 """This function deactivate's the user, marks them as a spammer, then
28 deletes the user's comments and forum posts.
29 """
30 user.is_active = False
31 user.save()
32
33 profile = user.get_profile()
34 profile.status = bio.models.STA_SPAMMER
35 profile.status_date = datetime.datetime.now()
36 profile.save()
37
38 Comment.objects.filter(user=user).delete()
39 delete_user_posts(user)
40
41
42 @login_required
43 def spammer(request, post_id):
44 """This view allows moderators to deactivate spammer accounts."""
45
46 post = get_object_or_404(Post.objects.select_related(), pk=post_id)
47
48 can_moderate = request.user.is_superuser or (
49 request.user in post.topic.forum.moderators.all())
50
51 if request.method == "POST":
52 if can_moderate:
53 deactivate_spammer(post.user)
54
55 email_admins(SPAMMER_NAILED_SUBJECT % post.user.username,
56 SPAMMER_NAILED_MSG_BODY % (
57 request.user.username, post.user.username))
58
59 return HttpResponseRedirect(reverse('forums-spammer_nailed', args=[
60 post.user.id]))
61
62 return render_to_response('forums/spammer.html', {
63 'can_moderate': can_moderate,
64 'post': post,
65 },
66 context_instance=RequestContext(request))
67
68
69 @login_required
70 def spammer_nailed(request, spammer_id):
71 """This view presents a confirmation screen that the spammer has been
72 deactivated.
73 """
74 user = get_object_or_404(User, pk=spammer_id)
75 profile = user.get_profile()
76
77 success = not user.is_active and profile.status == bio.models.STA_SPAMMER
78
79 return render_to_response('forums/spammer_nailed.html', {
80 'spammer': user,
81 'success': success,
82 },
83 context_instance=RequestContext(request))