mirror of
https://github.com/fastapi-admin/fastapi-admin.git
synced 2026-03-13 10:32:25 +08:00
add config switch_status example
This commit is contained in:
@@ -126,6 +126,18 @@ class ConfigResource(Model):
|
||||
return {"class": "bg-green text-white"}
|
||||
return await super().row_attributes(request, obj)
|
||||
|
||||
async def get_actions(self, request: Request) -> List[Action]:
|
||||
actions = await super().get_actions(request)
|
||||
switch_status = Action(
|
||||
label="Switch Status",
|
||||
icon="ti ti-toggle-left",
|
||||
name="switch_status",
|
||||
method="GET",
|
||||
ajax=False,
|
||||
)
|
||||
actions.append(switch_status)
|
||||
return actions
|
||||
|
||||
|
||||
@app.register
|
||||
class GithubLink(Link):
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
from fastapi import Depends
|
||||
from starlette.responses import RedirectResponse
|
||||
from starlette.status import HTTP_404_NOT_FOUND
|
||||
|
||||
from examples.models import Config
|
||||
from fastapi import Depends, HTTPException
|
||||
from starlette.requests import Request
|
||||
|
||||
from fastapi_admin.app import app
|
||||
@@ -21,3 +25,13 @@ async def home(
|
||||
"page_title": "Home page",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@app.get("/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"))
|
||||
|
||||
Reference in New Issue
Block a user