annotate gpp/bio/admin.py @ 214:28988cce138b

Implement #83: a string filter facility like NukeSeSentinel. It currently isn't hooked up to anything. Will do that in #84.
author Brian Neal <bgneal@gmail.com>
date Fri, 14 May 2010 02:19:48 +0000
parents 8bbefaa3e408
children 8c1832b9d815
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@207 9 import bio.badges
bgneal@147 10 from comments.models import Comment
bgneal@147 11 from forums.tools import delete_user_posts
bgneal@147 12
gremmie@1 13
bgneal@204 14 class BadgeOwnerInline(admin.TabularInline):
bgneal@204 15 model = bio.models.BadgeOwnership
bgneal@204 16 extra = 1
bgneal@204 17
bgneal@204 18
gremmie@1 19 class UserProfileAdmin(admin.ModelAdmin):
bgneal@138 20 search_fields = ('user__username', 'user__first_name', 'user__last_name',
bgneal@138 21 'user__email')
gremmie@1 22 exclude = ('profile_html', 'signature_html')
bgneal@147 23 list_display = ('__unicode__', 'get_status_display', 'status_date')
bgneal@147 24 list_filter = ('status', )
bgneal@147 25 date_hierarchy = 'status_date'
bgneal@204 26 inlines = (BadgeOwnerInline, )
bgneal@147 27 actions = (
bgneal@147 28 'mark_active',
bgneal@147 29 'mark_resigned',
bgneal@147 30 'mark_removed',
bgneal@147 31 'mark_suspended',
bgneal@147 32 'mark_spammer',
bgneal@147 33 )
bgneal@147 34
bgneal@147 35 def get_status_display(self, obj):
bgneal@147 36 return obj.get_status_display()
bgneal@147 37 get_status_display.short_description = 'Status'
bgneal@147 38
bgneal@147 39 def mark_user_status(self, request, qs, status):
bgneal@147 40 """
bgneal@147 41 Common code for the admin actions. Updates the status field in the
bgneal@147 42 profiles to 'status'. Updates the status_date. Sets the is_active
bgneal@147 43 field to True if the status is STA_ACTIVE and False otherwise.
bgneal@147 44 """
bgneal@147 45 now = datetime.datetime.now()
bgneal@147 46 for profile in qs:
bgneal@147 47 profile.user.is_active = status == bio.models.STA_ACTIVE
bgneal@147 48 profile.user.save()
bgneal@147 49 profile.status = status
bgneal@147 50 profile.status_date = now
bgneal@147 51 profile.save()
bgneal@147 52
bgneal@147 53 count = qs.count()
bgneal@147 54 msg = "1 user" if count == 1 else "%d users" % count
bgneal@147 55 self.message_user(request, "%s successfully marked as %s." % (msg,
bgneal@147 56 bio.models.USER_STATUS_CHOICES[status][1]))
bgneal@147 57
bgneal@147 58 def mark_active(self, request, qs):
bgneal@147 59 """
bgneal@147 60 Marks users as active. Updates their profile status to STA_ACTIVE.
bgneal@147 61 """
bgneal@147 62 self.mark_user_status(request, qs, bio.models.STA_ACTIVE)
bgneal@147 63 mark_active.short_description = "Mark selected users as active"
bgneal@147 64
bgneal@147 65 def mark_resigned(self, request, qs):
bgneal@147 66 """
bgneal@147 67 Marks users as inactive. Updates their profile status to STA_RESIGNED.
bgneal@147 68 """
bgneal@147 69 self.mark_user_status(request, qs, bio.models.STA_RESIGNED)
bgneal@147 70 mark_resigned.short_description = "Mark selected users as resigned"
bgneal@147 71
bgneal@147 72 def mark_removed(self, request, qs):
bgneal@147 73 """
bgneal@147 74 Marks users as inactive. Updates their profile status to STA_REMOVED.
bgneal@147 75 """
bgneal@147 76 self.mark_user_status(request, qs, bio.models.STA_REMOVED)
bgneal@147 77 mark_removed.short_description = "Mark selected users as removed"
bgneal@147 78
bgneal@147 79 def mark_suspended(self, request, qs):
bgneal@147 80 """
bgneal@147 81 Marks users as inactive. Updates their profile status to STA_SUSPENDED.
bgneal@147 82 """
bgneal@147 83 self.mark_user_status(request, qs, bio.models.STA_SUSPENDED)
bgneal@147 84 mark_suspended.short_description = "Mark selected users as suspended"
bgneal@147 85
bgneal@147 86 def mark_spammer(self, request, qs):
bgneal@147 87 """
bgneal@147 88 Marks users as inactive. Updates their profile status to STA_SPAMMER.
bgneal@147 89 Deletes all their comments and forum posts.
bgneal@147 90 """
bgneal@147 91 self.mark_user_status(request, qs, bio.models.STA_SPAMMER)
bgneal@147 92 for profile in qs:
bgneal@147 93 Comment.objects.filter(user=profile.user).delete()
bgneal@147 94 delete_user_posts(profile.user)
bgneal@147 95 mark_spammer.short_description = "Mark selected users as spammers"
gremmie@1 96
bgneal@138 97
bgneal@138 98 class UserProfileFlagAdmin(admin.ModelAdmin):
bgneal@138 99 list_display = ('__unicode__', 'flag_date', 'get_profile_url')
bgneal@207 100 actions = ('accept_flags', )
bgneal@207 101
bgneal@207 102 def accept_flags(self, request, qs):
bgneal@207 103 """This action awards a security pin to the user that reported the
bgneal@207 104 profile and then deletes the flag.
bgneal@207 105 """
bgneal@207 106 for flag in qs:
bgneal@207 107 bio.badges.award_badge(bio.badges.SECURITY_PIN, flag.user)
bgneal@207 108 flag.delete()
bgneal@207 109
bgneal@207 110 accept_flags.short_description = "Accept selected flagged profiles"
bgneal@207 111
bgneal@207 112
bgneal@207 113
bgneal@138 114
bgneal@138 115
bgneal@204 116 class BadgeAdmin(admin.ModelAdmin):
bgneal@204 117 list_display = ('name', 'html', 'order', 'numeric_id', 'description')
bgneal@204 118 list_editable = ('order', 'numeric_id')
bgneal@204 119
bgneal@204 120
bgneal@204 121 admin.site.register(bio.models.UserProfile, UserProfileAdmin)
bgneal@204 122 admin.site.register(bio.models.UserProfileFlag, UserProfileFlagAdmin)
bgneal@204 123 admin.site.register(bio.models.Badge, BadgeAdmin)