Last fix
[auf_django_auth_token.git] / auf / django / auth_token / admin.py
1 from django.contrib import admin
2 from django.contrib.auth.models import User
3 from django.contrib.auth.admin import UserAdmin
4 from .models import Token
5
6
7 def reset_token(modeladmin, request, qs):
8 for user in qs.select_related('auf_auth_token'):
9 user.auf_auth_token.reset_token()
10 reset_token.short_description = 'Reset token'
11
12
13 def send_token_by_email(modeladmin, request, qs):
14 for user in qs.select_related('auf_auth_token'):
15 user.auf_auth_token.send_token_by_email(request)
16 send_token_by_email.short_description = 'Send token email'
17
18
19 class TokenInline(admin.StackedInline):
20 extra = 1
21 model = Token
22
23
24 class TokenUserAdmin(UserAdmin):
25 def get_token(self, obj):
26 return obj.auf_auth_token.value
27 get_token.short_description = 'Login token'
28
29 def queryset(self, request):
30 return super(TokenUserAdmin, self).queryset(request).select_related('auf_auth_token')
31
32 inlines = UserAdmin.inlines + [TokenInline]
33 list_display = list(UserAdmin.list_display) + ['get_token']
34 actions = UserAdmin.actions + [
35 reset_token,
36 send_token_by_email,
37 ]
38
39
40 admin.site.unregister(User)
41 admin.site.register(User, TokenUserAdmin)