add config switch_status example

This commit is contained in:
long2ice
2021-05-14 16:42:38 +08:00
parent 88ae0f9278
commit c866614e47
5 changed files with 139 additions and 117 deletions

View File

@@ -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):

View File

@@ -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"))