bgneal@212: """ bgneal@212: This module contains views for dealing with spam and spammers. bgneal@212: """ bgneal@212: import datetime 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@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@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@212: bgneal@212: can_moderate = request.user.is_superuser or ( bgneal@212: request.user in post.topic.forum.moderators.all()) bgneal@212: bgneal@212: if request.method == "POST": bgneal@212: if can_moderate: bgneal@212: deactivate_spammer(post.user) bgneal@212: bgneal@212: email_admins(SPAMMER_NAILED_SUBJECT % post.user.username, bgneal@212: SPAMMER_NAILED_MSG_BODY % ( bgneal@212: request.user.username, post.user.username)) bgneal@212: bgneal@212: return HttpResponseRedirect(reverse('forums-spammer_nailed', args=[ bgneal@212: post.user.id])) bgneal@212: bgneal@212: return render_to_response('forums/spammer.html', { bgneal@212: 'can_moderate': can_moderate, 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))