File size: 1,235 Bytes
9d9d2a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import gradio as gr
from src.services.auth_service import is_admin
from src.db.connection import get_conn
from src.db.audit import recent as recent_audit

def render_admin_tab(db_path: str, auth_state: gr.State):
    with gr.Column() as panel:
        gr.Markdown("## Admin")

        info = gr.Markdown("Login as **Dr** to access admin features.")
        refresh = gr.Button("Refresh Audit (Admin)")
        table = gr.Dataframe(
            headers=["entity","entity_id","action","note","changed_by","changed_at_utc"],
            interactive=False,
            row_count=15,
            col_count=6,
        )

    def _refresh(auth):
        if not auth:
            return "Please login.", []
        if not is_admin(auth):
            return "❌ Admin only.", []
        conn = get_conn(db_path)
        try:
            rows = recent_audit(conn, limit=30)
        finally:
            conn.close()
        data = [[r["entity"], r["entity_id"], r["action"], r["note"], r["changed_by"], r["changed_at_utc"]] for r in rows]
        return "✅ Audit loaded.", data

    refresh.click(_refresh, inputs=[auth_state], outputs=[info, table])
    panel.load(_refresh, inputs=[auth_state], outputs=[info, table])

    return panel, {}