Provide default renderers for SA mapped tables, where possible

This commit is contained in:
Lance Edgar 2017-07-14 17:12:10 -05:00
parent 6954748452
commit 38418a4200

View file

@ -152,6 +152,9 @@ class Grid(object):
else: else:
self.enums.pop(key, None) 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): def render_boolean(self, obj, column_name):
value = self.obtain_value(obj, column_name) value = self.obtain_value(obj, column_name)
return pretty_boolean(value) return pretty_boolean(value)
@ -255,11 +258,33 @@ class Grid(object):
:attr:`model_class`. :attr:`model_class`.
""" """
renderers = dict(self.renderers) renderers = dict(self.renderers)
# for column in self.columns:
# if column not in renderers: if self.model_class:
# pass 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 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): def checkbox_column_format(self, column_number, row_number, item):
return HTML.td(self.render_checkbox(item), class_='checkbox') return HTML.td(self.render_checkbox(item), class_='checkbox')