feat: add Daily Report to admin sidebar via custom AdminSite
- Create OpenClawAdminSite (openclaw_admin) with Daily Reports menu in sidebar - Re-register Session/Message/ToolCall with custom site - Add daily/daily-reports URLs at AdminSite level - Override admin/index.html template for sidebar item - Use self.admin_view (not admin.site.admin_view) for site-aware view wrapping - Clean up admin.py: remove inline URLs from SessionAdmin (now at site level)
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
from django.contrib import admin
|
|
||||||
from django.urls import path, include
|
from django.urls import path, include
|
||||||
|
from openclaw.admin_custom_site import openclaw_admin_site
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("admin/", admin.site.urls),
|
path("admin/", openclaw_admin_site.urls),
|
||||||
path("api/", include("openclaw.urls")),
|
path("api/", include("openclaw.urls")),
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.template.response import TemplateResponse
|
|
||||||
|
|
||||||
from openclaw.export import export_daily_markdown
|
from openclaw.export import export_daily_markdown
|
||||||
from openclaw.models import Session, Message, ToolCall
|
from openclaw.models import Session, Message, ToolCall
|
||||||
from openclaw.admin_new_views import daily_report_list_view, daily_report_detail_view
|
|
||||||
|
|
||||||
|
|
||||||
class MessageInline(admin.TabularInline):
|
class MessageInline(admin.TabularInline):
|
||||||
@@ -45,42 +43,18 @@ def export_to_markdown(modeladmin, request, queryset):
|
|||||||
class SessionAdmin(admin.ModelAdmin):
|
class SessionAdmin(admin.ModelAdmin):
|
||||||
actions = [export_to_markdown]
|
actions = [export_to_markdown]
|
||||||
list_display = (
|
list_display = (
|
||||||
"session_id",
|
"session_id", "agent_name", "model_id", "total_tokens",
|
||||||
"agent_name",
|
"message_count", "tool_call_count", "status", "start_time",
|
||||||
"model_id",
|
|
||||||
"total_tokens",
|
|
||||||
"message_count",
|
|
||||||
"tool_call_count",
|
|
||||||
"status",
|
|
||||||
"start_time",
|
|
||||||
)
|
)
|
||||||
list_filter = ("agent_name", "source_node", "model_id", "start_time")
|
list_filter = ("agent_name", "source_node", "model_id", "start_time")
|
||||||
search_fields = ("session_id", "cwd")
|
search_fields = ("session_id", "cwd")
|
||||||
ordering = ("-start_time",)
|
ordering = ("-start_time",)
|
||||||
inlines = [MessageInline, ToolCallInline]
|
inlines = [MessageInline, ToolCallInline]
|
||||||
readonly_fields = (
|
readonly_fields = (
|
||||||
"session_id",
|
"session_id", "agent_name", "source_node",
|
||||||
"agent_name",
|
"start_time", "end_time", "pushed_at",
|
||||||
"source_node",
|
|
||||||
"start_time",
|
|
||||||
"end_time",
|
|
||||||
"pushed_at",
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_urls(self):
|
|
||||||
from django.urls import path
|
|
||||||
urls = super().get_urls()
|
|
||||||
custom_urls = [
|
|
||||||
path("daily/", admin.site.admin_view(daily_report_list_view), name="openclaw_daily"),
|
|
||||||
path("daily-reports/", admin.site.admin_view(daily_report_list_view), name="openclaw_daily_reports"),
|
|
||||||
path(
|
|
||||||
"daily-reports/<str:agent_name>/<int:year>-<int:month>-<int:day>/",
|
|
||||||
admin.site.admin_view(daily_report_detail_view),
|
|
||||||
name="openclaw_daily_report_detail",
|
|
||||||
),
|
|
||||||
]
|
|
||||||
return custom_urls + urls
|
|
||||||
|
|
||||||
|
|
||||||
@admin.register(Message)
|
@admin.register(Message)
|
||||||
class MessageAdmin(admin.ModelAdmin):
|
class MessageAdmin(admin.ModelAdmin):
|
||||||
|
|||||||
52
src/openclaw/admin_custom_site.py
Normal file
52
src/openclaw/admin_custom_site.py
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
"""
|
||||||
|
Custom AdminSite for OpenClaw — adds Daily Report to sidebar + URLs.
|
||||||
|
Re-registers Session/Message/ToolCall from admin.py, and adds custom report URLs.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from django.contrib.admin import AdminSite
|
||||||
|
|
||||||
|
from openclaw.admin_new_views import daily_report_list_view, daily_report_detail_view
|
||||||
|
|
||||||
|
|
||||||
|
class OpenClawAdminSite(AdminSite):
|
||||||
|
site_header = "OpenClaw Archive"
|
||||||
|
site_title = "OpenClaw Admin"
|
||||||
|
index_title = "Agent Sessions"
|
||||||
|
|
||||||
|
def get_urls(self):
|
||||||
|
from django.urls import path
|
||||||
|
urls = super().get_urls()
|
||||||
|
custom_urls = [
|
||||||
|
path(
|
||||||
|
"daily/",
|
||||||
|
self.admin_view(daily_report_list_view),
|
||||||
|
name="openclaw_daily",
|
||||||
|
),
|
||||||
|
path(
|
||||||
|
"daily-reports/",
|
||||||
|
self.admin_view(daily_report_list_view),
|
||||||
|
name="openclaw_daily_reports",
|
||||||
|
),
|
||||||
|
path(
|
||||||
|
"daily-reports/<str:agent_name>/<int:year>-<int:month>-<int:day>/",
|
||||||
|
self.admin_view(daily_report_detail_view),
|
||||||
|
name="openclaw_daily_report_detail",
|
||||||
|
),
|
||||||
|
]
|
||||||
|
return custom_urls + urls
|
||||||
|
|
||||||
|
def index(self, request, extra_context=None):
|
||||||
|
extra_context = extra_context or {}
|
||||||
|
extra_context["show_daily_report"] = True
|
||||||
|
return super().index(request, extra_context)
|
||||||
|
|
||||||
|
|
||||||
|
openclaw_admin_site = OpenClawAdminSite(name="openclaw_admin")
|
||||||
|
|
||||||
|
# Re-register models with this custom site (imported from admin.py)
|
||||||
|
from openclaw.admin import SessionAdmin, MessageAdmin, ToolCallAdmin
|
||||||
|
from openclaw.models import Session, Message, ToolCall
|
||||||
|
|
||||||
|
openclaw_admin_site.register(Session, SessionAdmin)
|
||||||
|
openclaw_admin_site.register(Message, MessageAdmin)
|
||||||
|
openclaw_admin_site.register(ToolCall, ToolCallAdmin)
|
||||||
24
src/templates/admin/index.html
Normal file
24
src/templates/admin/index.html
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
{% extends "admin/index.html" %}
|
||||||
|
{% load static %}
|
||||||
|
|
||||||
|
{% block branding %}
|
||||||
|
<h1 id="site-name">
|
||||||
|
<a href="{% url 'openclaw_admin:index' %}">{{ site_header|default:"Django Admin" }}</a>
|
||||||
|
</h1>
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block nav-sidebar %}
|
||||||
|
{{ block.super }}
|
||||||
|
|
||||||
|
{# Daily Report custom sidebar item #}
|
||||||
|
<div class="app-openclaw module">
|
||||||
|
<table>
|
||||||
|
<caption>
|
||||||
|
<a href="{% url 'openclaw_admin:openclaw_daily_reports' %}" class="section">
|
||||||
|
📊 Daily Reports
|
||||||
|
</a>
|
||||||
|
</caption>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% endblock %}
|
||||||
Reference in New Issue
Block a user