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