Skip to content

Commit ec3a19b

Browse files
committed
Enable ImageField forms rendering
1 parent 2edcdb5 commit ec3a19b

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

flask_mongoengine/db_fields.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -725,18 +725,15 @@ class ImageField(WtfFieldMixin, fields.ImageField):
725725
All arguments should be passed as keyword arguments, to exclude unexpected behaviour.
726726
"""
727727

728-
def to_wtf_field(
729-
self,
730-
*,
731-
model: Optional[Type] = None,
732-
field_kwargs: Optional[dict] = None,
733-
):
734-
"""
735-
Protection from execution of :func:`to_wtf_field` in form generation.
728+
DEFAULT_WTF_FIELD = custom_fields.MongoImageField if custom_fields else None
736729

737-
:raises NotImplementedError: Field converter to WTForm Field not implemented.
738-
"""
739-
raise NotImplementedError("Field converter to WTForm Field not implemented.")
730+
@property
731+
@wtf_required
732+
def wtf_generated_options(self) -> dict:
733+
"""Inserts accepted type in widget rendering (does not do validation)."""
734+
options = super().wtf_generated_options
735+
options["render_kw"] = {"accept": "image/*"}
736+
return options
740737

741738

742739
class IntField(WtfFieldMixin, fields.IntField):

flask_mongoengine/wtf/fields.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,12 @@ class MongoHiddenField(EmptyStringIsNoneMixin, wtf_fields.HiddenField):
436436
pass
437437

438438

439+
class MongoImageField(MongoFileField):
440+
"""GridFS image field."""
441+
442+
widget = mongo_widgets.MongoImageInput()
443+
444+
439445
class MongoPasswordField(EmptyStringIsNoneMixin, wtf_fields.PasswordField):
440446
"""
441447
Regular :class:`wtforms.fields.PasswordField`, that transform empty string to `None`.

tests/test_db_fields.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ def test__ensure_callable_or_list__raise_error_if_argument_not_callable_and_not_
158158
db_fields.GenericReferenceField,
159159
db_fields.GeoJsonBaseField,
160160
db_fields.GeoPointField,
161-
db_fields.ImageField,
162161
db_fields.LazyReferenceField,
163162
db_fields.LineStringField,
164163
db_fields.ListField,

0 commit comments

Comments
 (0)