feat: add Daily Reports to admin sidebar via monkey-patch
- urls.py: add daily/ and daily-reports/ URL patterns directly - admin_new_views.py: use admin.site.each_context for full admin context - openclaw_daily apps.py: monkey-patch admin.site.get_app_list to inject Daily Reports item into sidebar app list (no circular import issues)
This commit is contained in:
@@ -1,7 +1,20 @@
|
|||||||
|
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
|
|
||||||
|
from openclaw.admin_new_views import (
|
||||||
|
daily_report_list_view,
|
||||||
|
daily_report_detail_view,
|
||||||
|
)
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("admin/", openclaw_admin_site.urls),
|
# Custom daily report URLs (before admin.site to take precedence)
|
||||||
|
path("admin/daily/", admin.site.admin_view(daily_report_list_view), name="openclaw_daily"),
|
||||||
|
path("admin/daily-reports/", admin.site.admin_view(daily_report_list_view), name="openclaw_daily_reports"),
|
||||||
|
path(
|
||||||
|
"admin/daily-reports/<str:agent_name>/<int:year>-<int:month>-<int:day>/",
|
||||||
|
admin.site.admin_view(daily_report_detail_view),
|
||||||
|
name="openclaw_daily_report_detail",
|
||||||
|
),
|
||||||
|
path("admin/", admin.site.urls),
|
||||||
path("api/", include("openclaw.urls")),
|
path("api/", include("openclaw.urls")),
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ from collections import defaultdict
|
|||||||
from datetime import date
|
from datetime import date
|
||||||
|
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.db.models import Count
|
|
||||||
from django.db.models.functions import TruncDate
|
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
from django.template.response import TemplateResponse
|
from django.template.response import TemplateResponse
|
||||||
|
|
||||||
@@ -17,10 +15,6 @@ from openclaw.models import Session, Message, ToolCall
|
|||||||
|
|
||||||
|
|
||||||
def daily_report_list_view(request):
|
def daily_report_list_view(request):
|
||||||
"""
|
|
||||||
List page: shows all (agent_name, date) combinations with message/session counts.
|
|
||||||
Filterable by agent and date range.
|
|
||||||
"""
|
|
||||||
start_str = request.GET.get("start")
|
start_str = request.GET.get("start")
|
||||||
end_str = request.GET.get("end")
|
end_str = request.GET.get("end")
|
||||||
agent_filter = request.GET.get("agent", "")
|
agent_filter = request.GET.get("agent", "")
|
||||||
@@ -63,10 +57,8 @@ def daily_report_list_view(request):
|
|||||||
key=lambda x: (-x["date_val"].toordinal(), x["agent_name"]),
|
key=lambda x: (-x["date_val"].toordinal(), x["agent_name"]),
|
||||||
)
|
)
|
||||||
|
|
||||||
# Use request.admin_site (set by AdminSite.admin_view wrapper)
|
|
||||||
admin_site = getattr(request, "admin_site", admin.site)
|
|
||||||
context = {
|
context = {
|
||||||
**admin_site.each_context(request),
|
**admin.site.each_context(request),
|
||||||
"start_date": start_date,
|
"start_date": start_date,
|
||||||
"end_date": end_date,
|
"end_date": end_date,
|
||||||
"selected_agent": agent_filter,
|
"selected_agent": agent_filter,
|
||||||
@@ -78,9 +70,6 @@ def daily_report_list_view(request):
|
|||||||
|
|
||||||
|
|
||||||
def daily_report_detail_view(request, agent_name, year, month, day):
|
def daily_report_detail_view(request, agent_name, year, month, day):
|
||||||
"""
|
|
||||||
Detail page: all messages for a specific agent on a specific date.
|
|
||||||
"""
|
|
||||||
target_date = date(int(year), int(month), int(day))
|
target_date = date(int(year), int(month), int(day))
|
||||||
|
|
||||||
sessions = (
|
sessions = (
|
||||||
@@ -111,9 +100,8 @@ def daily_report_detail_view(request, agent_name, year, month, day):
|
|||||||
"system": "System",
|
"system": "System",
|
||||||
}
|
}
|
||||||
|
|
||||||
admin_site = getattr(request, "admin_site", admin.site)
|
|
||||||
context = {
|
context = {
|
||||||
**admin_site.each_context(request),
|
**admin.site.each_context(request),
|
||||||
"agent_name": agent_name,
|
"agent_name": agent_name,
|
||||||
"target_date": target_date,
|
"target_date": target_date,
|
||||||
"sessions": sessions_with_messages,
|
"sessions": sessions_with_messages,
|
||||||
|
|||||||
@@ -1,6 +1,29 @@
|
|||||||
from django.apps import AppConfig
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
class OpenClawDailyConfig(AppConfig):
|
class OpenClawDailyConfig(AppConfig):
|
||||||
name = "openclaw_daily"
|
name = "openclaw_daily"
|
||||||
label = "openclaw_daily"
|
label = "openclaw_daily"
|
||||||
verbose_name = "Daily Reports"
|
verbose_name = "Daily Reports"
|
||||||
|
|
||||||
|
def ready(self):
|
||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
|
_orig = admin.site.get_app_list
|
||||||
|
|
||||||
|
def _patched(request, app_label=None):
|
||||||
|
app_list = _orig(request, app_label)
|
||||||
|
app_list.insert(0, {
|
||||||
|
"name": "Daily Reports",
|
||||||
|
"app_label": "openclaw_daily",
|
||||||
|
"app_url": "/admin/daily-reports/",
|
||||||
|
"models": [{
|
||||||
|
"name": "Daily Reports",
|
||||||
|
"object_name": "DailyReports",
|
||||||
|
"admin_url": "/admin/daily-reports/",
|
||||||
|
"view_only": True,
|
||||||
|
}],
|
||||||
|
})
|
||||||
|
return app_list
|
||||||
|
|
||||||
|
admin.site.get_app_list = _patched
|
||||||
|
|||||||
Reference in New Issue
Block a user