70 lines
2.7 KiB
Python
70 lines
2.7 KiB
Python
# pxy_bots/admin.py
|
|
from django.contrib import admin
|
|
from .models import TelegramBot, TelegramConversation, TelegramMessage, Connection, CommandRoute
|
|
|
|
# ---- Connections ----
|
|
@admin.register(Connection)
|
|
class ConnectionAdmin(admin.ModelAdmin):
|
|
list_display = ("name", "base_url", "auth_type", "timeout_s", "is_active")
|
|
list_filter = ("auth_type", "is_active")
|
|
search_fields = ("name", "base_url")
|
|
readonly_fields = ("created_at", "updated_at")
|
|
fieldsets = (
|
|
(None, {
|
|
"fields": ("name", "is_active")
|
|
}),
|
|
("Endpoint", {
|
|
"fields": ("base_url", "path_default", "timeout_s", "allowed_hosts")
|
|
}),
|
|
("Auth & Headers", {
|
|
"fields": ("auth_type", "auth_value", "headers_json"),
|
|
"description": "Optional headers in JSON (e.g. {\"X-Tenant\":\"mx\"})."
|
|
}),
|
|
("Timestamps", {
|
|
"fields": ("created_at", "updated_at")
|
|
}),
|
|
)
|
|
|
|
# ---- Command routes inline under each bot ----
|
|
class CommandRouteInline(admin.TabularInline):
|
|
model = CommandRoute
|
|
extra = 0
|
|
fields = ("enabled", "priority", "trigger", "command_name", "connection", "path", "note")
|
|
ordering = ("priority", "id")
|
|
autocomplete_fields = ("connection",)
|
|
show_change_link = True
|
|
|
|
@admin.register(CommandRoute)
|
|
class CommandRouteAdmin(admin.ModelAdmin):
|
|
list_display = ("bot", "enabled", "priority", "trigger", "command_name", "connection", "path")
|
|
list_filter = ("enabled", "trigger", "bot", "connection")
|
|
search_fields = ("command_name", "note", "bot__name", "connection__name")
|
|
ordering = ("bot__name", "priority", "id")
|
|
|
|
# ---- Bots (with routes inline) ----
|
|
@admin.register(TelegramBot)
|
|
class TelegramBotAdmin(admin.ModelAdmin):
|
|
list_display = ("name", "username", "token_preview", "is_active", "created_at")
|
|
list_filter = ("is_active",)
|
|
search_fields = ("name", "username")
|
|
inlines = [CommandRouteInline]
|
|
readonly_fields = ("created_at", "updated_at")
|
|
|
|
def token_preview(self, obj):
|
|
return f"{obj.token[:8]}…{obj.token[-4:]}" if obj.token else "—"
|
|
token_preview.short_description = "Token"
|
|
|
|
# ---- (Optional) conversation/message logs, already simple ----
|
|
@admin.register(TelegramConversation)
|
|
class TelegramConversationAdmin(admin.ModelAdmin):
|
|
list_display = ("bot", "user_id", "started_at")
|
|
list_filter = ("bot",)
|
|
search_fields = ("user_id",)
|
|
|
|
@admin.register(TelegramMessage)
|
|
class TelegramMessageAdmin(admin.ModelAdmin):
|
|
list_display = ("conversation", "direction", "short", "timestamp", "response_time_ms")
|
|
list_filter = ("direction",)
|
|
search_fields = ("content",)
|
|
def short(self, obj): return (obj.content or "")[:60]
|