Mercurial > public > sg101
comparison bio/admin.py @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 |
parents | gpp/bio/admin.py@93f049a241ff |
children | 84865fcd7c26 |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """ | |
2 This file contains the admin definitions for the bio application. | |
3 """ | |
4 import datetime | |
5 | |
6 from django.contrib import admin | |
7 | |
8 import django.contrib.auth.models | |
9 import django.contrib.auth.admin | |
10 | |
11 import bio.models | |
12 import bio.badges | |
13 from antispam.utils import deactivate_spammer | |
14 | |
15 | |
16 class BadgeOwnerInline(admin.TabularInline): | |
17 model = bio.models.BadgeOwnership | |
18 extra = 1 | |
19 | |
20 | |
21 class UserProfileAdmin(admin.ModelAdmin): | |
22 search_fields = ('user__username', 'user__first_name', 'user__last_name', | |
23 'user__email') | |
24 exclude = ('profile_html', 'signature_html') | |
25 list_display = ('__unicode__', 'user_is_active', 'get_status_display', 'status_date') | |
26 readonly_fields = ('status', 'status_date', 'update_date') | |
27 list_filter = ('status', ) | |
28 date_hierarchy = 'status_date' | |
29 inlines = (BadgeOwnerInline, ) | |
30 actions = ( | |
31 'mark_active', | |
32 'mark_resigned', | |
33 'mark_removed', | |
34 'mark_suspended', | |
35 'mark_spammer', | |
36 'mark_stranger', | |
37 ) | |
38 | |
39 def get_status_display(self, obj): | |
40 return obj.get_status_display() | |
41 get_status_display.short_description = 'Status' | |
42 | |
43 def mark_user_status(self, request, qs, status): | |
44 """ | |
45 Common code for the admin actions. Updates the status field in the | |
46 profiles to 'status'. Updates the status_date. Sets the is_active | |
47 field to True if the status is STA_ACTIVE and False otherwise. | |
48 """ | |
49 now = datetime.datetime.now() | |
50 for profile in qs: | |
51 profile.user.is_active = (status == bio.models.STA_ACTIVE or | |
52 status == bio.models.STA_STRANGER) | |
53 profile.user.save() | |
54 profile.status = status | |
55 profile.status_date = now | |
56 profile.save(content_update=False) | |
57 | |
58 count = len(qs) | |
59 msg = "1 user" if count == 1 else "%d users" % count | |
60 self.message_user(request, "%s successfully marked as %s." % (msg, | |
61 bio.models.USER_STATUS_CHOICES[status][1])) | |
62 | |
63 def mark_active(self, request, qs): | |
64 """ | |
65 Marks users as active. Updates their profile status to STA_ACTIVE. | |
66 """ | |
67 self.mark_user_status(request, qs, bio.models.STA_ACTIVE) | |
68 mark_active.short_description = "Mark selected users as active" | |
69 | |
70 def mark_resigned(self, request, qs): | |
71 """ | |
72 Marks users as inactive. Updates their profile status to STA_RESIGNED. | |
73 """ | |
74 self.mark_user_status(request, qs, bio.models.STA_RESIGNED) | |
75 mark_resigned.short_description = "Mark selected users as resigned" | |
76 | |
77 def mark_removed(self, request, qs): | |
78 """ | |
79 Marks users as inactive. Updates their profile status to STA_REMOVED. | |
80 """ | |
81 self.mark_user_status(request, qs, bio.models.STA_REMOVED) | |
82 mark_removed.short_description = "Mark selected users as removed" | |
83 | |
84 def mark_suspended(self, request, qs): | |
85 """ | |
86 Marks users as inactive. Updates their profile status to STA_SUSPENDED. | |
87 """ | |
88 self.mark_user_status(request, qs, bio.models.STA_SUSPENDED) | |
89 mark_suspended.short_description = "Mark selected users as suspended" | |
90 | |
91 def mark_spammer(self, request, qs): | |
92 """ | |
93 Calls deactivate_spammer() on each user in the profile queryset. | |
94 | |
95 """ | |
96 count = qs.count() | |
97 for profile in qs: | |
98 deactivate_spammer(profile.user) | |
99 | |
100 self.message_user(request, | |
101 "%s profile(s) successfully marked as spammers." % count) | |
102 | |
103 mark_spammer.short_description = "Mark selected users as spammers" | |
104 | |
105 def mark_stranger(self, request, qs): | |
106 """ | |
107 Marks users as strangers. Updates their profile status to STA_STRANGER. | |
108 """ | |
109 self.mark_user_status(request, qs, bio.models.STA_STRANGER) | |
110 mark_stranger.short_description = "Mark selected users as strangers" | |
111 | |
112 | |
113 class UserProfileFlagAdmin(admin.ModelAdmin): | |
114 list_display = ['__unicode__', 'flag_date', 'get_profile_url'] | |
115 actions = ['accept_flags'] | |
116 raw_id_fields = ['user', 'profile'] | |
117 | |
118 def accept_flags(self, request, qs): | |
119 """ | |
120 This action awards a security pin to the user that reported the | |
121 profile, deletes the flags, then deactivates the spammers. | |
122 """ | |
123 count = qs.count() | |
124 for flag in qs: | |
125 deactivate_spammer(flag.profile.user) | |
126 bio.badges.award_badge(bio.badges.SECURITY_PIN, flag.user) | |
127 flag.delete() | |
128 | |
129 self.message_user(request, | |
130 "%s profile(s) successfully marked as spammers." % count) | |
131 | |
132 accept_flags.short_description = "Mark selected profiles as spammers" | |
133 | |
134 | |
135 class BadgeAdmin(admin.ModelAdmin): | |
136 list_display = ('name', 'html', 'order', 'numeric_id', 'description') | |
137 list_editable = ('order', 'numeric_id') | |
138 | |
139 | |
140 # We like the User admin but would like a date hierarcy on date_joined. | |
141 class UserAdmin(django.contrib.auth.admin.UserAdmin): | |
142 date_hierarchy = 'date_joined' | |
143 | |
144 | |
145 admin.site.register(bio.models.UserProfile, UserProfileAdmin) | |
146 admin.site.register(bio.models.UserProfileFlag, UserProfileFlagAdmin) | |
147 admin.site.register(bio.models.Badge, BadgeAdmin) | |
148 | |
149 # Unregister existing ModelAdmin for User, then register ours | |
150 admin.site.unregister(django.contrib.auth.models.User) | |
151 admin.site.register(django.contrib.auth.models.User, UserAdmin) |