annotate gpp/bio/admin.py @ 550:9beda24d558c

Updated the SOPA page.
author Brian Neal <bgneal@gmail.com>
date Sun, 15 Jan 2012 13:48:19 -0600
parents 701730b2fcda
children 98b373ca09f3
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@347 8 import django.contrib.auth.models
bgneal@347 9 import django.contrib.auth.admin
bgneal@347 10
bgneal@147 11 import bio.models
bgneal@207 12 import bio.badges
bgneal@147 13 from comments.models import Comment
bgneal@147 14 from forums.tools import delete_user_posts
bgneal@147 15
gremmie@1 16
bgneal@204 17 class BadgeOwnerInline(admin.TabularInline):
bgneal@204 18 model = bio.models.BadgeOwnership
bgneal@204 19 extra = 1
bgneal@204 20
bgneal@204 21
gremmie@1 22 class UserProfileAdmin(admin.ModelAdmin):
bgneal@138 23 search_fields = ('user__username', 'user__first_name', 'user__last_name',
bgneal@138 24 'user__email')
gremmie@1 25 exclude = ('profile_html', 'signature_html')
bgneal@215 26 list_display = ('__unicode__', 'user_is_active', 'get_status_display', 'status_date')
bgneal@277 27 readonly_fields = ('status', 'status_date', 'update_date')
bgneal@147 28 list_filter = ('status', )
bgneal@147 29 date_hierarchy = 'status_date'
bgneal@204 30 inlines = (BadgeOwnerInline, )
bgneal@147 31 actions = (
bgneal@347 32 'mark_active',
bgneal@147 33 'mark_resigned',
bgneal@147 34 'mark_removed',
bgneal@147 35 'mark_suspended',
bgneal@147 36 'mark_spammer',
bgneal@215 37 'mark_stranger',
bgneal@147 38 )
bgneal@147 39
bgneal@147 40 def get_status_display(self, obj):
bgneal@147 41 return obj.get_status_display()
bgneal@147 42 get_status_display.short_description = 'Status'
bgneal@147 43
bgneal@147 44 def mark_user_status(self, request, qs, status):
bgneal@147 45 """
bgneal@347 46 Common code for the admin actions. Updates the status field in the
bgneal@347 47 profiles to 'status'. Updates the status_date. Sets the is_active
bgneal@147 48 field to True if the status is STA_ACTIVE and False otherwise.
bgneal@147 49 """
bgneal@147 50 now = datetime.datetime.now()
bgneal@147 51 for profile in qs:
bgneal@215 52 profile.user.is_active = (status == bio.models.STA_ACTIVE or
bgneal@215 53 status == bio.models.STA_STRANGER)
bgneal@147 54 profile.user.save()
bgneal@147 55 profile.status = status
bgneal@147 56 profile.status_date = now
bgneal@147 57 profile.save()
bgneal@147 58
bgneal@398 59 count = len(qs)
bgneal@147 60 msg = "1 user" if count == 1 else "%d users" % count
bgneal@147 61 self.message_user(request, "%s successfully marked as %s." % (msg,
bgneal@147 62 bio.models.USER_STATUS_CHOICES[status][1]))
bgneal@147 63
bgneal@147 64 def mark_active(self, request, qs):
bgneal@147 65 """
bgneal@147 66 Marks users as active. Updates their profile status to STA_ACTIVE.
bgneal@147 67 """
bgneal@147 68 self.mark_user_status(request, qs, bio.models.STA_ACTIVE)
bgneal@147 69 mark_active.short_description = "Mark selected users as active"
bgneal@147 70
bgneal@147 71 def mark_resigned(self, request, qs):
bgneal@147 72 """
bgneal@147 73 Marks users as inactive. Updates their profile status to STA_RESIGNED.
bgneal@147 74 """
bgneal@147 75 self.mark_user_status(request, qs, bio.models.STA_RESIGNED)
bgneal@147 76 mark_resigned.short_description = "Mark selected users as resigned"
bgneal@147 77
bgneal@147 78 def mark_removed(self, request, qs):
bgneal@147 79 """
bgneal@147 80 Marks users as inactive. Updates their profile status to STA_REMOVED.
bgneal@147 81 """
bgneal@147 82 self.mark_user_status(request, qs, bio.models.STA_REMOVED)
bgneal@147 83 mark_removed.short_description = "Mark selected users as removed"
bgneal@147 84
bgneal@147 85 def mark_suspended(self, request, qs):
bgneal@147 86 """
bgneal@147 87 Marks users as inactive. Updates their profile status to STA_SUSPENDED.
bgneal@147 88 """
bgneal@147 89 self.mark_user_status(request, qs, bio.models.STA_SUSPENDED)
bgneal@147 90 mark_suspended.short_description = "Mark selected users as suspended"
bgneal@147 91
bgneal@147 92 def mark_spammer(self, request, qs):
bgneal@147 93 """
bgneal@147 94 Marks users as inactive. Updates their profile status to STA_SPAMMER.
bgneal@147 95 Deletes all their comments and forum posts.
bgneal@147 96 """
bgneal@147 97 self.mark_user_status(request, qs, bio.models.STA_SPAMMER)
bgneal@147 98 for profile in qs:
bgneal@147 99 Comment.objects.filter(user=profile.user).delete()
bgneal@147 100 delete_user_posts(profile.user)
bgneal@363 101 profile.reset_text_fields()
bgneal@348 102 profile.save()
bgneal@147 103 mark_spammer.short_description = "Mark selected users as spammers"
gremmie@1 104
bgneal@215 105 def mark_stranger(self, request, qs):
bgneal@215 106 """
bgneal@215 107 Marks users as strangers. Updates their profile status to STA_STRANGER.
bgneal@215 108 """
bgneal@215 109 self.mark_user_status(request, qs, bio.models.STA_STRANGER)
bgneal@215 110 mark_stranger.short_description = "Mark selected users as strangers"
bgneal@215 111
bgneal@138 112
bgneal@138 113 class UserProfileFlagAdmin(admin.ModelAdmin):
bgneal@138 114 list_display = ('__unicode__', 'flag_date', 'get_profile_url')
bgneal@207 115 actions = ('accept_flags', )
bgneal@207 116
bgneal@207 117 def accept_flags(self, request, qs):
bgneal@207 118 """This action awards a security pin to the user that reported the
bgneal@207 119 profile and then deletes the flag.
bgneal@207 120 """
bgneal@207 121 for flag in qs:
bgneal@207 122 bio.badges.award_badge(bio.badges.SECURITY_PIN, flag.user)
bgneal@207 123 flag.delete()
bgneal@207 124
bgneal@207 125 accept_flags.short_description = "Accept selected flagged profiles"
bgneal@207 126
bgneal@207 127
bgneal@204 128 class BadgeAdmin(admin.ModelAdmin):
bgneal@204 129 list_display = ('name', 'html', 'order', 'numeric_id', 'description')
bgneal@204 130 list_editable = ('order', 'numeric_id')
bgneal@204 131
bgneal@204 132
bgneal@347 133 # We like the User admin but would like a date hierarcy on date_joined.
bgneal@347 134 class UserAdmin(django.contrib.auth.admin.UserAdmin):
bgneal@347 135 date_hierarchy = 'date_joined'
bgneal@347 136
bgneal@347 137
bgneal@204 138 admin.site.register(bio.models.UserProfile, UserProfileAdmin)
bgneal@204 139 admin.site.register(bio.models.UserProfileFlag, UserProfileFlagAdmin)
bgneal@204 140 admin.site.register(bio.models.Badge, BadgeAdmin)
bgneal@347 141
bgneal@347 142 # Unregister existing ModelAdmin for User, then register ours
bgneal@347 143 admin.site.unregister(django.contrib.auth.models.User)
bgneal@347 144 admin.site.register(django.contrib.auth.models.User, UserAdmin)