From bf8397ba23c0124cf9ecf09c20e390be536c4f2c Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 11 Dec 2024 22:38:51 -0600 Subject: [PATCH] fix: add support for date, datetime form fields using buefy-based picker widgets etc. --- docs/conf.py | 1 + src/wuttaweb/forms/schema.py | 29 ++++ src/wuttaweb/templates/deform/dateinput.pt | 6 + .../templates/deform/datetimeinput.pt | 10 ++ src/wuttaweb/templates/wutta-components.mako | 137 ++++++++++++++++++ tests/forms/test_schema.py | 18 +++ 6 files changed, 201 insertions(+) create mode 100644 src/wuttaweb/templates/deform/dateinput.pt create mode 100644 src/wuttaweb/templates/deform/datetimeinput.pt diff --git a/docs/conf.py b/docs/conf.py index cf15be8..8bf7c8e 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -33,6 +33,7 @@ intersphinx_mapping = { 'pyramid': ('https://docs.pylonsproject.org/projects/pyramid/en/latest/', None), 'python': ('https://docs.python.org/3/', None), 'rattail-manual': ('https://rattailproject.org/docs/rattail-manual/', None), + 'sqlalchemy': ('http://docs.sqlalchemy.org/en/latest/', None), 'webhelpers2': ('https://webhelpers2.readthedocs.io/en/latest/', None), 'wuttjamaican': ('https://rattailproject.org/docs/wuttjamaican/', None), 'wutta-continuum': ('https://rattailproject.org/docs/wutta-continuum/', None), diff --git a/src/wuttaweb/forms/schema.py b/src/wuttaweb/forms/schema.py index b618187..275d42c 100644 --- a/src/wuttaweb/forms/schema.py +++ b/src/wuttaweb/forms/schema.py @@ -24,15 +24,40 @@ Form schema types """ +import datetime import uuid as _uuid import colander +import sqlalchemy as sa from wuttaweb.db import Session from wuttaweb.forms import widgets from wuttjamaican.db.model import Person +class WuttaDateTime(colander.DateTime): + """ + Custom schema type for ``datetime`` fields. + + This should be used automatically for + :class:`sqlalchemy:sqlalchemy.types.DateTime` columns unless you + register another default. + + This schema type exists for sake of convenience, when working with + the Buefy datepicker + timepicker widgets. + """ + + def deserialize(self, node, cstruct): + """ """ + if not cstruct: + return colander.null + + try: + return datetime.datetime.strptime(cstruct, '%Y-%m-%dT%I:%M %p') + except: + node.raise_invalid("Invalid date and/or time") + + class ObjectNode(colander.SchemaNode): """ Custom schema node class which adds methods for compatibility with @@ -502,3 +527,7 @@ class FileDownload(colander.String): """ """ kwargs.setdefault('url', self.url) return widgets.FileDownloadWidget(self.request, **kwargs) + + +# nb. colanderalchemy schema overrides +sa.DateTime.__colanderalchemy_config__ = {'typ': WuttaDateTime} diff --git a/src/wuttaweb/templates/deform/dateinput.pt b/src/wuttaweb/templates/deform/dateinput.pt new file mode 100644 index 0000000..b0e1285 --- /dev/null +++ b/src/wuttaweb/templates/deform/dateinput.pt @@ -0,0 +1,6 @@ +
+ ${field.start_mapping()} + + ${field.end_mapping()} +
diff --git a/src/wuttaweb/templates/deform/datetimeinput.pt b/src/wuttaweb/templates/deform/datetimeinput.pt new file mode 100644 index 0000000..b617929 --- /dev/null +++ b/src/wuttaweb/templates/deform/datetimeinput.pt @@ -0,0 +1,10 @@ +
+ ${field.start_mapping()} + + + ${field.end_mapping()} +
diff --git a/src/wuttaweb/templates/wutta-components.mako b/src/wuttaweb/templates/wutta-components.mako index b52992e..6030840 100644 --- a/src/wuttaweb/templates/wutta-components.mako +++ b/src/wuttaweb/templates/wutta-components.mako @@ -2,6 +2,8 @@ <%def name="make_wutta_components()"> ${self.make_wutta_request_mixin()} ${self.make_wutta_button_component()} + ${self.make_wutta_datepicker_component()} + ${self.make_wutta_timepicker_component()} ${self.make_wutta_filter_component()} ${self.make_wutta_filter_value_component()} @@ -149,6 +151,141 @@ +<%def name="make_wutta_datepicker_component()"> + + + + +<%def name="make_wutta_timepicker_component()"> + + + + <%def name="make_wutta_filter_component()">