Add custom schema type for jQuery time picker data

This commit is contained in:
Lance Edgar 2017-12-08 17:38:52 -06:00
parent abd47ae7ae
commit 927eb3b38c
4 changed files with 45 additions and 12 deletions

View file

@ -26,5 +26,6 @@ Forms Library
from __future__ import unicode_literals, absolute_import from __future__ import unicode_literals, absolute_import
from . import types
from . import widgets from . import widgets
from .core import Form from .core import Form

View file

@ -46,6 +46,7 @@ from pyramid.renderers import render
from webhelpers2.html import tags, HTML from webhelpers2.html import tags, HTML
from tailbone.util import raw_datetime from tailbone.util import raw_datetime
from . import types
from .widgets import ReadonlyWidget, JQueryDateWidget, JQueryTimeWidget from .widgets import ReadonlyWidget, JQueryDateWidget, JQueryTimeWidget
@ -466,8 +467,11 @@ class Form(object):
elif type_ == 'datetime_local': elif type_ == 'datetime_local':
self.set_renderer(key, self.render_datetime_local) self.set_renderer(key, self.render_datetime_local)
elif type_ == 'date_jquery': elif type_ == 'date_jquery':
# TODO: is this safe / a good idea?
# self.set_node(key, colander.Date())
self.set_widget(key, JQueryDateWidget()) self.set_widget(key, JQueryDateWidget())
elif type_ == 'time_jquery': elif type_ == 'time_jquery':
self.set_node(key, types.JQueryTime())
self.set_widget(key, JQueryTimeWidget()) self.set_widget(key, JQueryTimeWidget())
elif type_ == 'duration': elif type_ == 'duration':
self.set_renderer(key, self.render_duration) self.set_renderer(key, self.render_duration)

40
tailbone/forms2/types.py Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
#
################################################################################
"""
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')

View file

@ -87,18 +87,6 @@ class JQueryTimeWidget(dfwidget.TimeInputWidget):
('showPeriod', True), ('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): class JQueryAutocompleteWidget(dfwidget.AutocompleteInputWidget):
""" """