-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to use Jinja2 templates in action forms #505
base: main
Are you sure you want to change the base?
Conversation
This also partially solves the problem described in #375. |
You're implementation looks quite interesting and it kinda makes #375 obsolete. Let's hear what @jowilf is thinking about it. Edit: At #444, we talked about that validation issue. I recommend checking it. It would be awesome if you could integrate validation feature to the Great work 💯 |
starlette_admin/actions.py
Outdated
@@ -11,6 +11,8 @@ def action( | |||
submit_btn_text: Optional[str] = _("Yes, Proceed"), | |||
icon_class: Optional[str] = None, | |||
form: Optional[str] = None, | |||
form_template: Optional[str] = "forms/action_form.html", | |||
form_context: Optional[dict] = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
form_context: Optional[dict] = None, | |
form_context: Optional[Dict] = None, |
To support older Python versions, you should import Dict
from typing or typing_extensions and use it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
I believe that client validation is not enough, and furthermore, it cannot be trusted. I can suggest a temporary workaround to validate forms, which you may see in examples: async def add_tag(self, request: Request, pks: List[Any]):
form_fields = [HasOne("tag", identity="tag")]
try:
form = (
await request.form()
) # from starlette_admin.base.BaseAdmin._render_edit
data = {
f.name: f.parse_form_data(request, form, request.state.action)
for f in form_fields
} # from starlette_admin.base.BaseAdmin.form_to_dict
except Exception as e:
raise ActionFailed(f"Form processing error: {e}") from e
data = await self.arrange_data_for_fields(request, data, form_fields)
await self.validate_add_tag(request, data) Of course, I would like to be able to hide all this boilerplate code inside I would like to be able to validate the form in the same way as in, for example, UPD. Still needs testing. Perhaps it should be a separate PR, thoughts? |
53d3b3f
to
c91c07c
Compare
c91c07c
to
2d990d5
Compare
Taking a fresh look, I realized that there wasn't much refactoring. Added form parsing in BaseView. Now, if the action has if you want to add extra validation, you can write a function: async def validate_my_action(self, request: Request, data: Dict[str, Any]):
...
if len(errors) > 0:
raise FormValidationError(errors)
@action(...)
async def my_action(self, request: Request, pks: List[Any], data: Dict):
await self.validate_my_action(request, data) So, this solves the problem stated at #444 Also, I realized that module-level I left fixups to simplify your review. Will squash them later after you will check the changes. |
2d990d5
to
f3d756c
Compare
Well, that separation bothers me a lot...
But the login is in the backend instead of frontend right?
I totally agree with you, if form_context is going to take one keyword (form_fields), why not just use that keyword?
I'll take a look at it as soon as possible, great work! |
I don't see anything about login at #444. It's a typo? However, validation is completely in the backend, and it is completely similar to how it is done in
Thank you, looking forward to your review! |
Yep, it's a typo. I was walking about validation logic. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, you've done a great job 👏.
I cloned your fork and tried the example. It seems to me that everything is working as it supposed to be but I don't think my approval will (and should not) be enough to be merged. Let's hear what @jowilf has to say about this, as for me, it's far better implementation than #375.
I'll definetely use the changes you made. 🚀.
There is one more thing. What do you @geoolekom think about #463 and #472? Do you think we can find a more Pythonic/python-first (instead of overriding Jinja2 templates) solution for this? |
f3d756c
to
9d2a2bf
Compare
@jowilf @hasansezertasan Any updates on this? |
9d2a2bf
to
3a637f5
Compare
Hey there, sorry for late response. I don't know what the author thinks about it but it looks good enough for me. |
This PR allows the use of Jinja2 templates in action forms, making it more flexible than plain HTML.
select2
controls the same way it is done in editing/creation forms, without duplicating logic and without re-creating controls.