mirror of
https://github.com/fastapi-admin/fastapi-admin.git
synced 2025-08-14 18:58:13 +08:00
Remove can_create
and add get_toolbar_actions
.
This commit is contained in:
@ -5,6 +5,7 @@
|
||||
### 1.0.1
|
||||
|
||||
- Add `column_attributes`.
|
||||
- Remove `can_create` and add `get_toolbar_actions`.
|
||||
|
||||
### 1.0.0
|
||||
|
||||
|
@ -9,7 +9,7 @@ from examples.models import Admin, Category, Config, Product
|
||||
from fastapi_admin.app import app
|
||||
from fastapi_admin.enums import Method
|
||||
from fastapi_admin.file_upload import FileUpload
|
||||
from fastapi_admin.resources import Action, Dropdown, Field, Link, Model
|
||||
from fastapi_admin.resources import Action, Dropdown, Field, Link, Model, ToolbarAction
|
||||
from fastapi_admin.widgets import displays, filters, inputs
|
||||
|
||||
upload = FileUpload(uploads_dir=os.path.join(BASE_DIR, "static", "uploads"))
|
||||
@ -56,7 +56,8 @@ class AdminResource(Model):
|
||||
),
|
||||
"created_at",
|
||||
]
|
||||
can_create = False
|
||||
async def get_toolbar_actions(self, request: Request) -> List[ToolbarAction]:
|
||||
return []
|
||||
|
||||
async def cell_attributes(self, request: Request, obj: dict, field: Field) -> dict:
|
||||
if field.name == "id":
|
||||
@ -92,7 +93,7 @@ class Content(Dropdown):
|
||||
"is_reviewed",
|
||||
"type",
|
||||
Field(name="image", label="Image", display=displays.Image(width="40")),
|
||||
Field(name="body", label='Body', input_=inputs.Editor()),
|
||||
Field(name="body", label="Body", input_=inputs.Editor()),
|
||||
"created_at",
|
||||
]
|
||||
|
||||
|
@ -25,6 +25,8 @@ async def get_model_resource(request: Request, model=Depends(get_model)):
|
||||
raise HTTPException(status_code=HTTP_404_NOT_FOUND)
|
||||
actions = await model_resource.get_actions(request)
|
||||
bulk_actions = await model_resource.get_bulk_actions(request)
|
||||
toolbar_actions = await model_resource.get_toolbar_actions(request)
|
||||
setattr(model_resource, "toolbar_actions", toolbar_actions)
|
||||
setattr(model_resource, "actions", actions)
|
||||
setattr(model_resource, "bulk_actions", bulk_actions)
|
||||
return model_resource
|
||||
|
@ -68,6 +68,10 @@ class Action(BaseModel):
|
||||
raise ValueError("ajax is False only available when method is Method.GET")
|
||||
|
||||
|
||||
class ToolbarAction(Action):
|
||||
class_: Optional[str]
|
||||
|
||||
|
||||
class Model(Resource):
|
||||
model: Type[TortoiseModel]
|
||||
fields: List[Union[str, Field]] = []
|
||||
@ -78,6 +82,18 @@ class Model(Resource):
|
||||
can_create: bool = True
|
||||
enctype = "application/x-www-form-urlencoded"
|
||||
|
||||
async def get_toolbar_actions(self, request: Request) -> List[ToolbarAction]:
|
||||
return [
|
||||
ToolbarAction(
|
||||
label=_("create"),
|
||||
icon="fas fa-plus",
|
||||
name="create",
|
||||
method=Method.GET,
|
||||
ajax=False,
|
||||
class_="btn-dark",
|
||||
)
|
||||
]
|
||||
|
||||
async def row_attributes(self, request: Request, obj: dict) -> dict:
|
||||
return {}
|
||||
|
||||
|
@ -29,14 +29,7 @@
|
||||
{% endfor %}
|
||||
<div class="ms-2">
|
||||
<button type="submit" class="btn btn-primary">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-search"
|
||||
width="24" height="24" viewBox="0 0 24 24" stroke-width="2"
|
||||
stroke="currentColor" fill="none" stroke-linecap="round"
|
||||
stroke-linejoin="round">
|
||||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||||
<circle cx="10" cy="10" r="7"></circle>
|
||||
<line x1="21" y1="21" x2="15" y2="15"></line>
|
||||
</svg>
|
||||
<i class="fas fa-search me-2"></i>
|
||||
{{ _('search') }}
|
||||
</button>
|
||||
</div>
|
||||
@ -72,18 +65,15 @@
|
||||
</span>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% if model_resource.can_create %}
|
||||
<a class="ms-auto btn btn-dark" href="{{ request.app.admin_path }}/{{ resource }}/create">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24"
|
||||
stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
|
||||
stroke-linejoin="round">
|
||||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||||
<line x1="12" y1="5" x2="12" y2="19"></line>
|
||||
<line x1="5" y1="12" x2="19" y2="12"></line>
|
||||
</svg>
|
||||
{{ _('create') }}
|
||||
<div id="toolbar-actions" class="ms-auto btn-list">
|
||||
{% for action in model_resource.toolbar_actions %}
|
||||
<a class="btn {{ action.class_ }}"
|
||||
href="{{ request.app.admin_path }}/{{ resource }}/{{ action.name }}">
|
||||
<i class="{{ action.icon }} me-2"></i>
|
||||
{{ action.label }}
|
||||
</a>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
<div class="table-responsive">
|
||||
<table class="table card-table table-vcenter text-nowrap datatable">
|
||||
|
Reference in New Issue
Block a user