changeset 563:93f049a241ff

For bitbucket issue #4, deactivate users for spam when accepting flagged user profiles. Also use raw_id_fields on UserProfileFlagAdmin.
author Brian Neal <bgneal@gmail.com>
date Thu, 09 Feb 2012 19:38:03 -0600
parents 98b373ca09f3
children e5d3552d4ad0
files gpp/antispam/utils.py gpp/bio/admin.py gpp/forums/views/spam.py
diffstat 3 files changed, 51 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/antispam/utils.py	Wed Feb 08 18:58:57 2012 -0600
+++ b/gpp/antispam/utils.py	Thu Feb 09 19:38:03 2012 -0600
@@ -8,7 +8,9 @@
 from antispam import SPAM_PHRASE_KEY
 from antispam.models import SpamPhrase
 from core.functions import email_admins
-from bio.models import STA_SUSPENDED
+from bio.models import STA_SUSPENDED, STA_SPAMMER
+from comments.models import Comment
+from forums.tools import delete_user_posts
 
 
 def contains_spam(s):
@@ -61,11 +63,36 @@
     profile.status = STA_SUSPENDED
     profile.status_date = datetime.datetime.now()
     profile.save(content_update=False)
+    logging.info("User suspended: %s", user.username)
+
+
+def deactivate_spammer(user):
+    """
+    This function deactivate's the user, marks them as a spammer, then
+    deletes the user's comments and forum posts. The spammer's profile is
+    cleared so any spam links won't show up anymore.
+
+    """
+    user.is_active = False
+    user.save()
+
+    profile = user.get_profile()
+    profile.status = STA_SPAMMER
+    profile.status_date = datetime.datetime.now()
+    profile.reset_text_fields()
+    profile.save()
+
+    Comment.objects.filter(user=user).delete()
+    delete_user_posts(user)
+
+    logging.info("User deactivated for spam: %s", user.username)
 
 
 def _get_spam_phrases():
-    """This function returns the current list of spam phrase strings.
+    """
+    This function returns the current list of spam phrase strings.
     The strings are cached to avoid hitting the database.
+
     """
     phrases = cache.get(SPAM_PHRASE_KEY)
     if phrases:
--- a/gpp/bio/admin.py	Wed Feb 08 18:58:57 2012 -0600
+++ b/gpp/bio/admin.py	Thu Feb 09 19:38:03 2012 -0600
@@ -10,8 +10,7 @@
 
 import bio.models
 import bio.badges
-from comments.models import Comment
-from forums.tools import delete_user_posts
+from antispam.utils import deactivate_spammer
 
 
 class BadgeOwnerInline(admin.TabularInline):
@@ -91,15 +90,16 @@
 
     def mark_spammer(self, request, qs):
         """
-        Marks users as inactive. Updates their profile status to STA_SPAMMER.
-        Deletes all their comments and forum posts.
+        Calls deactivate_spammer() on each user in the profile queryset.
+
         """
-        self.mark_user_status(request, qs, bio.models.STA_SPAMMER)
+        count = qs.count()
         for profile in qs:
-            Comment.objects.filter(user=profile.user).delete()
-            delete_user_posts(profile.user)
-            profile.reset_text_fields()
-            profile.save()
+            deactivate_spammer(profile.user)
+
+        self.message_user(request,
+                "%s profile(s) successfully marked as spammers." % count)
+
     mark_spammer.short_description = "Mark selected users as spammers"
 
     def mark_stranger(self, request, qs):
@@ -111,18 +111,25 @@
 
 
 class UserProfileFlagAdmin(admin.ModelAdmin):
-    list_display = ('__unicode__', 'flag_date', 'get_profile_url')
-    actions = ('accept_flags', )
+    list_display = ['__unicode__', 'flag_date', 'get_profile_url']
+    actions = ['accept_flags']
+    raw_id_fields = ['user', 'profile']
 
     def accept_flags(self, request, qs):
-        """This action awards a security pin to the user that reported the
-        profile and then deletes the flag.
         """
+        This action awards a security pin to the user that reported the
+        profile, deletes the flags, then deactivates the spammers.
+        """
+        count = qs.count()
         for flag in qs:
+            deactivate_spammer(flag.profile.user)
             bio.badges.award_badge(bio.badges.SECURITY_PIN, flag.user)
             flag.delete()
 
-    accept_flags.short_description = "Accept selected flagged profiles"
+        self.message_user(request,
+                "%s profile(s) successfully marked as spammers." % count)
+
+    accept_flags.short_description = "Mark selected profiles as spammers"
 
 
 class BadgeAdmin(admin.ModelAdmin):
--- a/gpp/forums/views/spam.py	Wed Feb 08 18:58:57 2012 -0600
+++ b/gpp/forums/views/spam.py	Thu Feb 09 19:38:03 2012 -0600
@@ -13,12 +13,11 @@
 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 forums.permissions as perms
 import bio.models
 from core.functions import email_admins
+from antispam.utils import deactivate_spammer
 
 
 SPAMMER_NAILED_SUBJECT = "Spammer Nailed: %s"
@@ -26,23 +25,6 @@
 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. The spammer's profile is
-    cleared so any spam links won't show up anymore.
-    """
-    user.is_active = False
-    user.save()
-
-    profile = user.get_profile()
-    profile.status = bio.models.STA_SPAMMER
-    profile.status_date = datetime.datetime.now()
-    profile.reset_text_fields()
-    profile.save()
-
-    Comment.objects.filter(user=user).delete()
-    delete_user_posts(user)
-
 
 def promote_stranger(user):
     """This function upgrades the user from stranger status to a regular user.