Skip to content

Commit 80f4849

Browse files
committed
Add in forms support of BinaryField
1 parent f17a964 commit 80f4849

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

example_app/binary_demo.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class BinaryDemoModel(db.Document):
88

99
string_field = db.StringField()
1010
binary_field = db.BinaryField()
11+
binary_field_with_default = db.BinaryField(default=lambda: "foobar".encode("utf-8"))
1112

1213

1314
def binary_demo_view(pk=None):

flask_mongoengine/db_fields.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -305,20 +305,7 @@ class BinaryField(WtfFieldMixin, fields.BinaryField):
305305
All arguments should be passed as keyword arguments, to exclude unexpected behaviour.
306306
"""
307307

308-
DEFAULT_WTF_FIELD = custom_fields.BinaryField if custom_fields else None
309-
310-
def to_wtf_field(
311-
self,
312-
*,
313-
model: Optional[Type] = None,
314-
field_kwargs: Optional[dict] = None,
315-
):
316-
"""
317-
Protection from execution of :func:`to_wtf_field` in form generation.
318-
319-
:raises NotImplementedError: Field converter to WTForm Field not implemented.
320-
"""
321-
raise NotImplementedError("Field converter to WTForm Field not implemented.")
308+
DEFAULT_WTF_FIELD = custom_fields.MongoBinaryField if custom_fields else None
322309

323310

324311
class BooleanField(WtfFieldMixin, fields.BooleanField):

flask_mongoengine/wtf/fields.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,26 @@ def process_formdata(self, valuelist):
309309
super().process_formdata(valuelist)
310310

311311

312+
# noinspection PyAttributeOutsideInit
313+
class MongoBinaryField(wtf_fields.TextAreaField):
314+
"""
315+
Special WTForm :class:`~.wtforms.fields.TextAreaField` that convert input to binary.
316+
"""
317+
318+
def process_formdata(self, valuelist):
319+
"""Converts string form value to binary type and ignoring empty form fields."""
320+
if not valuelist or valuelist[0] == "":
321+
self.data = None
322+
else:
323+
self.data = valuelist[0].encode("utf-8")
324+
325+
def _value(self):
326+
"""
327+
Ensures that encoded string data will not be encoded once more on form edit.
328+
"""
329+
return self.data.decode("utf-8") if self.data is not None else ""
330+
331+
312332
class MongoBooleanField(wtf_fields.SelectField):
313333
"""Mongo SelectField field for BooleanFields, that correctly coerce values."""
314334

@@ -325,8 +345,6 @@ def __init__(
325345
Replaces defaults of :class:`wtforms.fields.SelectField` with for Boolean values.
326346
327347
Fully compatible with :class:`wtforms.fields.SelectField` and have same parameters.
328-
329-
330348
"""
331349
if coerce is None:
332350
coerce = coerce_boolean

tests/test_db_fields.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ def test__ensure_callable_or_list__raise_error_if_argument_not_callable_and_not_
148148
@pytest.mark.parametrize(
149149
"FieldClass",
150150
[
151-
db_fields.BinaryField,
152151
db_fields.CachedReferenceField,
153152
db_fields.DynamicField,
154153
db_fields.EmbeddedDocumentField,

0 commit comments

Comments
 (0)