From 3afc4bc3fa9fbeef3259291bb43775056e988389 Mon Sep 17 00:00:00 2001 From: Amin Alaee Date: Tue, 15 Oct 2024 10:45:49 +0000 Subject: [PATCH] Update test for form_columns order (#833) --- sqladmin/application.py | 6 ++---- sqladmin/models.py | 10 ++++++++-- tests/test_models.py | 4 ++-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/sqladmin/application.py b/sqladmin/application.py index 5027f79d..9cbe0fbd 100644 --- a/sqladmin/application.py +++ b/sqladmin/application.py @@ -511,8 +511,7 @@ async def create(self, request: Request) -> Response: identity = request.path_params["identity"] model_view = self._find_model_view(identity) - Form = await model_view.scaffold_form() - model_view._validate_form_class(model_view._form_create_rules, Form) + Form = await model_view.scaffold_form(model_view._form_create_rules) form_data = await self._handle_form_data(request) form = Form(form_data) @@ -562,8 +561,7 @@ async def edit(self, request: Request) -> Response: if not model: raise HTTPException(status_code=404) - Form = await model_view.scaffold_form() - model_view._validate_form_class(model_view._form_edit_rules, Form) + Form = await model_view.scaffold_form(model_view._form_edit_rules) context = { "obj": model, "model_view": model_view, diff --git a/sqladmin/models.py b/sqladmin/models.py index d201be3f..3b1f6d6e 100644 --- a/sqladmin/models.py +++ b/sqladmin/models.py @@ -1015,10 +1015,11 @@ async def after_model_delete(self, model: Any, request: Request) -> None: By default do nothing. """ - async def scaffold_form(self) -> Type[Form]: + async def scaffold_form(self, rules: List[str] | None = None) -> Type[Form]: if self.form is not None: return self.form - return await get_model_form( + + form = await get_model_form( model=self.model, session_maker=self.session_maker, only=self._form_prop_names, @@ -1032,6 +1033,11 @@ async def scaffold_form(self) -> Type[Form]: form_converter=self.form_converter, ) + if rules: + self._validate_form_class(rules, form) + + return form + def search_placeholder(self) -> str: """Return search placeholder text. diff --git a/tests/test_models.py b/tests/test_models.py index 71a9f98f..058ff0bd 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -236,9 +236,9 @@ class UserAdmin(ModelView, model=User): def test_form_columns_by_model_columns() -> None: class UserAdmin(ModelView, model=User): - form_columns = [User.id, User.name] + form_columns = [User.id, User.profile, User.name, User.addresses] - assert UserAdmin().get_form_columns() == ["id", "name"] + assert UserAdmin().get_form_columns() == ["id", "profile", "name", "addresses"] def test_form_columns_by_str_name() -> None: