annotate gpp/forums/spam.py @ 215:8c1832b9d815

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