diff gpp/forums/spam.py @ 215:8c1832b9d815

Implement #84; additional checks on spammers; implement stranger status.
author Brian Neal <bgneal@gmail.com>
date Sat, 29 May 2010 04:51:28 +0000
parents fad7548b7f6e
children
line wrap: on
line diff
--- a/gpp/forums/spam.py	Fri May 14 02:19:48 2010 +0000
+++ b/gpp/forums/spam.py	Sat May 29 04:51:28 2010 +0000
@@ -2,10 +2,13 @@
 This module contains views for dealing with spam and spammers.
 """
 import datetime
+import logging
+import textwrap
 
 from django.contrib.auth.decorators import login_required
 from django.core.urlresolvers import reverse
 from django.http import HttpResponseRedirect
+from django.http import HttpResponseForbidden
 from django.shortcuts import get_object_or_404
 from django.shortcuts import render_to_response
 from django.template import RequestContext
@@ -39,28 +42,51 @@
     delete_user_posts(user)
 
 
+def promote_stranger(user):
+    """This function upgrades the user from stranger status to a regular user.
+    """
+    profile = user.get_profile()
+    if user.is_active and profile.status == bio.models.STA_STRANGER:
+        profile.status = bio.models.STA_ACTIVE
+        profile.status_date = datetime.datetime.now()
+        profile.save()
+
+
 @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)
+    poster = post.user
+    poster_profile = poster.get_profile()
 
     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)
+    can_deactivate = (poster_profile.status == bio.models.STA_STRANGER and not
+            poster.is_superuser)
 
-            email_admins(SPAMMER_NAILED_SUBJECT % post.user.username, 
-                SPAMMER_NAILED_MSG_BODY % (
-                    request.user.username, post.user.username))
+    if request.method == "POST" and can_moderate and can_deactivate:
+        deactivate_spammer(poster)
 
-            return HttpResponseRedirect(reverse('forums-spammer_nailed', args=[
-                post.user.id]))
+        email_admins(SPAMMER_NAILED_SUBJECT % poster.username, 
+            SPAMMER_NAILED_MSG_BODY % (
+                request.user.username, poster.username))
+
+        logging.info(textwrap.dedent("""\
+            SPAMMER DEACTIVATED: %s nailed %s for spam.
+            IP: %s
+            Message:
+            %s
+            """ % (request.user.username, poster.username, post.user_ip,
+                post.body)))
+
+        return HttpResponseRedirect(reverse('forums-spammer_nailed', args=[
+            poster.id]))
 
     return render_to_response('forums/spammer.html', {
         'can_moderate': can_moderate,
+        'can_deactivate': can_deactivate,
         'post': post,
         },
         context_instance=RequestContext(request))
@@ -81,3 +107,33 @@
         'success': success,
         },
         context_instance=RequestContext(request))
+
+
+@login_required
+def stranger(request, post_id):
+    """This view allows a forum moderator or super user to promote a user from
+    stranger status to regular user.
+    """
+    post = get_object_or_404(Post.objects.select_related(), pk=post_id)
+    poster = post.user
+    poster_profile = poster.get_profile()
+
+    can_moderate = request.user.is_superuser or (
+            request.user in post.topic.forum.moderators.all())
+
+    can_promote = poster_profile.status == bio.models.STA_STRANGER
+
+    if request.method == "POST" and can_moderate and can_promote:
+        promote_stranger(poster)
+
+        logging.info("STRANGER PROMOTED: %s promoted %s." % (
+            request.user.username, poster.username))
+
+        return HttpResponseRedirect(post.get_absolute_url())
+
+    return render_to_response('forums/stranger.html', {
+        'can_moderate': can_moderate,
+        'can_promote': can_promote,
+        'post': post,
+        },
+        context_instance=RequestContext(request))