From 6186700a668fcc61e3c48b8cb66ec0f238950d8a Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Tue, 26 Sep 2017 22:06:50 -0500 Subject: [PATCH] Add 'duration' type for new form fields this only supports readonly, for now.. --- tailbone/forms2/core.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tailbone/forms2/core.py b/tailbone/forms2/core.py index d613681b..48e36ad6 100644 --- a/tailbone/forms2/core.py +++ b/tailbone/forms2/core.py @@ -26,6 +26,7 @@ Forms Core from __future__ import unicode_literals, absolute_import +import datetime import logging import six @@ -33,7 +34,7 @@ import sqlalchemy as sa from sqlalchemy import orm 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 from colanderalchemy import SQLAlchemySchemaNode @@ -286,6 +287,8 @@ class Form(object): def set_type(self, key, type_): if type_ == 'datetime': self.set_renderer(key, self.render_datetime) + elif type_ == 'duration': + self.set_renderer(key, self.render_duration) elif type_ == 'boolean': self.set_renderer(key, self.render_boolean) self.set_widget(key, dfwidget.CheckboxWidget(true_val='True', false_val='False')) @@ -398,6 +401,12 @@ class Form(object): return "" 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): value = self.obtain_value(record, field_name) return pretty_boolean(value)