changeset 212:fad7548b7f6e

Fix #80: give moderators the ability to deactivate users for spam.
author Brian Neal <bgneal@gmail.com>
date Sun, 09 May 2010 20:53:34 +0000
parents 3a626c48e9ae
children 65016249bf35
files gpp/forums/spam.py gpp/forums/urls.py gpp/templates/forums/display_post.html gpp/templates/forums/spammer.html gpp/templates/forums/spammer_nailed.html media/icons/exclamation.png
diffstat 6 files changed, 134 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/forums/spam.py	Sun May 09 20:53:34 2010 +0000
@@ -0,0 +1,83 @@
+"""
+This module contains views for dealing with spam and spammers.
+"""
+import datetime
+
+from django.contrib.auth.decorators import login_required
+from django.core.urlresolvers import reverse
+from django.http import HttpResponseRedirect
+from django.shortcuts import get_object_or_404
+from django.shortcuts import render_to_response
+from django.template import RequestContext
+from django.contrib.auth.models import User
+
+from comments.models import Comment
+from forums.models import Post
+from forums.tools import delete_user_posts
+import bio.models
+from core.functions import email_admins
+
+
+SPAMMER_NAILED_SUBJECT = "Spammer Nailed: %s"
+SPAMMER_NAILED_MSG_BODY = """
+The admin/moderator user %s has just deactivated the account of %s for spam.
+"""
+
+def deactivate_spammer(user):
+    """This function deactivate's the user, marks them as a spammer, then
+    deletes the user's comments and forum posts.
+    """
+    user.is_active = False
+    user.save()
+
+    profile = user.get_profile()
+    profile.status = bio.models.STA_SPAMMER
+    profile.status_date = datetime.datetime.now()
+    profile.save()
+
+    Comment.objects.filter(user=user).delete()
+    delete_user_posts(user)
+
+
+@login_required
+def spammer(request, post_id):
+    """This view allows moderators to deactivate spammer accounts."""
+
+    post = get_object_or_404(Post.objects.select_related(), pk=post_id)
+
+    can_moderate = request.user.is_superuser or (
+            request.user in post.topic.forum.moderators.all())
+
+    if request.method == "POST":
+        if can_moderate:
+            deactivate_spammer(post.user)
+
+            email_admins(SPAMMER_NAILED_SUBJECT % post.user.username, 
+                SPAMMER_NAILED_MSG_BODY % (
+                    request.user.username, post.user.username))
+
+            return HttpResponseRedirect(reverse('forums-spammer_nailed', args=[
+                post.user.id]))
+
+    return render_to_response('forums/spammer.html', {
+        'can_moderate': can_moderate,
+        'post': post,
+        },
+        context_instance=RequestContext(request))
+
+
+@login_required
+def spammer_nailed(request, spammer_id):
+    """This view presents a confirmation screen that the spammer has been
+    deactivated.
+    """
+    user = get_object_or_404(User, pk=spammer_id)
+    profile = user.get_profile()
+
+    success = not user.is_active and profile.status == bio.models.STA_SPAMMER
+
+    return render_to_response('forums/spammer_nailed.html', {
+        'spammer': user,
+        'success': success,
+        },
+        context_instance=RequestContext(request))
--- a/gpp/forums/urls.py	Sat May 08 23:44:59 2010 +0000
+++ b/gpp/forums/urls.py	Sun May 09 20:53:34 2010 +0000
@@ -34,3 +34,8 @@
     url(r'^subscriptions/(\d+)/$', 'subscription_status', name='forums-subscription_status'),
     url(r'^unsubscribe/(\d+)/$', 'unsubscribe_topic', name='forums-unsubscribe_topic'),
 )
+
+urlpatterns += patterns('forums.spam',
+    url(r'^spammer/(\d+)/$', 'spammer', name='forums-spammer'),
+    url(r'^spammer/nailed/(\d+)/$', 'spammer_nailed', name='forums-spammer_nailed'),
+)
--- a/gpp/templates/forums/display_post.html	Sat May 08 23:44:59 2010 +0000
+++ b/gpp/templates/forums/display_post.html	Sun May 09 20:53:34 2010 +0000
@@ -49,6 +49,10 @@
       {% if can_moderate %}
       <a href="#" class="post-delete" id="dp-{{ post.id }}"
          title="Delete this post"><img src="{{ MEDIA_URL }}icons/cross.png" alt="Delete post" /></a>
+         {% if post.user != user %}
+         <a href="{% url forums-spammer post.id %}" title="This is spam">
+            <img src="{{ MEDIA_URL }}icons/exclamation.png" alt="Spammer" /></a>
+         {% endif %}
       {% endif %}
       </div>
    </td>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/templates/forums/spammer.html	Sun May 09 20:53:34 2010 +0000
@@ -0,0 +1,24 @@
+{% extends 'base.html' %}
+{% block title %}Deactivate Spammer: {{ post.user.username }}{% endblock %}
+{% block content %}
+<h2>Deactivate Spammer: {{ post.user.username }}</h2>
+
+{% if can_moderate %}
+<p>Please confirm that you wish to mark the user
+<a href="{% url bio-view_profile username=post.user.username %}">{{ post.user.username }}</a> as a
+spammer based on <a href="{% url forums-goto_post post.id %}">this post</a>. 
+If you confirm, the user's account will be deactivated, and all posts and comments
+left by the user will be deleted.</p>
+<p><strong>This is a drastic action, so please be absolutely sure
+you wish to proceed!</strong></p>
+<form action="." method="post">{% csrf_token %}
+   <input type="submit" value="Deactivate {{ post.user.username }}" />
+</form>
+{% else %}
+<p>Sorry, but you don't have permission to deactivate spammers in that post's forum.</p>
+{% endif %}
+<hr />
+<p>
+<a href="{% url forums-goto_post post.id %}">Return to the post</a>.
+</p>
+{% endblock %}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/templates/forums/spammer_nailed.html	Sun May 09 20:53:34 2010 +0000
@@ -0,0 +1,18 @@
+{% extends 'base.html' %}
+{% block title %}Spammer Nailed: {{ spammer.username }}{% endblock %}
+{% block content %}
+<h2>Spammer Nailed: {{ spammer.username }}</h2>
+<p>
+{% if success %}
+The user <a href="{% url bio-view_profile username=spammer.username %}">{{ spammer.username }}</a>
+has had his/her account deactivated for spamming.
+All forum posts and comments this user has made have been deleted. The site admin has been
+notified of this action. Thanks for helping to keep our site spam-free!
+{% else %}
+Whoops, something went wrong deactivating the account of
+<a href="{% url bio-view_profile username=spammer.username %}">{{ spammer.username }}</a>. 
+Or, possibly some time has passed and the account was reinstated. If you have any questions, contact
+the site admin.
+{% endif %}
+</p>
+{% endblock %}
Binary file media/icons/exclamation.png has changed