Files
chunyu_project/logs/admin.py
T
2026-08-05 23:59:15 +08:00

81 lines
2.7 KiB
Python

from django.contrib import admin
from django.utils import timezone
from .models import ApiRequestLog, ErrorLog, SystemEventLog
@admin.register(ApiRequestLog)
class ApiRequestLogAdmin(admin.ModelAdmin):
list_display = [
'timestamp', 'method', 'path', 'status_code', 'duration_ms',
'username', 'ip_address', 'app_name', 'is_error',
]
list_filter = ['method', 'status_code', 'app_name', 'is_error', 'timestamp']
search_fields = ['path', 'username', 'ip_address']
readonly_fields = [
'timestamp', 'user', 'username', 'ip_address', 'user_agent',
'method', 'path', 'query_string', 'request_body', 'status_code',
'response_size', 'duration_ms', 'app_name', 'is_error',
]
ordering = ['-timestamp']
date_hierarchy = 'timestamp'
def has_add_permission(self, request):
return False
def has_change_permission(self, request, obj=None):
return False
def has_delete_permission(self, request, obj=None):
return True
@admin.register(ErrorLog)
class ErrorLogAdmin(admin.ModelAdmin):
list_display = [
'timestamp', 'level', 'exception_type', 'module', 'message_short',
'is_resolved', 'occurrence_count',
]
list_filter = ['level', 'is_resolved', 'module', 'timestamp']
search_fields = ['message', 'exception_type', 'module', 'path']
readonly_fields = [
'timestamp', 'level', 'message', 'module', 'function',
'line_number', 'exception_type', 'traceback', 'user', 'ip_address',
'path', 'request_data', 'hash_key', 'occurrence_count',
]
ordering = ['-timestamp']
date_hierarchy = 'timestamp'
actions = ['mark_as_resolved']
def message_short(self, obj):
return obj.message[:100] + '...' if len(obj.message) > 100 else obj.message
message_short.short_description = '消息'
@admin.action(description='标记为已处理')
def mark_as_resolved(self, request, queryset):
queryset.update(is_resolved=True, resolved_at=timezone.now())
def has_add_permission(self, request):
return False
@admin.register(SystemEventLog)
class SystemEventLogAdmin(admin.ModelAdmin):
list_display = [
'timestamp', 'event_type', 'user', 'target_user', 'description',
'result', 'ip_address',
]
list_filter = ['event_type', 'result', 'timestamp']
search_fields = ['user__username', 'target_user__username', 'description']
readonly_fields = [
'timestamp', 'event_type', 'user', 'target_user', 'description',
'metadata', 'ip_address', 'user_agent', 'result',
]
ordering = ['-timestamp']
date_hierarchy = 'timestamp'
def has_add_permission(self, request):
return False
def has_change_permission(self, request, obj=None):
return False