diff --git a/docs/en/docs/getting_started/quick_start.md b/docs/en/docs/getting_started/quick_start.md index 16e37e8..4956f36 100644 --- a/docs/en/docs/getting_started/quick_start.md +++ b/docs/en/docs/getting_started/quick_start.md @@ -38,8 +38,7 @@ async def startup(): logo_url="https://preview.tabler.io/static/logo-white.svg", login_logo_url="https://preview.tabler.io/static/logo.svg", template_folders=[os.path.join(BASE_DIR, "templates")], - login_provider=login_provider, - maintenance=False, + providers=[login_provider], redis=redis, ) ``` @@ -119,10 +118,10 @@ class AdminResource(Model): ] can_create = False - def get_actions(self) -> List[Action]: + async def get_actions(self,request:Request) -> List[Action]: return [] - def get_bulk_actions(self) -> List[Action]: + async def get_bulk_actions(self,request:Request) -> List[Action]: return [] ``` diff --git a/examples/resources.py b/examples/resources.py index 30d7d43..8b417dd 100644 --- a/examples/resources.py +++ b/examples/resources.py @@ -1,6 +1,8 @@ import os from typing import List +from starlette.requests import Request + from examples import enums from examples.constants import BASE_DIR from examples.models import Admin, Category, Config, Product @@ -55,10 +57,10 @@ class AdminResource(Model): ] can_create = False - def cell_attributes(self, obj: dict, field: Field) -> dict: + async def cell_attributes(self, request: Request, obj: dict, field: Field) -> dict: if field.name == "id": - return {"class": "bg-danger"} - return super().cell_attributes(obj, field) + return {"class": "bg-danger text-white"} + return await super().cell_attributes(request, obj, field) def get_actions(self) -> List[Action]: return [] @@ -119,10 +121,10 @@ class ConfigResource(Model): ), ] - def row_attributes(self, obj: dict) -> dict: + async def row_attributes(self, request: Request, obj: dict) -> dict: if obj.get("status") == enums.Status.on: return {"class": "bg-green text-white"} - return super().row_attributes(obj) + return await super().row_attributes(obj) @app.register diff --git a/fastapi_admin/resources.py b/fastapi_admin/resources.py index 04de351..1eef6ca 100644 --- a/fastapi_admin/resources.py +++ b/fastapi_admin/resources.py @@ -2,6 +2,7 @@ from typing import List, Optional, Type, Union from pydantic import BaseModel from starlette.datastructures import FormData +from starlette.requests import Request from tortoise import ForeignKeyFieldInstance, ManyToManyFieldInstance from tortoise import Model as TortoiseModel from tortoise.fields import BooleanField, DateField, DatetimeField, JSONField @@ -70,10 +71,10 @@ class Model(Resource): can_create: bool = True enctype = "application/x-www-form-urlencoded" - def row_attributes(self, obj: dict) -> dict: + async def row_attributes(self, request: Request, obj: dict) -> dict: return {} - def cell_attributes(self, obj: dict, field: Field) -> dict: + async def cell_attributes(self, request: Request, obj: dict, field: Field) -> dict: return {} def get_actions(self) -> List[Action]: @@ -135,14 +136,14 @@ class Model(Resource): return ret, m2m_ret @classmethod - async def get_filters(cls, values: Optional[dict] = None): + async def get_filters(cls, request: Request, values: Optional[dict] = None): if not values: values = {} ret = [] for f in cls.filters: name = f.context.get("name") value = values.get(name) - ret.append(await f.render(value)) + ret.append(await f.render(request, value)) return ret @classmethod diff --git a/fastapi_admin/routes/resources.py b/fastapi_admin/routes/resources.py index 7fcb99a..1609fd9 100644 --- a/fastapi_admin/routes/resources.py +++ b/fastapi_admin/routes/resources.py @@ -29,7 +29,7 @@ async def list_view( fields_label = model_resource.get_fields_label() fields = model_resource.get_fields() params = await model_resource.resolve_query_params(dict(request.query_params)) - filters = await model_resource.get_filters(params) + filters = await model_resource.get_filters(request, params) qs = model.filter(**params) total = await qs.count() if page_size: @@ -38,13 +38,17 @@ async def list_view( page_size = model_resource.page_size qs = qs.offset((page_num - 1) * page_size) values = await qs.values(*fields_name) - rendered_values = await render_values(fields, values) + rendered_values, row_attributes, cell_attributes = await render_values( + request, model_resource, fields, values + ) context = { "request": request, "resources": resources, "fields_label": fields_label, "fields": fields, "values": values, + "row_attributes": row_attributes, + "cell_attributes": cell_attributes, "rendered_values": rendered_values, "filters": filters, "resource": resource, diff --git a/fastapi_admin/template.py b/fastapi_admin/template.py index 095f8cf..1ff73df 100644 --- a/fastapi_admin/template.py +++ b/fastapi_admin/template.py @@ -12,7 +12,7 @@ from fastapi_admin import VERSION from fastapi_admin.constants import BASE_DIR if typing.TYPE_CHECKING: - from fastapi_admin.resources import Field + from fastapi_admin.resources import Field, Model templates = Jinja2Templates(directory=os.path.join(BASE_DIR, "templates")) templates.env.globals["VERSION"] = VERSION @@ -46,22 +46,34 @@ def add_template_folder(*folders: str): async def render_values( - fields: List["Field"], values: List[Tuple[Any]], display: bool = True -) -> List[List[Any]]: + request: Request, + model: "Model", + fields: List["Field"], + values: List[typing.Dict[str, Any]], + display: bool = True, +) -> typing.Tuple[List[List[Any]], List[dict], List[List[dict]]]: """ render values with template render :param fields: :param values: :param display: + :params request: + :params model: :return: """ ret = [] + cell_attributes: List[List[dict]] = [] + row_attributes: List[dict] = [] for value in values: + row_attributes.append(await model.row_attributes(request, value)) item = [] + cell_item = [] for i, k in enumerate(value): + cell_item.append(await model.cell_attributes(request, value, fields[i])) if display: - item.append(await fields[i].display.render(value[k])) + item.append(await fields[i].display.render(request, value[k])) else: - item.append(await fields[i].input.render(value[k])) + item.append(await fields[i].input.render(request, value[k])) ret.append(item) - return ret + cell_attributes.append(cell_item) + return ret, row_attributes, cell_attributes diff --git a/fastapi_admin/templates/list.html b/fastapi_admin/templates/list.html index f4f309b..19404f8 100644 --- a/fastapi_admin/templates/list.html +++ b/fastapi_admin/templates/list.html @@ -108,7 +108,7 @@
{% for value in rendered_values %} -