annotate gpp/bio/admin.py @ 204:b4305e18d3af

Resolve ticket #74. Add user badges. Some extra credit was done here: also refactored how pending news, links, and downloads are handled.
author Brian Neal <bgneal@gmail.com>
date Sat, 01 May 2010 21:53:59 +0000
parents 152d77265da6
children 8bbefaa3e408
rev   line source
gremmie@1 1 """
gremmie@1 2 This file contains the admin definitions for the bio application.
gremmie@1 3 """
bgneal@147 4 import datetime
gremmie@1 5
gremmie@1 6 from django.contrib import admin
bgneal@204 7
bgneal@147 8 import bio.models
bgneal@147 9 from comments.models import Comment
bgneal@147 10 from forums.tools import delete_user_posts
bgneal@147 11
gremmie@1 12
bgneal@204 13 class BadgeOwnerInline(admin.TabularInline):
bgneal@204 14 model = bio.models.BadgeOwnership
bgneal@204 15 extra = 1
bgneal@204 16
bgneal@204 17
gremmie@1 18 class UserProfileAdmin(admin.ModelAdmin):
bgneal@138 19 search_fields = ('user__username', 'user__first_name', 'user__last_name',
bgneal@138 20 'user__email')
gremmie@1 21 exclude = ('profile_html', 'signature_html')
bgneal@147 22 list_display = ('__unicode__', 'get_status_display', 'status_date')
bgneal@147 23 list_filter = ('status', )
bgneal@147 24 date_hierarchy = 'status_date'
bgneal@204 25 inlines = (BadgeOwnerInline, )
bgneal@147 26 actions = (
bgneal@147 27 'mark_active',
bgneal@147 28 'mark_resigned',
bgneal@147 29 'mark_removed',
bgneal@147 30 'mark_suspended',
bgneal@147 31 'mark_spammer',
bgneal@147 32 )
bgneal@147 33
bgneal@147 34 def get_status_display(self, obj):
bgneal@147 35 return obj.get_status_display()
bgneal@147 36 get_status_display.short_description = 'Status'
bgneal@147 37
bgneal@147 38 def mark_user_status(self, request, qs, status):
bgneal@147 39 """
bgneal@147 40 Common code for the admin actions. Updates the status field in the
bgneal@147 41 profiles to 'status'. Updates the status_date. Sets the is_active
bgneal@147 42 field to True if the status is STA_ACTIVE and False otherwise.
bgneal@147 43 """
bgneal@147 44 now = datetime.datetime.now()
bgneal@147 45 for profile in qs:
bgneal@147 46 profile.user.is_active = status == bio.models.STA_ACTIVE
bgneal@147 47 profile.user.save()
bgneal@147 48 profile.status = status
bgneal@147 49 profile.status_date = now
bgneal@147 50 profile.save()
bgneal@147 51
bgneal@147 52 count = qs.count()
bgneal@147 53 msg = "1 user" if count == 1 else "%d users" % count
bgneal@147 54 self.message_user(request, "%s successfully marked as %s." % (msg,
bgneal@147 55 bio.models.USER_STATUS_CHOICES[status][1]))
bgneal@147 56
bgneal@147 57 def mark_active(self, request, qs):
bgneal@147 58 """
bgneal@147 59 Marks users as active. Updates their profile status to STA_ACTIVE.
bgneal@147 60 """
bgneal@147 61 self.mark_user_status(request, qs, bio.models.STA_ACTIVE)
bgneal@147 62 mark_active.short_description = "Mark selected users as active"
bgneal@147 63
bgneal@147 64 def mark_resigned(self, request, qs):
bgneal@147 65 """
bgneal@147 66 Marks users as inactive. Updates their profile status to STA_RESIGNED.
bgneal@147 67 """
bgneal@147 68 self.mark_user_status(request, qs, bio.models.STA_RESIGNED)
bgneal@147 69 mark_resigned.short_description = "Mark selected users as resigned"
bgneal@147 70
bgneal@147 71 def mark_removed(self, request, qs):
bgneal@147 72 """
bgneal@147 73 Marks users as inactive. Updates their profile status to STA_REMOVED.
bgneal@147 74 """
bgneal@147 75 self.mark_user_status(request, qs, bio.models.STA_REMOVED)
bgneal@147 76 mark_removed.short_description = "Mark selected users as removed"
bgneal@147 77
bgneal@147 78 def mark_suspended(self, request, qs):
bgneal@147 79 """
bgneal@147 80 Marks users as inactive. Updates their profile status to STA_SUSPENDED.
bgneal@147 81 """
bgneal@147 82 self.mark_user_status(request, qs, bio.models.STA_SUSPENDED)
bgneal@147 83 mark_suspended.short_description = "Mark selected users as suspended"
bgneal@147 84
bgneal@147 85 def mark_spammer(self, request, qs):
bgneal@147 86 """
bgneal@147 87 Marks users as inactive. Updates their profile status to STA_SPAMMER.
bgneal@147 88 Deletes all their comments and forum posts.
bgneal@147 89 """
bgneal@147 90 self.mark_user_status(request, qs, bio.models.STA_SPAMMER)
bgneal@147 91 for profile in qs:
bgneal@147 92 Comment.objects.filter(user=profile.user).delete()
bgneal@147 93 delete_user_posts(profile.user)
bgneal@147 94 mark_spammer.short_description = "Mark selected users as spammers"
gremmie@1 95
bgneal@138 96
bgneal@138 97 class UserProfileFlagAdmin(admin.ModelAdmin):
bgneal@138 98 list_display = ('__unicode__', 'flag_date', 'get_profile_url')
bgneal@138 99
bgneal@138 100
bgneal@204 101 class BadgeAdmin(admin.ModelAdmin):
bgneal@204 102 list_display = ('name', 'html', 'order', 'numeric_id', 'description')
bgneal@204 103 list_editable = ('order', 'numeric_id')
bgneal@204 104
bgneal@204 105
bgneal@204 106 admin.site.register(bio.models.UserProfile, UserProfileAdmin)
bgneal@204 107 admin.site.register(bio.models.UserProfileFlag, UserProfileFlagAdmin)
bgneal@204 108 admin.site.register(bio.models.Badge, BadgeAdmin)