From 38418a420014b801c666fb5a929e51830a989d05 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Fri, 14 Jul 2017 17:12:10 -0500 Subject: [PATCH] Provide default renderers for SA mapped tables, where possible --- tailbone/grids3/core.py | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/tailbone/grids3/core.py b/tailbone/grids3/core.py index 7477ed49..ab9a8535 100644 --- a/tailbone/grids3/core.py +++ b/tailbone/grids3/core.py @@ -152,6 +152,9 @@ class Grid(object): else: self.enums.pop(key, None) + def render_generic(self, obj, column_name): + return self.obtain_value(obj, column_name) + def render_boolean(self, obj, column_name): value = self.obtain_value(obj, column_name) return pretty_boolean(value) @@ -255,11 +258,33 @@ class Grid(object): :attr:`model_class`. """ renderers = dict(self.renderers) - # for column in self.columns: - # if column not in renderers: - # pass + + if self.model_class: + mapper = orm.class_mapper(self.model_class) + for prop in mapper.iterate_properties: + if isinstance(prop, orm.ColumnProperty) and not prop.key.endswith('uuid'): + if prop.key in self.columns and prop.key not in renderers: + if len(prop.columns) == 1: + coltype = prop.columns[0].type + renderers[prop.key] = self.get_renderer_for_column_type(coltype) + return renderers + def get_renderer_for_column_type(self, coltype): + """ + Returns an appropriate renderer according to the given SA column type. + """ + if isinstance(coltype, sa.Boolean): + return self.render_boolean + + if isinstance(coltype, sa.DateTime): + return self.render_datetime + + if isinstance(coltype, GPCType): + return self.render_gpc + + return self.render_generic + def checkbox_column_format(self, column_number, row_number, item): return HTML.td(self.render_checkbox(item), class_='checkbox')