mirror of
https://github.com/fastapi-admin/fastapi-admin.git
synced 2025-08-06 18:49:47 +08:00
37 lines
1.1 KiB
Python
37 lines
1.1 KiB
Python
from fastapi import Depends, HTTPException
|
|
from starlette.requests import Request
|
|
from starlette.responses import RedirectResponse
|
|
from starlette.status import HTTP_303_SEE_OTHER, HTTP_404_NOT_FOUND
|
|
|
|
from examples.models import Config
|
|
from fastapi_admin.app import app
|
|
from fastapi_admin.depends import get_resources
|
|
from fastapi_admin.template import templates
|
|
|
|
|
|
@app.get("/")
|
|
async def home(
|
|
request: Request,
|
|
resources=Depends(get_resources),
|
|
):
|
|
return templates.TemplateResponse(
|
|
"dashboard.html",
|
|
context={
|
|
"request": request,
|
|
"resources": resources,
|
|
"resource_label": "Dashboard",
|
|
"page_pre_title": "overview",
|
|
"page_title": "Dashboard",
|
|
},
|
|
)
|
|
|
|
|
|
@app.put("/config/switch_status/{config_id}")
|
|
async def switch_config_status(request: Request, config_id: int):
|
|
config = await Config.get_or_none(pk=config_id)
|
|
if not config:
|
|
raise HTTPException(status_code=HTTP_404_NOT_FOUND)
|
|
config.status = not config.status
|
|
await config.save(update_fields=["status"])
|
|
return RedirectResponse(url=request.headers.get("referer"), status_code=HTTP_303_SEE_OTHER)
|