annotate gpp/forums/views/spam.py @ 363:9d470c7a2b93

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