comparison 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
comparison
equal deleted inserted replaced
146:023132c90021 147:152d77265da6
1 """ 1 """
2 This file contains the admin definitions for the bio application. 2 This file contains the admin definitions for the bio application.
3 """ 3 """
4 import datetime
4 5
5 from django.contrib import admin 6 from django.contrib import admin
6 from bio.models import UserProfile 7 from bio.models import UserProfile
7 from bio.models import UserProfileFlag 8 from bio.models import UserProfileFlag
9 import bio.models
10 from comments.models import Comment
11 from forums.tools import delete_user_posts
12
8 13
9 class UserProfileAdmin(admin.ModelAdmin): 14 class UserProfileAdmin(admin.ModelAdmin):
10 search_fields = ('user__username', 'user__first_name', 'user__last_name', 15 search_fields = ('user__username', 'user__first_name', 'user__last_name',
11 'user__email') 16 'user__email')
12 exclude = ('profile_html', 'signature_html') 17 exclude = ('profile_html', 'signature_html')
18 list_display = ('__unicode__', 'get_status_display', 'status_date')
19 list_filter = ('status', )
20 date_hierarchy = 'status_date'
21 actions = (
22 'mark_active',
23 'mark_resigned',
24 'mark_removed',
25 'mark_suspended',
26 'mark_spammer',
27 )
28
29 def get_status_display(self, obj):
30 return obj.get_status_display()
31 get_status_display.short_description = 'Status'
32
33 def mark_user_status(self, request, qs, status):
34 """
35 Common code for the admin actions. Updates the status field in the
36 profiles to 'status'. Updates the status_date. Sets the is_active
37 field to True if the status is STA_ACTIVE and False otherwise.
38 """
39 now = datetime.datetime.now()
40 for profile in qs:
41 profile.user.is_active = status == bio.models.STA_ACTIVE
42 profile.user.save()
43 profile.status = status
44 profile.status_date = now
45 profile.save()
46
47 count = qs.count()
48 msg = "1 user" if count == 1 else "%d users" % count
49 self.message_user(request, "%s successfully marked as %s." % (msg,
50 bio.models.USER_STATUS_CHOICES[status][1]))
51
52 def mark_active(self, request, qs):
53 """
54 Marks users as active. Updates their profile status to STA_ACTIVE.
55 """
56 self.mark_user_status(request, qs, bio.models.STA_ACTIVE)
57 mark_active.short_description = "Mark selected users as active"
58
59 def mark_resigned(self, request, qs):
60 """
61 Marks users as inactive. Updates their profile status to STA_RESIGNED.
62 """
63 self.mark_user_status(request, qs, bio.models.STA_RESIGNED)
64 mark_resigned.short_description = "Mark selected users as resigned"
65
66 def mark_removed(self, request, qs):
67 """
68 Marks users as inactive. Updates their profile status to STA_REMOVED.
69 """
70 self.mark_user_status(request, qs, bio.models.STA_REMOVED)
71 mark_removed.short_description = "Mark selected users as removed"
72
73 def mark_suspended(self, request, qs):
74 """
75 Marks users as inactive. Updates their profile status to STA_SUSPENDED.
76 """
77 self.mark_user_status(request, qs, bio.models.STA_SUSPENDED)
78 mark_suspended.short_description = "Mark selected users as suspended"
79
80 def mark_spammer(self, request, qs):
81 """
82 Marks users as inactive. Updates their profile status to STA_SPAMMER.
83 Deletes all their comments and forum posts.
84 """
85 self.mark_user_status(request, qs, bio.models.STA_SPAMMER)
86 for profile in qs:
87 Comment.objects.filter(user=profile.user).delete()
88 delete_user_posts(profile.user)
89 mark_spammer.short_description = "Mark selected users as spammers"
13 90
14 91
15 class UserProfileFlagAdmin(admin.ModelAdmin): 92 class UserProfileFlagAdmin(admin.ModelAdmin):
16 list_display = ('__unicode__', 'flag_date', 'get_profile_url') 93 list_display = ('__unicode__', 'flag_date', 'get_profile_url')
17 94