Skip to content

Commit c915fd0

Browse files
committed
Add support of ComplexDateTimeField, DateField, DateTimeField
1 parent 1c9258c commit c915fd0

File tree

2 files changed

+38
-39
lines changed

2 files changed

+38
-39
lines changed

flask_mongoengine/db_fields.py

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -374,20 +374,33 @@ class ComplexDateTimeField(WtfFieldMixin, fields.ComplexDateTimeField):
374374
375375
For full list of arguments and keyword arguments, look parent field docs.
376376
All arguments should be passed as keyword arguments, to exclude unexpected behaviour.
377+
378+
.. important::
379+
During WTForm generation this field uses :class:`wtforms.fields.DateTimeLocalField`
380+
with milliseconds accuracy. Direct microseconds not supported by browsers for
381+
this type of field. If exact microseconds support required, please use
382+
:class:`wtforms.fields.DateTimeField` with extended text format set. Examples
383+
available in example app.
384+
385+
This does not affect on in database accuracy.
377386
"""
378387

379-
def to_wtf_field(
380-
self,
381-
*,
382-
model: Optional[Type] = None,
383-
field_kwargs: Optional[dict] = None,
384-
):
385-
"""
386-
Protection from execution of :func:`to_wtf_field` in form generation.
388+
DEFAULT_WTF_FIELD = wtf_fields.DateTimeLocalField if wtf_fields else None
387389

388-
:raises NotImplementedError: Field converter to WTForm Field not implemented.
389-
"""
390-
raise NotImplementedError("Field converter to WTForm Field not implemented.")
390+
@property
391+
@wtf_required
392+
def wtf_generated_options(self) -> dict:
393+
"""Extend form date time field with milliseconds support."""
394+
options = super().wtf_generated_options
395+
options["format"] = [
396+
"%Y-%m-%d %H:%M:%S",
397+
"%Y-%m-%dT%H:%M:%S",
398+
"%Y-%m-%d %H:%M:%S.%f",
399+
"%Y-%m-%dT%H:%M:%S.%f",
400+
]
401+
options["render_kw"] = {"step": "0.000001"}
402+
403+
return options
391404

392405

393406
class DateField(WtfFieldMixin, fields.DateField):
@@ -400,19 +413,6 @@ class DateField(WtfFieldMixin, fields.DateField):
400413

401414
DEFAULT_WTF_FIELD = wtf_fields.DateField if wtf_fields else None
402415

403-
def to_wtf_field(
404-
self,
405-
*,
406-
model: Optional[Type] = None,
407-
field_kwargs: Optional[dict] = None,
408-
):
409-
"""
410-
Protection from execution of :func:`to_wtf_field` in form generation.
411-
412-
:raises NotImplementedError: Field converter to WTForm Field not implemented.
413-
"""
414-
raise NotImplementedError("Field converter to WTForm Field not implemented.")
415-
416416

417417
class DateTimeField(WtfFieldMixin, fields.DateTimeField):
418418
"""
@@ -422,20 +422,22 @@ class DateTimeField(WtfFieldMixin, fields.DateTimeField):
422422
All arguments should be passed as keyword arguments, to exclude unexpected behaviour.
423423
"""
424424

425-
DEFAULT_WTF_FIELD = wtf_fields.DateTimeField if wtf_fields else None
425+
DEFAULT_WTF_FIELD = wtf_fields.DateTimeLocalField if wtf_fields else None
426426

427-
def to_wtf_field(
428-
self,
429-
*,
430-
model: Optional[Type] = None,
431-
field_kwargs: Optional[dict] = None,
432-
):
433-
"""
434-
Protection from execution of :func:`to_wtf_field` in form generation.
427+
@property
428+
@wtf_required
429+
def wtf_generated_options(self) -> dict:
430+
"""Extend form date time field with milliseconds support."""
431+
options = super().wtf_generated_options
432+
options["format"] = [
433+
"%Y-%m-%d %H:%M:%S",
434+
"%Y-%m-%dT%H:%M:%S",
435+
"%Y-%m-%d %H:%M:%S.%f",
436+
"%Y-%m-%dT%H:%M:%S.%f",
437+
]
438+
options["render_kw"] = {"step": "1"}
435439

436-
:raises NotImplementedError: Field converter to WTForm Field not implemented.
437-
"""
438-
raise NotImplementedError("Field converter to WTForm Field not implemented.")
440+
return options
439441

440442

441443
class DecimalField(WtfFieldMixin, fields.DecimalField):

tests/test_db_fields.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,6 @@ def test__ensure_callable_or_list__raise_error_if_argument_not_callable_and_not_
151151
db_fields.BinaryField,
152152
db_fields.BooleanField,
153153
db_fields.CachedReferenceField,
154-
db_fields.ComplexDateTimeField,
155-
db_fields.DateField,
156-
db_fields.DateTimeField,
157154
db_fields.DictField,
158155
db_fields.DynamicField,
159156
db_fields.EmbeddedDocumentField,

0 commit comments

Comments
 (0)