fix: improve built-in grid renderer logic
- add `render_batch_id()` - allow kwargs for `render_currency()` - caller may specify built-in renderer w/ string identifier
This commit is contained in:
parent
2de08ad50d
commit
b3f1f8b6d9
|
@ -389,7 +389,6 @@ class Grid:
|
||||||
self.key = key
|
self.key = key
|
||||||
self.data = data
|
self.data = data
|
||||||
self.labels = labels or {}
|
self.labels = labels or {}
|
||||||
self.renderers = renderers or {}
|
|
||||||
self.row_class = row_class
|
self.row_class = row_class
|
||||||
self.actions = actions or []
|
self.actions = actions or []
|
||||||
self.linked_columns = linked_columns or []
|
self.linked_columns = linked_columns or []
|
||||||
|
@ -399,6 +398,10 @@ class Grid:
|
||||||
self.app = self.config.get_app()
|
self.app = self.config.get_app()
|
||||||
|
|
||||||
self.set_columns(columns or self.get_columns())
|
self.set_columns(columns or self.get_columns())
|
||||||
|
self.renderers = {}
|
||||||
|
if renderers:
|
||||||
|
for key, val in renderers.items():
|
||||||
|
self.set_renderer(key, val)
|
||||||
self.set_default_renderers()
|
self.set_default_renderers()
|
||||||
self.set_tools(tools)
|
self.set_tools(tools)
|
||||||
|
|
||||||
|
@ -593,8 +596,29 @@ class Grid:
|
||||||
grid = Grid(request, columns=['foo', 'bar'])
|
grid = Grid(request, columns=['foo', 'bar'])
|
||||||
grid.set_renderer('foo', render_foo)
|
grid.set_renderer('foo', render_foo)
|
||||||
|
|
||||||
|
For convenience, in lieu of a renderer callable, you may
|
||||||
|
specify one of the following strings, which will be
|
||||||
|
interpreted as a built-in renderer callable, as shown below:
|
||||||
|
|
||||||
|
* ``'batch_id'`` -> :meth:`render_batch_id()`
|
||||||
|
* ``'boolean'`` -> :meth:`render_boolean()`
|
||||||
|
* ``'currency'`` -> :meth:`render_currency()`
|
||||||
|
* ``'datetime'`` -> :meth:`render_datetime()`
|
||||||
|
* ``'quantity'`` -> :meth:`render_quantity()`
|
||||||
|
|
||||||
Renderer overrides are tracked via :attr:`renderers`.
|
Renderer overrides are tracked via :attr:`renderers`.
|
||||||
"""
|
"""
|
||||||
|
builtins = {
|
||||||
|
'batch_id': self.render_batch_id,
|
||||||
|
'boolean': self.render_boolean,
|
||||||
|
'currency': self.render_currency,
|
||||||
|
'datetime': self.render_datetime,
|
||||||
|
'quantity': self.render_quantity,
|
||||||
|
}
|
||||||
|
|
||||||
|
if renderer in builtins:
|
||||||
|
renderer = builtins[renderer]
|
||||||
|
|
||||||
if kwargs:
|
if kwargs:
|
||||||
renderer = functools.partial(renderer, **kwargs)
|
renderer = functools.partial(renderer, **kwargs)
|
||||||
self.renderers[key] = renderer
|
self.renderers[key] = renderer
|
||||||
|
@ -1759,6 +1783,20 @@ class Grid:
|
||||||
# rendering methods
|
# rendering methods
|
||||||
##############################
|
##############################
|
||||||
|
|
||||||
|
def render_batch_id(self, obj, key, value):
|
||||||
|
"""
|
||||||
|
Column renderer for batch ID values.
|
||||||
|
|
||||||
|
This is not used automatically but you can use it explicitly::
|
||||||
|
|
||||||
|
grid.set_renderer('foo', 'batch_id')
|
||||||
|
"""
|
||||||
|
if value is None:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
batch_id = int(value)
|
||||||
|
return f'{batch_id:08d}'
|
||||||
|
|
||||||
def render_boolean(self, obj, key, value):
|
def render_boolean(self, obj, key, value):
|
||||||
"""
|
"""
|
||||||
Column renderer for boolean values.
|
Column renderer for boolean values.
|
||||||
|
@ -1774,7 +1812,7 @@ class Grid:
|
||||||
"""
|
"""
|
||||||
return self.app.render_boolean(value)
|
return self.app.render_boolean(value)
|
||||||
|
|
||||||
def render_currency(self, obj, key, value):
|
def render_currency(self, obj, key, value, **kwargs):
|
||||||
"""
|
"""
|
||||||
Column renderer for currency values.
|
Column renderer for currency values.
|
||||||
|
|
||||||
|
@ -1784,9 +1822,10 @@ class Grid:
|
||||||
|
|
||||||
This is not used automatically but you can use it explicitly::
|
This is not used automatically but you can use it explicitly::
|
||||||
|
|
||||||
grid.set_renderer('foo', grid.render_currency)
|
grid.set_renderer('foo', 'currency')
|
||||||
|
grid.set_renderer('foo', 'currency', scale=4)
|
||||||
"""
|
"""
|
||||||
return self.app.render_currency(value)
|
return self.app.render_currency(value, **kwargs)
|
||||||
|
|
||||||
def render_datetime(self, obj, key, value):
|
def render_datetime(self, obj, key, value):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -208,6 +208,11 @@ class TestGrid(WebTestCase):
|
||||||
self.assertIsNot(grid.renderers['foo'], render2)
|
self.assertIsNot(grid.renderers['foo'], render2)
|
||||||
self.assertEqual(grid.renderers['foo'](None, None, None), 42)
|
self.assertEqual(grid.renderers['foo'](None, None, None), 42)
|
||||||
|
|
||||||
|
# can use built-in string shortcut
|
||||||
|
grid.set_renderer('foo', 'quantity')
|
||||||
|
obj = MagicMock(foo=42.00)
|
||||||
|
self.assertEqual(grid.renderers['foo'](obj, 'foo', 42.00), '42')
|
||||||
|
|
||||||
def test_set_default_renderer(self):
|
def test_set_default_renderer(self):
|
||||||
model = self.app.model
|
model = self.app.model
|
||||||
|
|
||||||
|
@ -1343,6 +1348,17 @@ class TestGrid(WebTestCase):
|
||||||
# rendering methods
|
# rendering methods
|
||||||
##############################
|
##############################
|
||||||
|
|
||||||
|
def test_render_batch_id(self):
|
||||||
|
grid = self.make_grid(columns=['foo', 'bar'])
|
||||||
|
|
||||||
|
# null
|
||||||
|
obj = MagicMock(foo=None)
|
||||||
|
self.assertEqual(grid.render_batch_id(obj, 'foo', None), "")
|
||||||
|
|
||||||
|
# int
|
||||||
|
obj = MagicMock(foo=42)
|
||||||
|
self.assertEqual(grid.render_batch_id(obj, 'foo', 42), "00000042")
|
||||||
|
|
||||||
def test_render_boolean(self):
|
def test_render_boolean(self):
|
||||||
grid = self.make_grid(columns=['foo', 'bar'])
|
grid = self.make_grid(columns=['foo', 'bar'])
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue