Skip to content
Snippets Groups Projects
Commit 5a91d884 authored by Andreas Nüßlein's avatar Andreas Nüßlein Committed by Andreas Nüßlein (sinnwerkstatt)
Browse files

django admin token handling improvement

(cherry picked from commit 17b09e4f)
parent 738b7a57
Branches
No related tags found
No related merge requests found
from django import forms from django import forms
from django.contrib import admin from django.contrib import admin
from django.contrib.admin import SimpleListFilter
from django.contrib.auth import admin as auth_admin from django.contrib.auth import admin as auth_admin
from django.contrib.auth.models import Group from django.contrib.auth.models import Group
from apps.accounts.models import User from apps.accounts.models import User
from apps.api.models import NoteToken
class CustomUserChangeForm(forms.ModelForm): class CustomUserChangeForm(forms.ModelForm):
...@@ -19,12 +21,30 @@ class CustomUserChangeForm(forms.ModelForm): ...@@ -19,12 +21,30 @@ class CustomUserChangeForm(forms.ModelForm):
pass pass
class UserAdmin(auth_admin.UserAdmin): class TokenAdmin(admin.StackedInline):
""" max_num = 1
The representation of :class:`accounts.models.User` in the min_num = 0
administration interface. model = NoteToken
""" can_delete = False
show_change_link = True
readonly_fields = ["key"]
class TokenFilter(SimpleListFilter):
title = "has_token"
parameter_name = "has_token"
def lookups(self, request, model_admin):
return [("", "True"), ("x", "False")]
def queryset(self, request, queryset):
if self.value() == "":
return queryset.filter(auth_token__isnull=False)
if self.value():
return queryset.filter(auth_token__isnull=True)
class UserAdmin(auth_admin.UserAdmin):
form = CustomUserChangeForm form = CustomUserChangeForm
list_display = ( list_display = (
...@@ -35,8 +55,9 @@ class UserAdmin(auth_admin.UserAdmin): ...@@ -35,8 +55,9 @@ class UserAdmin(auth_admin.UserAdmin):
"is_superuser", "is_superuser",
"date_joined", "date_joined",
"last_login", "last_login",
"auth_token",
) )
list_filter = ("is_superuser", "groups") list_filter = ("is_superuser", "groups", TokenFilter)
search_fields = ("email",) search_fields = ("email",)
ordering = ("email",) ordering = ("email",)
filter_horizontal = ("groups",) filter_horizontal = ("groups",)
...@@ -47,6 +68,8 @@ class UserAdmin(auth_admin.UserAdmin): ...@@ -47,6 +68,8 @@ class UserAdmin(auth_admin.UserAdmin):
) )
readonly_fields = ("email",) readonly_fields = ("email",)
inlines = [TokenAdmin]
# No new users can be added. # No new users can be added.
def has_add_permission(self, request): def has_add_permission(self, request):
return False return False
......
...@@ -12,5 +12,6 @@ class RequestLogAdmin(admin.ModelAdmin): ...@@ -12,5 +12,6 @@ class RequestLogAdmin(admin.ModelAdmin):
@admin.register(NoteToken) @admin.register(NoteToken)
class NoteTokenAdmin(admin.ModelAdmin): class NoteTokenAdmin(admin.ModelAdmin):
fields = ["user", "notes"] fields = ["user", "notes", "key"]
readonly_fields = ["key"]
list_display = ["__str__", "user", "created", "requests_from_user"] list_display = ["__str__", "user", "created", "requests_from_user"]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment