annotate bio/admin.py @ 629:f4c043cf55ac

Wiki integration. Requests don't always have sessions. In particular this occurs when a request is made without a trailing slash. The Common middleware redirects when this happens, and the middleware process_request() processing stops before a session can get added. So just set an attribute on the request object for each operation. This seemed weird to me at first, but there are plenty of examples of this in the Django code base already.
author Brian Neal <bgneal@gmail.com>
date Tue, 13 Nov 2012 13:50:06 -0600
parents ee87ea74d46b
children 84865fcd7c26
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@563 13 from antispam.utils import deactivate_spammer
bgneal@147 14
gremmie@1 15
bgneal@204 16 class BadgeOwnerInline(admin.TabularInline):
bgneal@204 17 model = bio.models.BadgeOwnership
bgneal@204 18 extra = 1
bgneal@204 19
bgneal@204 20
gremmie@1 21 class UserProfileAdmin(admin.ModelAdmin):
bgneal@138 22 search_fields = ('user__username', 'user__first_name', 'user__last_name',
bgneal@138 23 'user__email')
gremmie@1 24 exclude = ('profile_html', 'signature_html')
bgneal@215 25 list_display = ('__unicode__', 'user_is_active', 'get_status_display', 'status_date')
bgneal@277 26 readonly_fields = ('status', 'status_date', 'update_date')
bgneal@147 27 list_filter = ('status', )
bgneal@147 28 date_hierarchy = 'status_date'
bgneal@204 29 inlines = (BadgeOwnerInline, )
bgneal@147 30 actions = (
bgneal@347 31 'mark_active',
bgneal@147 32 'mark_resigned',
bgneal@147 33 'mark_removed',
bgneal@147 34 'mark_suspended',
bgneal@147 35 'mark_spammer',
bgneal@215 36 'mark_stranger',
bgneal@147 37 )
bgneal@147 38
bgneal@147 39 def get_status_display(self, obj):
bgneal@147 40 return obj.get_status_display()
bgneal@147 41 get_status_display.short_description = 'Status'
bgneal@147 42
bgneal@147 43 def mark_user_status(self, request, qs, status):
bgneal@147 44 """
bgneal@347 45 Common code for the admin actions. Updates the status field in the
bgneal@347 46 profiles to 'status'. Updates the status_date. Sets the is_active
bgneal@147 47 field to True if the status is STA_ACTIVE and False otherwise.
bgneal@147 48 """
bgneal@147 49 now = datetime.datetime.now()
bgneal@147 50 for profile in qs:
bgneal@215 51 profile.user.is_active = (status == bio.models.STA_ACTIVE or
bgneal@215 52 status == bio.models.STA_STRANGER)
bgneal@147 53 profile.user.save()
bgneal@147 54 profile.status = status
bgneal@147 55 profile.status_date = now
bgneal@562 56 profile.save(content_update=False)
bgneal@147 57
bgneal@398 58 count = len(qs)
bgneal@147 59 msg = "1 user" if count == 1 else "%d users" % count
bgneal@147 60 self.message_user(request, "%s successfully marked as %s." % (msg,
bgneal@147 61 bio.models.USER_STATUS_CHOICES[status][1]))
bgneal@147 62
bgneal@147 63 def mark_active(self, request, qs):
bgneal@147 64 """
bgneal@147 65 Marks users as active. Updates their profile status to STA_ACTIVE.
bgneal@147 66 """
bgneal@147 67 self.mark_user_status(request, qs, bio.models.STA_ACTIVE)
bgneal@147 68 mark_active.short_description = "Mark selected users as active"
bgneal@147 69
bgneal@147 70 def mark_resigned(self, request, qs):
bgneal@147 71 """
bgneal@147 72 Marks users as inactive. Updates their profile status to STA_RESIGNED.
bgneal@147 73 """
bgneal@147 74 self.mark_user_status(request, qs, bio.models.STA_RESIGNED)
bgneal@147 75 mark_resigned.short_description = "Mark selected users as resigned"
bgneal@147 76
bgneal@147 77 def mark_removed(self, request, qs):
bgneal@147 78 """
bgneal@147 79 Marks users as inactive. Updates their profile status to STA_REMOVED.
bgneal@147 80 """
bgneal@147 81 self.mark_user_status(request, qs, bio.models.STA_REMOVED)
bgneal@147 82 mark_removed.short_description = "Mark selected users as removed"
bgneal@147 83
bgneal@147 84 def mark_suspended(self, request, qs):
bgneal@147 85 """
bgneal@147 86 Marks users as inactive. Updates their profile status to STA_SUSPENDED.
bgneal@147 87 """
bgneal@147 88 self.mark_user_status(request, qs, bio.models.STA_SUSPENDED)
bgneal@147 89 mark_suspended.short_description = "Mark selected users as suspended"
bgneal@147 90
bgneal@147 91 def mark_spammer(self, request, qs):
bgneal@147 92 """
bgneal@563 93 Calls deactivate_spammer() on each user in the profile queryset.
bgneal@563 94
bgneal@147 95 """
bgneal@563 96 count = qs.count()
bgneal@147 97 for profile in qs:
bgneal@563 98 deactivate_spammer(profile.user)
bgneal@563 99
bgneal@563 100 self.message_user(request,
bgneal@563 101 "%s profile(s) successfully marked as spammers." % count)
bgneal@563 102
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@563 114 list_display = ['__unicode__', 'flag_date', 'get_profile_url']
bgneal@563 115 actions = ['accept_flags']
bgneal@563 116 raw_id_fields = ['user', 'profile']
bgneal@207 117
bgneal@207 118 def accept_flags(self, request, qs):
bgneal@207 119 """
bgneal@563 120 This action awards a security pin to the user that reported the
bgneal@563 121 profile, deletes the flags, then deactivates the spammers.
bgneal@563 122 """
bgneal@563 123 count = qs.count()
bgneal@207 124 for flag in qs:
bgneal@563 125 deactivate_spammer(flag.profile.user)
bgneal@207 126 bio.badges.award_badge(bio.badges.SECURITY_PIN, flag.user)
bgneal@207 127 flag.delete()
bgneal@207 128
bgneal@563 129 self.message_user(request,
bgneal@563 130 "%s profile(s) successfully marked as spammers." % count)
bgneal@563 131
bgneal@563 132 accept_flags.short_description = "Mark selected profiles as spammers"
bgneal@207 133
bgneal@207 134
bgneal@204 135 class BadgeAdmin(admin.ModelAdmin):
bgneal@204 136 list_display = ('name', 'html', 'order', 'numeric_id', 'description')
bgneal@204 137 list_editable = ('order', 'numeric_id')
bgneal@204 138
bgneal@204 139
bgneal@347 140 # We like the User admin but would like a date hierarcy on date_joined.
bgneal@347 141 class UserAdmin(django.contrib.auth.admin.UserAdmin):
bgneal@347 142 date_hierarchy = 'date_joined'
bgneal@347 143
bgneal@347 144
bgneal@204 145 admin.site.register(bio.models.UserProfile, UserProfileAdmin)
bgneal@204 146 admin.site.register(bio.models.UserProfileFlag, UserProfileFlagAdmin)
bgneal@204 147 admin.site.register(bio.models.Badge, BadgeAdmin)
bgneal@347 148
bgneal@347 149 # Unregister existing ModelAdmin for User, then register ours
bgneal@347 150 admin.site.unregister(django.contrib.auth.models.User)
bgneal@347 151 admin.site.register(django.contrib.auth.models.User, UserAdmin)