-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
add default coerce to any Select2Field
#2784
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,8 @@ | |
| from sqlalchemy import Boolean | ||
| from sqlalchemy import Column | ||
| from sqlalchemy.orm import ColumnProperty | ||
| from sqlalchemy.sql.type_api import TypeEngine | ||
| from sqlalchemy_utils import ChoiceType | ||
| from wtforms import Field | ||
| from wtforms import fields | ||
| from wtforms import Form | ||
|
|
@@ -36,6 +38,7 @@ | |
| from ..._types import T_FIELD_ARGS_VALIDATORS | ||
| from ..._types import T_FIELD_ARGS_VALIDATORS_ALLOW_BLANK | ||
| from ..._types import T_FIELD_ARGS_VALIDATORS_FILES | ||
| from ..._types import T_FIELD_ARGS_VALIDATORS_SELECTABLE | ||
| from ..._types import T_INSTRUMENTED_ATTRIBUTE | ||
| from ..._types import T_MODEL_VIEW | ||
| from ..._types import T_ORM_MODEL | ||
|
|
@@ -228,7 +231,7 @@ def convert( | |
| if isinstance(prop, FieldPlaceholder): | ||
| return form.recreate_field(prop.field) | ||
|
|
||
| kwargs: T_FIELD_ARGS_VALIDATORS_ALLOW_BLANK = {"validators": [], "filters": []} | ||
| kwargs: T_FIELD_ARGS_VALIDATORS_SELECTABLE = {"validators": [], "filters": []} | ||
|
|
||
| if field_args: | ||
| kwargs.update(field_args) # type: ignore[typeddict-item] | ||
|
|
@@ -356,7 +359,11 @@ def convert( | |
| form_choices = getattr(self.view, "form_choices", None) | ||
| if mapper.class_ == self.view.model and form_choices: | ||
| choices = form_choices.get(prop.key) | ||
|
|
||
| if choices: | ||
| if "coerce" not in kwargs: | ||
| kwargs["coerce"] = coerce_factory(column.type) | ||
|
|
||
| return form.Select2Field( # type: ignore[misc] | ||
| choices=choices, | ||
| allow_blank=column.nullable, # type: ignore[arg-type] | ||
|
|
@@ -648,6 +655,18 @@ def avoid_empty_strings(value: t.Any) -> t.Any: | |
| return value if value else None | ||
|
|
||
|
|
||
| def coerce_factory(type_: TypeEngine[t.Any]) -> t.Callable[[t.Any], t.Any]: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this PR only make it work for sqlalchemy? Or other libraries do not have this issue?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is tested only in Sqlalchemy, however, i've tried to add a Peewee test but it seems like there is no way to use coerce function in Peewee or Pymongo |
||
| """ | ||
| Return a function to coerce a column, for use by Select2Field. | ||
| :param type_: Column type | ||
| """ | ||
|
|
||
| if isinstance(type_, ChoiceType): | ||
| return choice_type_coerce_factory(type_) | ||
| else: | ||
| return type_.python_type | ||
|
|
||
|
|
||
| def choice_type_coerce_factory(type_: T_CHOICE_TYPE) -> t.Callable[[t.Any], t.Any]: | ||
| """ | ||
| Return a function to coerce a ChoiceType column, for use by Select2Field. | ||
|
|
@@ -692,7 +711,10 @@ def get_form( | |
| base_class: type[form.BaseForm] = form.BaseForm, | ||
| only: t.Collection[str | T_INSTRUMENTED_ATTRIBUTE] | None = None, | ||
| exclude: t.Collection[str | T_INSTRUMENTED_ATTRIBUTE] | None = None, | ||
| field_args: dict[str, T_FIELD_ARGS_VALIDATORS_FILES] | None = None, | ||
| field_args: dict[ | ||
| str, T_FIELD_ARGS_VALIDATORS_FILES | T_FIELD_ARGS_VALIDATORS_SELECTABLE | ||
| ] | ||
| | None = None, | ||
| hidden_pk: bool = False, | ||
| ignore_hidden: bool = True, | ||
| extra_fields: dict[str | T_INSTRUMENTED_ATTRIBUTE, Field] | None = 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.
I think we can simply add coerce to
T_FIELD_ARGS_VALIDATORSand removeT_FIELD_ARGS_VALIDATORS_SELECTABLEThere 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.
I've tried your suggestion but unfortunately it all fields that don't include coerce attr. will be affected and raise typing issues. I believe T_FIELD_ARGS_VALIDATORS_SELECTABLE is more clear and dedicated to SELECT fields