annotate gpp/bio/admin.py @ 257:6e7e84707e7d

Fix #106: CSRF verification failures on the deleting of checked PMs.
author Brian Neal <bgneal@gmail.com>
date Tue, 21 Sep 2010 01:16:02 +0000
parents 8c1832b9d815
children d424b8bae71d
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@215 23 list_display = ('__unicode__', 'user_is_active', 'get_status_display', 'status_date')
bgneal@215 24 readonly_fields = ('status', 'status_date')
bgneal@147 25 list_filter = ('status', )
bgneal@147 26 date_hierarchy = 'status_date'
bgneal@204 27 inlines = (BadgeOwnerInline, )
bgneal@147 28 actions = (
bgneal@147 29 'mark_active',
bgneal@147 30 'mark_resigned',
bgneal@147 31 'mark_removed',
bgneal@147 32 'mark_suspended',
bgneal@147 33 'mark_spammer',
bgneal@215 34 'mark_stranger',
bgneal@147 35 )
bgneal@147 36
bgneal@147 37 def get_status_display(self, obj):
bgneal@147 38 return obj.get_status_display()
bgneal@147 39 get_status_display.short_description = 'Status'
bgneal@147 40
bgneal@147 41 def mark_user_status(self, request, qs, status):
bgneal@147 42 """
bgneal@147 43 Common code for the admin actions. Updates the status field in the
bgneal@147 44 profiles to 'status'. Updates the status_date. Sets the is_active
bgneal@147 45 field to True if the status is STA_ACTIVE and False otherwise.
bgneal@147 46 """
bgneal@147 47 now = datetime.datetime.now()
bgneal@147 48 for profile in qs:
bgneal@215 49 profile.user.is_active = (status == bio.models.STA_ACTIVE or
bgneal@215 50 status == bio.models.STA_STRANGER)
bgneal@147 51 profile.user.save()
bgneal@147 52 profile.status = status
bgneal@147 53 profile.status_date = now
bgneal@147 54 profile.save()
bgneal@147 55
bgneal@147 56 count = qs.count()
bgneal@147 57 msg = "1 user" if count == 1 else "%d users" % count
bgneal@147 58 self.message_user(request, "%s successfully marked as %s." % (msg,
bgneal@147 59 bio.models.USER_STATUS_CHOICES[status][1]))
bgneal@147 60
bgneal@147 61 def mark_active(self, request, qs):
bgneal@147 62 """
bgneal@147 63 Marks users as active. Updates their profile status to STA_ACTIVE.
bgneal@147 64 """
bgneal@147 65 self.mark_user_status(request, qs, bio.models.STA_ACTIVE)
bgneal@147 66 mark_active.short_description = "Mark selected users as active"
bgneal@147 67
bgneal@147 68 def mark_resigned(self, request, qs):
bgneal@147 69 """
bgneal@147 70 Marks users as inactive. Updates their profile status to STA_RESIGNED.
bgneal@147 71 """
bgneal@147 72 self.mark_user_status(request, qs, bio.models.STA_RESIGNED)
bgneal@147 73 mark_resigned.short_description = "Mark selected users as resigned"
bgneal@147 74
bgneal@147 75 def mark_removed(self, request, qs):
bgneal@147 76 """
bgneal@147 77 Marks users as inactive. Updates their profile status to STA_REMOVED.
bgneal@147 78 """
bgneal@147 79 self.mark_user_status(request, qs, bio.models.STA_REMOVED)
bgneal@147 80 mark_removed.short_description = "Mark selected users as removed"
bgneal@147 81
bgneal@147 82 def mark_suspended(self, request, qs):
bgneal@147 83 """
bgneal@147 84 Marks users as inactive. Updates their profile status to STA_SUSPENDED.
bgneal@147 85 """
bgneal@147 86 self.mark_user_status(request, qs, bio.models.STA_SUSPENDED)
bgneal@147 87 mark_suspended.short_description = "Mark selected users as suspended"
bgneal@147 88
bgneal@147 89 def mark_spammer(self, request, qs):
bgneal@147 90 """
bgneal@147 91 Marks users as inactive. Updates their profile status to STA_SPAMMER.
bgneal@147 92 Deletes all their comments and forum posts.
bgneal@147 93 """
bgneal@147 94 self.mark_user_status(request, qs, bio.models.STA_SPAMMER)
bgneal@147 95 for profile in qs:
bgneal@147 96 Comment.objects.filter(user=profile.user).delete()
bgneal@147 97 delete_user_posts(profile.user)
bgneal@147 98 mark_spammer.short_description = "Mark selected users as spammers"
gremmie@1 99
bgneal@215 100 def mark_stranger(self, request, qs):
bgneal@215 101 """
bgneal@215 102 Marks users as strangers. Updates their profile status to STA_STRANGER.
bgneal@215 103 """
bgneal@215 104 self.mark_user_status(request, qs, bio.models.STA_STRANGER)
bgneal@215 105 mark_stranger.short_description = "Mark selected users as strangers"
bgneal@215 106
bgneal@138 107
bgneal@138 108 class UserProfileFlagAdmin(admin.ModelAdmin):
bgneal@138 109 list_display = ('__unicode__', 'flag_date', 'get_profile_url')
bgneal@207 110 actions = ('accept_flags', )
bgneal@207 111
bgneal@207 112 def accept_flags(self, request, qs):
bgneal@207 113 """This action awards a security pin to the user that reported the
bgneal@207 114 profile and then deletes the flag.
bgneal@207 115 """
bgneal@207 116 for flag in qs:
bgneal@207 117 bio.badges.award_badge(bio.badges.SECURITY_PIN, flag.user)
bgneal@207 118 flag.delete()
bgneal@207 119
bgneal@207 120 accept_flags.short_description = "Accept selected flagged profiles"
bgneal@207 121
bgneal@207 122
bgneal@207 123
bgneal@138 124
bgneal@138 125
bgneal@204 126 class BadgeAdmin(admin.ModelAdmin):
bgneal@204 127 list_display = ('name', 'html', 'order', 'numeric_id', 'description')
bgneal@204 128 list_editable = ('order', 'numeric_id')
bgneal@204 129
bgneal@204 130
bgneal@204 131 admin.site.register(bio.models.UserProfile, UserProfileAdmin)
bgneal@204 132 admin.site.register(bio.models.UserProfileFlag, UserProfileFlagAdmin)
bgneal@204 133 admin.site.register(bio.models.Badge, BadgeAdmin)