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