Skip to content

Commit 61de931

Browse files
committed
Copy fields generation basic parameters from orm.py (For existed fields)
1 parent 4f4bc8a commit 61de931

File tree

1 file changed

+65
-6
lines changed

1 file changed

+65
-6
lines changed

flask_mongoengine/db_fields.py

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,19 @@
4141
"URLField",
4242
"UUIDField",
4343
]
44+
import decimal
4445
from typing import Callable, List, Optional, Union
4546

47+
from bson import ObjectId
4648
from mongoengine import fields
4749

4850
try:
4951
from wtforms import fields as wtf_fields
5052
from wtforms import validators as wtf_validators
53+
54+
from flask_mongoengine.wtf import fields as custom_fields
5155
except ImportError: # pragma: no cover
56+
custom_fields = None
5257
wtf_fields = None
5358
wtf_validators = None
5459

@@ -57,22 +62,34 @@ class WtfFieldMixin:
5762
"""
5863
Extension wrapper class for mongoengine BaseField.
5964
60-
This enables flask-mongoengine wtf to extend the
61-
number of field parameters, and settings on behalf
62-
of document model form generator for WTForm.
65+
This enables flask-mongoengine wtf to extend the number of field parameters, and
66+
settings on behalf of document model form generator for WTForm.
67+
68+
**Class variables:**
6369
64-
:param validators: wtf model form field validators.
65-
:param filters: wtf model form field filters.
66-
:param kwargs: keyword arguments silently bypassed to normal mongoengine fields
70+
:cvar DEFAULT_WTF_CHOICES_FIELD: Default WTForms Field used for db fields when
71+
**choices** option specified.
72+
:cvar DEFAULT_WTF_FIELD: Default WTForms Field used for db field.
6773
"""
6874

75+
DEFAULT_WTF_FIELD = None
76+
DEFAULT_WTF_CHOICES_FIELD = wtf_fields.SelectField if wtf_fields else None
77+
DEFAULT_WTF_CHOICES_FIELD_COERCE = str
78+
6979
def __init__(
7080
self,
7181
*,
7282
validators: Optional[Union[List, Callable]] = None,
7383
filters: Optional[Union[List, Callable]] = None,
7484
**kwargs,
7585
):
86+
"""
87+
Extended :func:`__init__` method for mongoengine db field with WTForms options.
88+
89+
:param validators: wtf model form field validators.
90+
:param filters: wtf model form field filters.
91+
:param kwargs: keyword arguments silently bypassed to normal mongoengine fields
92+
"""
7693
self.validators = self._ensure_callable_or_list(validators, "validators")
7794
self.filters = self._ensure_callable_or_list(filters, "filters")
7895

