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.shortcuts import get_object_or_404 bgneal@1032: from django.shortcuts import render bgneal@232: from django.contrib.auth.models import User bgneal@232: bgneal@232: from forums.models import Post bgneal@460: import forums.permissions as perms bgneal@232: import bio.models bgneal@232: from core.functions import email_admins bgneal@563: from antispam.utils import deactivate_spammer 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: bgneal@232: def promote_stranger(user): bgneal@232: """This function upgrades the user from stranger status to a regular user. bgneal@232: """ bgneal@789: profile = user.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@562: profile.save(content_update=False) 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@789: poster_profile = poster.profile bgneal@232: bgneal@460: can_moderate = perms.can_moderate(post.topic.forum, request.user) 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@1032: return render(request, 'forums/spammer.html', { bgneal@232: 'can_moderate': can_moderate, bgneal@232: 'can_deactivate': can_deactivate, bgneal@232: 'post': post, bgneal@1032: }) 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@789: profile = user.profile bgneal@232: bgneal@232: success = not user.is_active and profile.status == bio.models.STA_SPAMMER bgneal@232: bgneal@1032: return render(request, 'forums/spammer_nailed.html', { bgneal@232: 'spammer': user, bgneal@232: 'success': success, bgneal@1032: }) 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@789: poster_profile = poster.profile bgneal@232: bgneal@460: can_moderate = perms.can_moderate(post.topic.forum, request.user) 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@1032: return render(request, 'forums/stranger.html', { bgneal@232: 'can_moderate': can_moderate, bgneal@232: 'can_promote': can_promote, bgneal@232: 'post': post, bgneal@1032: })