Remove can_create and add get_toolbar_actions.

This commit is contained in:
long2ice
2021-05-19 15:30:18 +08:00
parent 3ee095fa0d
commit a3c01fc42c
6 changed files with 82 additions and 72 deletions

View File

@ -5,6 +5,7 @@
### 1.0.1 ### 1.0.1
- Add `column_attributes`. - Add `column_attributes`.
- Remove `can_create` and add `get_toolbar_actions`.
### 1.0.0 ### 1.0.0

View File

@ -9,7 +9,7 @@ from examples.models import Admin, Category, Config, Product
from fastapi_admin.app import app from fastapi_admin.app import app
from fastapi_admin.enums import Method from fastapi_admin.enums import Method
from fastapi_admin.file_upload import FileUpload 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 from fastapi_admin.widgets import displays, filters, inputs
upload = FileUpload(uploads_dir=os.path.join(BASE_DIR, "static", "uploads")) upload = FileUpload(uploads_dir=os.path.join(BASE_DIR, "static", "uploads"))
@ -56,7 +56,8 @@ class AdminResource(Model):
), ),
"created_at", "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: async def cell_attributes(self, request: Request, obj: dict, field: Field) -> dict:
if field.name == "id": if field.name == "id":
@ -92,7 +93,7 @@ class Content(Dropdown):
"is_reviewed", "is_reviewed",
"type", "type",
Field(name="image", label="Image", display=displays.Image(width="40")), 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", "created_at",
] ]

View File

@ -25,6 +25,8 @@ async def get_model_resource(request: Request, model=Depends(get_model)):
raise HTTPException(status_code=HTTP_404_NOT_FOUND) raise HTTPException(status_code=HTTP_404_NOT_FOUND)
actions = await model_resource.get_actions(request) actions = await model_resource.get_actions(request)
bulk_actions = await model_resource.get_bulk_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, "actions", actions)
setattr(model_resource, "bulk_actions", bulk_actions) setattr(model_resource, "bulk_actions", bulk_actions)
return model_resource return model_resource

View File

@ -68,6 +68,10 @@ class Action(BaseModel):
raise ValueError("ajax is False only available when method is Method.GET") raise ValueError("ajax is False only available when method is Method.GET")
class ToolbarAction(Action):
class_: Optional[str]
class Model(Resource): class Model(Resource):
model: Type[TortoiseModel] model: Type[TortoiseModel]
fields: List[Union[str, Field]] = [] fields: List[Union[str, Field]] = []
@ -78,6 +82,18 @@ class Model(Resource):
can_create: bool = True can_create: bool = True
enctype = "application/x-www-form-urlencoded" 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: async def row_attributes(self, request: Request, obj: dict) -> dict:
return {} return {}

View File

@ -29,14 +29,7 @@
{% endfor %} {% endfor %}
<div class="ms-2"> <div class="ms-2">
<button type="submit" class="btn btn-primary"> <button type="submit" class="btn btn-primary">
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-search" <i class="fas fa-search me-2"></i>
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>
{{ _('search') }} {{ _('search') }}
</button> </button>
</div> </div>
@ -72,18 +65,15 @@
</span> </span>
{% endif %} {% endif %}
</div> </div>
{% if model_resource.can_create %} <div id="toolbar-actions" class="ms-auto btn-list">
<a class="ms-auto btn btn-dark" href="{{ request.app.admin_path }}/{{ resource }}/create"> {% for action in model_resource.toolbar_actions %}
<svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" <a class="btn {{ action.class_ }}"
stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" href="{{ request.app.admin_path }}/{{ resource }}/{{ action.name }}">
stroke-linejoin="round"> <i class="{{ action.icon }} me-2"></i>
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path> {{ action.label }}
<line x1="12" y1="5" x2="12" y2="19"></line>
<line x1="5" y1="12" x2="19" y2="12"></line>
</svg>
{{ _('create') }}
</a> </a>
{% endif %} {% endfor %}
</div>
</div> </div>
<div class="table-responsive"> <div class="table-responsive">
<table class="table card-table table-vcenter text-nowrap datatable"> <table class="table card-table table-vcenter text-nowrap datatable">