Skip to content

Commit

Permalink
Update test for form_columns order (#833)
Browse files Browse the repository at this point in the history
  • Loading branch information
aminalaee authored Oct 15, 2024
1 parent d99817a commit 3afc4bc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
6 changes: 2 additions & 4 deletions sqladmin/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down
10 changes: 8 additions & 2 deletions sqladmin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 3afc4bc

Please sign in to comment.