diff gpp/bio/admin.py @ 147:152d77265da6

Implement #38: add function to mark user as a spammer. Display only active members on member list. Display login form as table (not sure why wasn't doing this before).
author Brian Neal <bgneal@gmail.com>
date Sun, 13 Dec 2009 08:11:16 +0000
parents 7ea842744a57
children b4305e18d3af
line wrap: on
line diff
--- a/gpp/bio/admin.py	Wed Dec 09 22:58:05 2009 +0000
+++ b/gpp/bio/admin.py	Sun Dec 13 08:11:16 2009 +0000
@@ -1,15 +1,92 @@
 """
 This file contains the admin definitions for the bio application.
 """
+import datetime
 
 from django.contrib import admin
 from bio.models import UserProfile
 from bio.models import UserProfileFlag
+import bio.models
+from comments.models import Comment
+from forums.tools import delete_user_posts
+
 
 class UserProfileAdmin(admin.ModelAdmin):
     search_fields = ('user__username', 'user__first_name', 'user__last_name',
             'user__email')
     exclude = ('profile_html', 'signature_html')
+    list_display = ('__unicode__', 'get_status_display', 'status_date')
+    list_filter = ('status', )
+    date_hierarchy = 'status_date'
+    actions = (
+        'mark_active', 
+        'mark_resigned',
+        'mark_removed',
+        'mark_suspended',
+        'mark_spammer',
+    )
+
+    def get_status_display(self, obj):
+        return obj.get_status_display()
+    get_status_display.short_description = 'Status'
+
+    def mark_user_status(self, request, qs, status):
+        """
+        Common code for the admin actions. Updates the status field in the 
+        profiles to 'status'. Updates the status_date.  Sets the is_active 
+        field to True if the status is STA_ACTIVE and False otherwise.
+        """
+        now = datetime.datetime.now()
+        for profile in qs:
+            profile.user.is_active = status == bio.models.STA_ACTIVE
+            profile.user.save()
+            profile.status = status
+            profile.status_date = now
+            profile.save()
+
+        count = qs.count()
+        msg = "1 user" if count == 1 else "%d users" % count
+        self.message_user(request, "%s successfully marked as %s." % (msg,
+            bio.models.USER_STATUS_CHOICES[status][1]))
+
+    def mark_active(self, request, qs):
+        """
+        Marks users as active. Updates their profile status to STA_ACTIVE.
+        """
+        self.mark_user_status(request, qs, bio.models.STA_ACTIVE)
+    mark_active.short_description = "Mark selected users as active"
+
+    def mark_resigned(self, request, qs):
+        """
+        Marks users as inactive. Updates their profile status to STA_RESIGNED.
+        """
+        self.mark_user_status(request, qs, bio.models.STA_RESIGNED)
+    mark_resigned.short_description = "Mark selected users as resigned"
+
+    def mark_removed(self, request, qs):
+        """
+        Marks users as inactive. Updates their profile status to STA_REMOVED.
+        """
+        self.mark_user_status(request, qs, bio.models.STA_REMOVED)
+    mark_removed.short_description = "Mark selected users as removed"
+
+    def mark_suspended(self, request, qs):
+        """
+        Marks users as inactive. Updates their profile status to STA_SUSPENDED.
+        """
+        self.mark_user_status(request, qs, bio.models.STA_SUSPENDED)
+    mark_suspended.short_description = "Mark selected users as suspended"
+
+    def mark_spammer(self, request, qs):
+        """
+        Marks users as inactive. Updates their profile status to STA_SPAMMER.
+        Deletes all their comments and forum posts.
+        """
+        self.mark_user_status(request, qs, bio.models.STA_SPAMMER)
+        for profile in qs:
+            Comment.objects.filter(user=profile.user).delete()
+            delete_user_posts(profile.user)
+    mark_spammer.short_description = "Mark selected users as spammers"
 
 
 class UserProfileFlagAdmin(admin.ModelAdmin):