@@ -118,6 +135,8 @@ class BinaryField(WtfFieldMixin, fields.BinaryField):
118135
All arguments should be passed as keyword arguments, to exclude unexpected behaviour.
119136
"""
120137

138+
DEFAULT_WTF_FIELD = custom_fields.BinaryField if custom_fields else None
139+
121140
def to_wtf_field(self, model, field_kwargs):
122141
"""
123142
Protection from execution of :func:`to_wtf_field` in form generation.
@@ -135,6 +154,9 @@ class BooleanField(WtfFieldMixin, fields.BooleanField):
135154
All arguments should be passed as keyword arguments, to exclude unexpected behaviour.
136155
"""
137156

157+
DEFAULT_WTF_FIELD = wtf_fields.BooleanField if wtf_fields else None
158+
DEFAULT_WTF_CHOICES_FIELD_COERCE = bool
159+
138160
def to_wtf_field(self, model, field_kwargs):
139161
"""
140162
Protection from execution of :func:`to_wtf_field` in form generation.
@@ -186,6 +208,8 @@ class DateField(WtfFieldMixin, fields.DateField):
186208
All arguments should be passed as keyword arguments, to exclude unexpected behaviour.
187209
"""
188210

211+
DEFAULT_WTF_FIELD = wtf_fields.DateField if wtf_fields else None
212+
189213
def to_wtf_field(self, model, field_kwargs):
190214
"""
191215
Protection from execution of :func:`to_wtf_field` in form generation.
@@ -203,6 +227,8 @@ class DateTimeField(WtfFieldMixin, fields.DateTimeField):
203227
All arguments should be passed as keyword arguments, to exclude unexpected behaviour.
204228
"""
205229

230+
DEFAULT_WTF_FIELD = wtf_fields.DateTimeField if wtf_fields else None
231+
206232
def to_wtf_field(self, model, field_kwargs):
207233
"""
208234
Protection from execution of :func:`to_wtf_field` in form generation.
@@ -220,6 +246,9 @@ class DecimalField(WtfFieldMixin, fields.DecimalField):
220246
All arguments should be passed as keyword arguments, to exclude unexpected behaviour.
221247
"""
222248

249+
DEFAULT_WTF_FIELD = wtf_fields.DecimalField if wtf_fields else None
250+
DEFAULT_WTF_CHOICES_FIELD_COERCE = decimal.Decimal
251+
223252
def to_wtf_field(self, model, field_kwargs):
224253
"""
225254
Protection from execution of :func:`to_wtf_field` in form generation.
@@ -237,6 +266,8 @@ class DictField(WtfFieldMixin, fields.DictField):
237266
All arguments should be passed as keyword arguments, to exclude unexpected behaviour.
238267
"""
239268

269+
DEFAULT_WTF_FIELD = custom_fields.DictField if custom_fields else None
270+
240271
def to_wtf_field(self, model, field_kwargs):
241272
"""
242273
Protection from execution of :func:`to_wtf_field` in form generation.
@@ -269,8 +300,14 @@ class EmailField(WtfFieldMixin, fields.EmailField):
269300
270301
For full list of arguments and keyword arguments, look parent field docs.
271302
All arguments should be passed as keyword arguments, to exclude unexpected behaviour.
303+
304+
.. versionchanged:: 2.0.0
305+
Default field output changed from :class:`.NoneStringField` to
306+
:class:`wtforms.fields.EmailField`
272307
"""
273308

309+
DEFAULT_WTF_FIELD = wtf_fields.EmailField if wtf_fields else None
310+
274311
def to_wtf_field(self, model, field_kwargs):
275312
"""
276313
Protection from execution of :func:`to_wtf_field` in form generation.
@@ -288,6 +325,8 @@ class EmbeddedDocumentField(WtfFieldMixin, fields.EmbeddedDocumentField):
288325
All arguments should be passed as keyword arguments, to exclude unexpected behaviour.
289326
"""
290327

328+
DEFAULT_WTF_FIELD = wtf_fields.FormField if wtf_fields else None
329+
291330
def to_wtf_field(self, model, field_kwargs):
292331
"""
293332
Protection from execution of :func:`to_wtf_field` in form generation.
@@ -339,6 +378,8 @@ class FileField(WtfFieldMixin, fields.FileField):
339378
All arguments should be passed as keyword arguments, to exclude unexpected behaviour.
340379
"""
341380

381+
DEFAULT_WTF_FIELD = wtf_fields.FileField if wtf_fields else None
382+
342383
def to_wtf_field(self, model, field_kwargs):
343384
"""
344385
Protection from execution of :func:`to_wtf_field` in form generation.
@@ -356,6 +397,9 @@ class FloatField(WtfFieldMixin, fields.FloatField):
356397
All arguments should be passed as keyword arguments, to exclude unexpected behaviour.
357398
"""
358399

400+
DEFAULT_WTF_FIELD = wtf_fields.FloatField if wtf_fields else None
401+
DEFAULT_WTF_CHOICES_FIELD_COERCE = float
402+
359403
def to_wtf_field(self, model, field_kwargs):
360404
"""
361405
Protection from execution of :func:`to_wtf_field` in form generation.
@@ -475,6 +519,9 @@ class IntField(WtfFieldMixin, fields.IntField):
475519
All arguments should be passed as keyword arguments, to exclude unexpected behaviour.
476520
"""
477521

522+
DEFAULT_WTF_FIELD = wtf_fields.IntegerField if wtf_fields else None
523+
DEFAULT_WTF_CHOICES_FIELD_COERCE = int
524+
478525
def to_wtf_field(self, model, field_kwargs):
479526
"""
480527
Protection from execution of :func:`to_wtf_field` in form generation.
@@ -526,6 +573,8 @@ class ListField(WtfFieldMixin, fields.ListField):
526573
All arguments should be passed as keyword arguments, to exclude unexpected behaviour.
527574
"""
528575

576+
DEFAULT_WTF_FIELD = wtf_fields.FieldList if wtf_fields else None
577+
529578
def to_wtf_field(self, model, field_kwargs):
530579
"""
531580
Protection from execution of :func:`to_wtf_field` in form generation.
@@ -628,6 +677,8 @@ class ObjectIdField(WtfFieldMixin, fields.ObjectIdField):
628677
All arguments should be passed as keyword arguments, to exclude unexpected behaviour.
629678
"""
630679

680+
DEFAULT_WTF_CHOICES_FIELD_COERCE = ObjectId
681+
631682
def to_wtf_field(self, model, field_kwargs):
632683
"""
633684
Protection from execution of :func:`to_wtf_field` in form generation.
@@ -679,6 +730,8 @@ class ReferenceField(WtfFieldMixin, fields.ReferenceField):
679730
All arguments should be passed as keyword arguments, to exclude unexpected behaviour.
680731
"""
681732

733+
DEFAULT_WTF_FIELD = custom_fields.ModelSelectField if custom_fields else None
734+
682735
def to_wtf_field(self, model, field_kwargs):
683736
"""
684737
Protection from execution of :func:`to_wtf_field` in form generation.
@@ -713,6 +766,8 @@ class SortedListField(WtfFieldMixin, fields.SortedListField):
713766
All arguments should be passed as keyword arguments, to exclude unexpected behaviour.
714767
"""
715768

769+
DEFAULT_WTF_FIELD = wtf_fields.FieldList if wtf_fields else None
770+
716771
def to_wtf_field(self, model, field_kwargs):
717772
"""
718773
Protection from execution of :func:`to_wtf_field` in form generation.
@@ -730,6 +785,8 @@ class StringField(WtfFieldMixin, fields.StringField):
730785
All arguments should be passed as keyword arguments, to exclude unexpected behaviour.
731786
"""
732787

788+
DEFAULT_WTF_FIELD = wtf_fields.TextAreaField if wtf_fields else None
789+
733790
def to_wtf_field(self, model, field_kwargs):
734791
"""
735792
Protection from execution of :func:`to_wtf_field` in form generation.
@@ -747,6 +804,8 @@ class URLField(WtfFieldMixin, fields.URLField):
747804
All arguments should be passed as keyword arguments, to exclude unexpected behaviour.
748805
"""
749806

807+
DEFAULT_WTF_FIELD = custom_fields.NoneStringField if custom_fields else None
808+
750809
def to_wtf_field(self, model, field_kwargs):
751810
"""
752811
Protection from execution of :func:`to_wtf_field` in form generation.

0 commit comments

Comments
 (0)