From a3c01fc42c40e8b454dc0d228519633e3b06366c Mon Sep 17 00:00:00 2001 From: long2ice Date: Wed, 19 May 2021 15:30:18 +0800 Subject: [PATCH] Remove `can_create` and add `get_toolbar_actions`. --- CHANGELOG.md | 1 + examples/resources.py | 7 +-- fastapi_admin/depends.py | 2 + fastapi_admin/resources.py | 16 ++++++ fastapi_admin/templates/list.html | 40 ++++++-------- fastapi_admin/widgets/inputs.py | 88 +++++++++++++++---------------- 6 files changed, 82 insertions(+), 72 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f7d983..0342861 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### 1.0.1 - Add `column_attributes`. +- Remove `can_create` and add `get_toolbar_actions`. ### 1.0.0 diff --git a/examples/resources.py b/examples/resources.py index ebf0346..8a213b3 100644 --- a/examples/resources.py +++ b/examples/resources.py @@ -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", ] diff --git a/fastapi_admin/depends.py b/fastapi_admin/depends.py index 393c998..f6caa3f 100644 --- a/fastapi_admin/depends.py +++ b/fastapi_admin/depends.py @@ -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 diff --git a/fastapi_admin/resources.py b/fastapi_admin/resources.py index 9cce6d7..e4ad0b8 100644 --- a/fastapi_admin/resources.py +++ b/fastapi_admin/resources.py @@ -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 {} diff --git a/fastapi_admin/templates/list.html b/fastapi_admin/templates/list.html index 19404f8..485ebc2 100644 --- a/fastapi_admin/templates/list.html +++ b/fastapi_admin/templates/list.html @@ -29,14 +29,7 @@ {% endfor %}
@@ -72,18 +65,15 @@ {% endif %} - {% if model_resource.can_create %} - - - - - - - {{ _('create') }} - - {% endif %} +
+ {% for action in model_resource.toolbar_actions %} + + + {{ action.label }} + + {% endfor %} +
@@ -121,11 +111,11 @@ {% with outer_index = loop.index0 %} {% for x in value %} + {{ k }}="{{ v }}"{% endfor %}>{{ x|safe }} {% endfor %} {% endwith %} - {% if model_resource.actions %} - - {% endif %} + + {% endif %} {% endfor %} diff --git a/fastapi_admin/widgets/inputs.py b/fastapi_admin/widgets/inputs.py index 54f9949..06828d3 100644 --- a/fastapi_admin/widgets/inputs.py +++ b/fastapi_admin/widgets/inputs.py @@ -15,7 +15,7 @@ class Input(Widget): template = "widgets/inputs/input.html" def __init__( - self, help_text: Optional[str] = None, default: Any = None, null: bool = False, **context + self, help_text: Optional[str] = None, default: Any = None, null: bool = False, **context ): super().__init__(null=null, help_text=help_text, **context) self.default = default @@ -44,12 +44,12 @@ class Text(Input): input_type: Optional[str] = "text" def __init__( - self, - help_text: Optional[str] = None, - default: Any = None, - null: bool = False, - placeholder: str = "", - disabled: bool = False, + self, + help_text: Optional[str] = None, + default: Any = None, + null: bool = False, + placeholder: str = "", + disabled: bool = False, ): super().__init__( null=null, @@ -65,11 +65,11 @@ class Select(Input): template = "widgets/inputs/select.html" def __init__( - self, - help_text: Optional[str] = None, - default: Any = None, - null: bool = False, - disabled: bool = False, + self, + help_text: Optional[str] = None, + default: Any = None, + null: bool = False, + disabled: bool = False, ): super().__init__(help_text=help_text, null=null, default=default, disabled=disabled) @@ -91,12 +91,12 @@ class Select(Input): class ForeignKey(Select): def __init__( - self, - model: Type[Model], - default: Any = None, - null: bool = False, - disabled: bool = False, - help_text: Optional[str] = None, + self, + model: Type[Model], + default: Any = None, + null: bool = False, + disabled: bool = False, + help_text: Optional[str] = None, ): super().__init__(help_text=help_text, default=default, null=null, disabled=disabled) self.model = model @@ -116,10 +116,10 @@ class ManyToMany(Select): template = "widgets/inputs/many_to_many.html" def __init__( - self, - model: Type[Model], - disabled: bool = False, - help_text: Optional[str] = None, + self, + model: Type[Model], + disabled: bool = False, + help_text: Optional[str] = None, ): super().__init__(help_text=help_text, disabled=disabled) self.model = model @@ -144,13 +144,13 @@ class ManyToMany(Select): class Enum(Select): def __init__( - self, - enum: Type[EnumCLS], - default: Any = None, - enum_type: Type = int, - null: bool = False, - disabled: bool = False, - help_text: Optional[str] = None, + self, + enum: Type[EnumCLS], + default: Any = None, + enum_type: Type = int, + null: bool = False, + disabled: bool = False, + help_text: Optional[str] = None, ): super().__init__(help_text=help_text, default=default, null=null, disabled=disabled) self.enum = enum @@ -174,10 +174,10 @@ class Json(Input): template = "widgets/inputs/json.html" def __init__( - self, - help_text: Optional[str] = None, - null: bool = False, - options: Optional[dict] = None, + self, + help_text: Optional[str] = None, + null: bool = False, + options: Optional[dict] = None, ): """ options config to jsoneditor, see https://github.com/josdejong/jsoneditor @@ -215,12 +215,12 @@ class File(Input): input_type = "file" def __init__( - self, - upload: FileUpload, - default: Any = None, - null: bool = False, - disabled: bool = False, - help_text: Optional[str] = None, + self, + upload: FileUpload, + default: Any = None, + null: bool = False, + disabled: bool = False, + help_text: Optional[str] = None, ): super().__init__( null=null, @@ -246,11 +246,11 @@ class Radio(Select): template = "widgets/inputs/radio.html" def __init__( - self, - options: List[Tuple[str, Any]], - help_text: Optional[str] = None, - default: Any = None, - disabled: bool = False, + self, + options: List[Tuple[str, Any]], + help_text: Optional[str] = None, + default: Any = None, + disabled: bool = False, ): super().__init__(default=default, disabled=disabled, help_text=help_text) self.options = options
{{ x|safe }} + {% if model_resource.actions %} +