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
- Add `column_attributes`.
- Remove `can_create` and add `get_toolbar_actions`.
### 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.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",
]

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

View File

@ -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 {}

View File

@ -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') }}
</a>
{% endif %}
<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>
{% endfor %}
</div>
</div>
<div class="table-responsive">
<table class="table card-table table-vcenter text-nowrap datatable">
@ -121,11 +111,11 @@
{% with outer_index = loop.index0 %}
{% for x in value %}
<td {% for k,v in cell_attributes[outer_index][loop.index0].items() %}
{{ k }}="{{ v }}"{% endfor %}>{{ x|safe }}</td>
{{ k }}="{{ v }}"{% endfor %}>{{ x|safe }}</td>
{% endfor %}
{% endwith %}
{% if model_resource.actions %}
<td class="text-end">
{% if model_resource.actions %}
<td class="text-end">
<span class="dropdown">
<button
class="btn dropdown-toggle align-text-top"
@ -154,8 +144,8 @@
{% endfor %}
</div>
</span>
</td>
{% endif %}
</td>
{% endif %}
</tr>
{% endfor %}
</tbody>

View File

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