Add 'duration' type for new form fields

this only supports readonly, for now..
This commit is contained in:
Lance Edgar 2017-09-26 22:06:50 -05:00
parent 47ce0fd448
commit 6186700a66

View file

@ -26,6 +26,7 @@ Forms Core
from __future__ import unicode_literals, absolute_import from __future__ import unicode_literals, absolute_import
import datetime
import logging import logging
import six import six
@ -33,7 +34,7 @@ import sqlalchemy as sa
from sqlalchemy import orm from sqlalchemy import orm
from sqlalchemy.ext.associationproxy import AssociationProxy, ASSOCIATION_PROXY from sqlalchemy.ext.associationproxy import AssociationProxy, ASSOCIATION_PROXY
from rattail.util import prettify, pretty_boolean from rattail.util import prettify, pretty_boolean, pretty_hours
import colander import colander
from colanderalchemy import SQLAlchemySchemaNode from colanderalchemy import SQLAlchemySchemaNode
@ -286,6 +287,8 @@ class Form(object):
def set_type(self, key, type_): def set_type(self, key, type_):
if type_ == 'datetime': if type_ == 'datetime':
self.set_renderer(key, self.render_datetime) self.set_renderer(key, self.render_datetime)
elif type_ == 'duration':
self.set_renderer(key, self.render_duration)
elif type_ == 'boolean': elif type_ == 'boolean':
self.set_renderer(key, self.render_boolean) self.set_renderer(key, self.render_boolean)
self.set_widget(key, dfwidget.CheckboxWidget(true_val='True', false_val='False')) self.set_widget(key, dfwidget.CheckboxWidget(true_val='True', false_val='False'))
@ -398,6 +401,12 @@ class Form(object):
return "" return ""
return raw_datetime(self.request.rattail_config, value) return raw_datetime(self.request.rattail_config, value)
def render_duration(self, record, field_name):
value = self.obtain_value(record, field_name)
if value is None:
return ""
return pretty_hours(datetime.timedelta(seconds=value))
def render_boolean(self, record, field_name): def render_boolean(self, record, field_name):
value = self.obtain_value(record, field_name) value = self.obtain_value(record, field_name)
return pretty_boolean(value) return pretty_boolean(value)