-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.py
39 lines (28 loc) · 1.3 KB
/
admin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from base64 import urlsafe_b64encode
from django.contrib import admin
from django.contrib import messages
from cylinder_head_api.celery import tasks_logger
from .models import AuthToken
from simple_history.admin import SimpleHistoryAdmin
class AuthTokenAdmin(SimpleHistoryAdmin):
list_display = ('user', 'created')
exclude = ('created',)
actions = ['delete_selected']
def delete_selected(self, request, queryset):
for obj in queryset:
obj.delete()
delete_selected.short_description = 'Удалить выбранные токены'
def message_user(self, request, message, level=messages.INFO, extra_tags='',
fail_silently=False):
pass
def save_model(self, request, obj, form, change):
token_response = AuthToken.create_token_for_user(request.user)
if token_response.status_code // 100 == 2:
token = token_response.data.get('token')
token_str = urlsafe_b64encode(token).decode()
messages.success(request, f"Токен успешно создан: {token_str}")
else:
messages.error(request, "Ошибка при создании токена")
def has_change_permission(self, request, obj=None):
return False
admin.site.register(AuthToken, AuthTokenAdmin)