bgneal@212: """ bgneal@212: This module contains views for dealing with spam and spammers. bgneal@212: """ bgneal@212: import datetime bgneal@215: import logging bgneal@215: import textwrap bgneal@212: bgneal@212: from django.contrib.auth.decorators import login_required bgneal@212: from django.core.urlresolvers import reverse bgneal@212: from django.http import HttpResponseRedirect bgneal@215: from django.http import HttpResponseForbidden bgneal@212: from django.shortcuts import get_object_or_404 bgneal@212: from django.shortcuts import render_to_response bgneal@212: from django.template import RequestContext bgneal@212: from django.contrib.auth.models import User bgneal@212: bgneal@212: from comments.models import Comment bgneal@212: from forums.models import Post bgneal@212: from forums.tools import delete_user_posts bgneal@212: import bio.models bgneal@212: from core.functions import email_admins bgneal@212: bgneal@212: bgneal@212: SPAMMER_NAILED_SUBJECT = "Spammer Nailed: %s" bgneal@212: SPAMMER_NAILED_MSG_BODY = """ bgneal@212: The admin/moderator user %s has just deactivated the account of %s for spam. bgneal@212: """ bgneal@212: bgneal@212: def deactivate_spammer(user): bgneal@212: """This function deactivate's the user, marks them as a spammer, then bgneal@212: deletes the user's comments and forum posts. bgneal@212: """ bgneal@212: user.is_active = False bgneal@212: user.save() bgneal@212: bgneal@212: profile = user.get_profile() bgneal@212: profile.status = bio.models.STA_SPAMMER bgneal@212: profile.status_date = datetime.datetime.now() bgneal@212: profile.save() bgneal@212: bgneal@212: Comment.objects.filter(user=user).delete() bgneal@212: delete_user_posts(user) bgneal@212: bgneal@212: bgneal@215: def promote_stranger(user): bgneal@215: """This function upgrades the user from stranger status to a regular user. bgneal@215: """ bgneal@215: profile = user.get_profile() bgneal@215: if user.is_active and profile.status == bio.models.STA_STRANGER: bgneal@215: profile.status = bio.models.STA_ACTIVE bgneal@215: profile.status_date = datetime.datetime.now() bgneal@215: profile.save() bgneal@215: bgneal@215: bgneal@212: @login_required bgneal@212: def spammer(request, post_id): bgneal@212: """This view allows moderators to deactivate spammer accounts.""" bgneal@212: bgneal@212: post = get_object_or_404(Post.objects.select_related(), pk=post_id) bgneal@215: poster = post.user bgneal@215: poster_profile = poster.get_profile() bgneal@212: bgneal@212: can_moderate = request.user.is_superuser or ( bgneal@212: request.user in post.topic.forum.moderators.all()) bgneal@212: bgneal@215: can_deactivate = (poster_profile.status == bio.models.STA_STRANGER and not bgneal@215: poster.is_superuser) bgneal@212: bgneal@215: if request.method == "POST" and can_moderate and can_deactivate: bgneal@215: deactivate_spammer(poster) bgneal@212: bgneal@215: email_admins(SPAMMER_NAILED_SUBJECT % poster.username, bgneal@215: SPAMMER_NAILED_MSG_BODY % ( bgneal@215: request.user.username, poster.username)) bgneal@215: bgneal@215: logging.info(textwrap.dedent("""\ bgneal@215: SPAMMER DEACTIVATED: %s nailed %s for spam. bgneal@215: IP: %s bgneal@215: Message: bgneal@215: %s bgneal@215: """ % (request.user.username, poster.username, post.user_ip, bgneal@215: post.body))) bgneal@215: bgneal@215: return HttpResponseRedirect(reverse('forums-spammer_nailed', args=[ bgneal@215: poster.id])) bgneal@212: bgneal@212: return render_to_response('forums/spammer.html', { bgneal@212: 'can_moderate': can_moderate, bgneal@215: 'can_deactivate': can_deactivate, bgneal@212: 'post': post, bgneal@212: }, bgneal@212: context_instance=RequestContext(request)) bgneal@212: bgneal@212: bgneal@212: @login_required bgneal@212: def spammer_nailed(request, spammer_id): bgneal@212: """This view presents a confirmation screen that the spammer has been bgneal@212: deactivated. bgneal@212: """ bgneal@212: user = get_object_or_404(User, pk=spammer_id) bgneal@212: profile = user.get_profile() bgneal@212: bgneal@212: success = not user.is_active and profile.status == bio.models.STA_SPAMMER bgneal@212: bgneal@212: return render_to_response('forums/spammer_nailed.html', { bgneal@212: 'spammer': user, bgneal@212: 'success': success, bgneal@212: }, bgneal@212: context_instance=RequestContext(request)) bgneal@215: bgneal@215: bgneal@215: @login_required bgneal@215: def stranger(request, post_id): bgneal@215: """This view allows a forum moderator or super user to promote a user from bgneal@215: stranger status to regular user. bgneal@215: """ bgneal@215: post = get_object_or_404(Post.objects.select_related(), pk=post_id) bgneal@215: poster = post.user bgneal@215: poster_profile = poster.get_profile() bgneal@215: bgneal@215: can_moderate = request.user.is_superuser or ( bgneal@215: request.user in post.topic.forum.moderators.all()) bgneal@215: bgneal@215: can_promote = poster_profile.status == bio.models.STA_STRANGER bgneal@215: bgneal@215: if request.method == "POST" and can_moderate and can_promote: bgneal@215: promote_stranger(poster) bgneal@215: bgneal@215: logging.info("STRANGER PROMOTED: %s promoted %s." % ( bgneal@215: request.user.username, poster.username)) bgneal@215: bgneal@215: return HttpResponseRedirect(post.get_absolute_url()) bgneal@215: bgneal@215: return render_to_response('forums/stranger.html', { bgneal@215: 'can_moderate': can_moderate, bgneal@215: 'can_promote': can_promote, bgneal@215: 'post': post, bgneal@215: }, bgneal@215: context_instance=RequestContext(request))