blender-id/bid_api/admin.py

58 lines
1.5 KiB
Python

import json
from typing import Type
from functools import lru_cache
from django.contrib import admin
from django.contrib.admin import ModelAdmin
from django.urls import reverse
import django.db.models
from . import models
from bid_main.admin_decorators import short_description
@lru_cache(maxsize=100)
def _load_task_params(task_params_str):
return json.loads(task_params_str)
@lru_cache(maxsize=100)
def _get_task_webhook(webhook_pk):
return models.Webhook.objects.get(pk=webhook_pk)
def _get_admin_url_name(model: Type['django.db.models.Model'], action: str = 'change') -> str:
return 'admin:{}_{}_{}'.format(model._meta.app_label, model._meta.model_name, action)
def get_admin_change_url(model: Type['django.db.models.Model'], pk: int) -> str:
"""Return an admin URL to admin change view for the given object."""
return reverse(_get_admin_url_name(model), args=[pk])
@short_description("Disable selected webhooks")
def disable_webhook(modeladmin, request, queryset):
queryset.update(enabled=False)
@short_description("Enable selected webhooks")
def enable_webhook(modeladmin, request, queryset):
queryset.update(enabled=True)
@admin.register(models.Webhook)
class WebhookAdmin(ModelAdmin):
list_display = (
"name",
"hook_type",
"app",
"url",
"enabled",
)
list_display_links = ("name", "hook_type", "url")
list_filter = ("hook_type", "enabled")
search_fields = ("name", "hook_type", "url")
ordering = ("name",)
actions = [disable_webhook, enable_webhook]