From 927eb3b38c7be78a31a17366fa27f8c713c416ef Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Fri, 8 Dec 2017 17:38:52 -0600 Subject: [PATCH] Add custom schema type for jQuery time picker data --- tailbone/forms2/__init__.py | 1 + tailbone/forms2/core.py | 4 ++++ tailbone/forms2/types.py | 40 +++++++++++++++++++++++++++++++++++++ tailbone/forms2/widgets.py | 12 ----------- 4 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 tailbone/forms2/types.py diff --git a/tailbone/forms2/__init__.py b/tailbone/forms2/__init__.py index 753e2fea..7b412dca 100644 --- a/tailbone/forms2/__init__.py +++ b/tailbone/forms2/__init__.py @@ -26,5 +26,6 @@ Forms Library from __future__ import unicode_literals, absolute_import +from . import types from . import widgets from .core import Form diff --git a/tailbone/forms2/core.py b/tailbone/forms2/core.py index c17b1052..c93bc5fb 100644 --- a/tailbone/forms2/core.py +++ b/tailbone/forms2/core.py @@ -46,6 +46,7 @@ from pyramid.renderers import render from webhelpers2.html import tags, HTML from tailbone.util import raw_datetime +from . import types from .widgets import ReadonlyWidget, JQueryDateWidget, JQueryTimeWidget @@ -466,8 +467,11 @@ class Form(object): elif type_ == 'datetime_local': self.set_renderer(key, self.render_datetime_local) elif type_ == 'date_jquery': + # TODO: is this safe / a good idea? + # self.set_node(key, colander.Date()) self.set_widget(key, JQueryDateWidget()) elif type_ == 'time_jquery': + self.set_node(key, types.JQueryTime()) self.set_widget(key, JQueryTimeWidget()) elif type_ == 'duration': self.set_renderer(key, self.render_duration) diff --git a/tailbone/forms2/types.py b/tailbone/forms2/types.py new file mode 100644 index 00000000..2521adec --- /dev/null +++ b/tailbone/forms2/types.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8; -*- +################################################################################ +# +# Rattail -- Retail Software Framework +# Copyright © 2010-2017 Lance Edgar +# +# This file is part of Rattail. +# +# Rattail is free software: you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation, either version 3 of the License, or (at your option) any later +# version. +# +# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# Rattail. If not, see . +# +################################################################################ +""" +Form Schema Types +""" + +from __future__ import unicode_literals, absolute_import + +import colander + + +class JQueryTime(colander.Time): + """ + Custom type for jQuery widget Time data. + """ + + def deserialize(self, node, cstruct): + if not cstruct: + return colander.null + return colander.timeparse(cstruct, '%I:%M %p') diff --git a/tailbone/forms2/widgets.py b/tailbone/forms2/widgets.py index 90fd0930..cb0ecbad 100644 --- a/tailbone/forms2/widgets.py +++ b/tailbone/forms2/widgets.py @@ -87,18 +87,6 @@ class JQueryTimeWidget(dfwidget.TimeInputWidget): ('showPeriod', True), ) - def deserialize(self, field, pstruct): - if pstruct in ('', colander.null): - return colander.null - try: - validated = self._pstruct_schema.deserialize(pstruct) - except colander.Invalid as exc: - raise colander.Invalid(field.schema, "Invalid pstruct: %s" % exc) - value = validated['time_submit'] or validated['time'] - if value: - value = datetime.datetime.strptime(value, '%I:%M %p').strftime('%H:%M') - return value - class JQueryAutocompleteWidget(dfwidget.AutocompleteInputWidget): """