annotate bio/admin.py @ 931:1354c3d651eb

Fix admin dashboard for Django 1.7.7 upgrade.
author Brian Neal <bgneal@gmail.com>
date Wed, 15 Apr 2015 20:27:05 -0500
parents 6a06080e7ca8
children eeaf387803c6
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@663 22 search_fields = ['user__username', 'user__first_name', 'user__last_name',
bgneal@663 23 'user__email']
bgneal@663 24 exclude = ['profile_html', 'signature_html']
bgneal@663 25 list_display = ['__unicode__', 'user_is_active', 'get_status_display', 'status_date']
bgneal@663 26 readonly_fields = ['status', 'status_date', 'update_date']
bgneal@663 27 list_filter = ['status', ]
bgneal@147 28 date_hierarchy = 'status_date'
bgneal@663 29 inlines = [BadgeOwnerInline, ]
bgneal@663 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@663 37 'unsubscribe_forums',
bgneal@663 38 ]
bgneal@147 39
bgneal@790 40 def has_delete_permission(self, request, object=None):
bgneal@790 41 # We don't want to delete a user profile; it is tied to the user object
bgneal@790 42 return False
bgneal@790 43
bgneal@790 44 def get_actions(self, request):
bgneal@790 45 # We don't want to delete a user profile; it is tied to the user object
bgneal@790 46 actions = super(UserProfileAdmin, self).get_actions(request)
bgneal@790 47 del actions['delete_selected']
bgneal@790 48 return actions
bgneal@790 49
bgneal@147 50 def get_status_display(self, obj):
bgneal@147 51 return obj.get_status_display()
bgneal@147 52 get_status_display.short_description = 'Status'
bgneal@147 53
bgneal@147 54 def mark_user_status(self, request, qs, status):
bgneal@147 55 """
bgneal@347 56 Common code for the admin actions. Updates the status field in the
bgneal@347 57 profiles to 'status'. Updates the status_date. Sets the is_active
bgneal@147 58 field to True if the status is STA_ACTIVE and False otherwise.
bgneal@147 59 """
bgneal@147 60 now = datetime.datetime.now()
bgneal@147 61 for profile in qs:
bgneal@215 62 profile.user.is_active = (status == bio.models.STA_ACTIVE or
bgneal@215 63 status == bio.models.STA_STRANGER)
bgneal@147 64 profile.user.save()
bgneal@147 65 profile.status = status
bgneal@147 66 profile.status_date = now
bgneal@562 67 profile.save(content_update=False)
bgneal@147 68
bgneal@398 69 count = len(qs)
bgneal@147 70 msg = "1 user" if count == 1 else "%d users" % count
bgneal@147 71 self.message_user(request, "%s successfully marked as %s." % (msg,
bgneal@147 72 bio.models.USER_STATUS_CHOICES[status][1]))
bgneal@147 73
bgneal@147 74 def mark_active(self, request, qs):
bgneal@147 75 """
bgneal@147 76 Marks users as active. Updates their profile status to STA_ACTIVE.
bgneal@147 77 """
bgneal@147 78 self.mark_user_status(request, qs, bio.models.STA_ACTIVE)
bgneal@147 79 mark_active.short_description = "Mark selected users as active"
bgneal@147 80
bgneal@147 81 def mark_resigned(self, request, qs):
bgneal@147 82 """
bgneal@147 83 Marks users as inactive. Updates their profile status to STA_RESIGNED.
bgneal@147 84 """
bgneal@147 85 self.mark_user_status(request, qs, bio.models.STA_RESIGNED)
bgneal@147 86 mark_resigned.short_description = "Mark selected users as resigned"
bgneal@147 87
bgneal@147 88 def mark_removed(self, request, qs):
bgneal@147 89 """
bgneal@147 90 Marks users as inactive. Updates their profile status to STA_REMOVED.
bgneal@147 91 """
bgneal@147 92 self.mark_user_status(request, qs, bio.models.STA_REMOVED)
bgneal@147 93 mark_removed.short_description = "Mark selected users as removed"
bgneal@147 94
bgneal@147 95 def mark_suspended(self, request, qs):
bgneal@147 96 """
bgneal@147 97 Marks users as inactive. Updates their profile status to STA_SUSPENDED.
bgneal@147 98 """
bgneal@147 99 self.mark_user_status(request, qs, bio.models.STA_SUSPENDED)
bgneal@147 100 mark_suspended.short_description = "Mark selected users as suspended"
bgneal@147 101
bgneal@147 102 def mark_spammer(self, request, qs):
bgneal@147 103 """
bgneal@563 104 Calls deactivate_spammer() on each user in the profile queryset.
bgneal@563 105
bgneal@147 106 """
bgneal@563 107 count = qs.count()
bgneal@147 108 for profile in qs:
bgneal@563 109 deactivate_spammer(profile.user)
bgneal@563 110
bgneal@563 111 self.message_user(request,
bgneal@563 112 "%s profile(s) successfully marked as spammers." % count)
bgneal@563 113
bgneal@147 114 mark_spammer.short_description = "Mark selected users as spammers"
gremmie@1 115
bgneal@215 116 def mark_stranger(self, request, qs):
bgneal@215 117 """
bgneal@215 118 Marks users as strangers. Updates their profile status to STA_STRANGER.
bgneal@215 119 """
bgneal@215 120 self.mark_user_status(request, qs, bio.models.STA_STRANGER)
bgneal@215 121 mark_stranger.short_description = "Mark selected users as strangers"
bgneal@215 122
bgneal@663 123 def unsubscribe_forums(self, request, qs):
bgneal@663 124 """Delete users forum topic subscriptions."""
bgneal@663 125 for profile in qs:
bgneal@663 126 profile.user.subscriptions.clear()
bgneal@663 127
bgneal@663 128 self.message_user(request, "%s subscription(s) deleted." % qs.count())
bgneal@663 129
bgneal@663 130 unsubscribe_forums.short_description = "Delete users' forum subscriptions"
bgneal@663 131
bgneal@138 132
bgneal@138 133 class UserProfileFlagAdmin(admin.ModelAdmin):
bgneal@563 134 list_display = ['__unicode__', 'flag_date', 'get_profile_url']
bgneal@563 135 actions = ['accept_flags']
bgneal@563 136 raw_id_fields = ['user', 'profile']
bgneal@207 137
bgneal@207 138 def accept_flags(self, request, qs):
bgneal@207 139 """
bgneal@563 140 This action awards a security pin to the user that reported the
bgneal@563 141 profile, deletes the flags, then deactivates the spammers.
bgneal@563 142 """
bgneal@563 143 count = qs.count()
bgneal@207 144 for flag in qs:
bgneal@563 145 deactivate_spammer(flag.profile.user)
bgneal@207 146 bio.badges.award_badge(bio.badges.SECURITY_PIN, flag.user)
bgneal@207 147 flag.delete()
bgneal@207 148
bgneal@563 149 self.message_user(request,
bgneal@563 150 "%s profile(s) successfully marked as spammers." % count)
bgneal@563 151
bgneal@563 152 accept_flags.short_description = "Mark selected profiles as spammers"
bgneal@207 153
bgneal@207 154
bgneal@204 155 class BadgeAdmin(admin.ModelAdmin):
bgneal@204 156 list_display = ('name', 'html', 'order', 'numeric_id', 'description')
bgneal@204 157 list_editable = ('order', 'numeric_id')
bgneal@204 158
bgneal@204 159
bgneal@347 160 # We like the User admin but would like a date hierarcy on date_joined.
bgneal@347 161 class UserAdmin(django.contrib.auth.admin.UserAdmin):
bgneal@347 162 date_hierarchy = 'date_joined'
bgneal@347 163
bgneal@347 164
bgneal@204 165 admin.site.register(bio.models.UserProfile, UserProfileAdmin)
bgneal@204 166 admin.site.register(bio.models.UserProfileFlag, UserProfileFlagAdmin)
bgneal@204 167 admin.site.register(bio.models.Badge, BadgeAdmin)
bgneal@347 168
bgneal@347 169 # Unregister existing ModelAdmin for User, then register ours
bgneal@347 170 admin.site.unregister(django.contrib.auth.models.User)
bgneal@347 171 admin.site.register(django.contrib.auth.models.User, UserAdmin)