diff --git a/src/wuttaweb/grids/base.py b/src/wuttaweb/grids/base.py index 2ac2646..1906997 100644 --- a/src/wuttaweb/grids/base.py +++ b/src/wuttaweb/grids/base.py @@ -2515,8 +2515,10 @@ class Grid: # pylint: disable=too-many-instance-attributes,too-many-public-meth # customize value rendering where applicable for key, renderer in self.renderers.items(): - value = record.get(key, None) - record[key] = renderer(original_record, key, value) + # nb. no need to render if column not included + if key in self.columns: + value = record.get(key, None) + record[key] = renderer(original_record, key, value) # add action urls to each record for action in self.actions: diff --git a/tests/grids/test_base.py b/tests/grids/test_base.py index 1e193ea..15a7a06 100644 --- a/tests/grids/test_base.py +++ b/tests/grids/test_base.py @@ -1985,11 +1985,13 @@ class TestGrid(WebTestCase): # typical data is a list mydata = [ - {"foo": "bar"}, + {"foo": "bar", "baz": "zoo"}, ] - grid = self.make_grid(columns=["foo"], data=mydata) + grid = self.make_grid(columns=["foo", "baz"], data=mydata) context = grid.get_vue_context() - self.assertEqual(context, {"data": [{"foo": "bar"}], "row_classes": {}}) + self.assertEqual( + context, {"data": [{"foo": "bar", "baz": "zoo"}], "row_classes": {}} + ) # non-declared columns are discarded mydata = [ @@ -2001,27 +2003,47 @@ class TestGrid(WebTestCase): # if grid has actions, that list may be supplemented mydata = [ - {"foo": "bar"}, + {"foo": "bar", "baz": "zoo"}, ] - grid = self.make_grid(columns=["foo"], data=mydata) + grid = self.make_grid(columns=["foo", "baz"], data=mydata) grid.actions.append(mod.GridAction(self.request, "view", url="/blarg")) context = grid.get_vue_context() self.assertIsNot(context["data"], mydata) self.assertEqual( context, - {"data": [{"foo": "bar", "_action_url_view": "/blarg"}], "row_classes": {}}, + { + "data": [{"foo": "bar", "baz": "zoo", "_action_url_view": "/blarg"}], + "row_classes": {}, + }, ) # can override value rendering - grid.set_renderer("foo", lambda record, key, value: "blah blah") + renderer = MagicMock(return_value="blah blah") + grid.set_renderer("foo", renderer) context = grid.get_vue_context() self.assertEqual( context, { - "data": [{"foo": "blah blah", "_action_url_view": "/blarg"}], + "data": [ + {"foo": "blah blah", "baz": "zoo", "_action_url_view": "/blarg"} + ], "row_classes": {}, }, ) + renderer.assert_called_once_with({"foo": "bar", "baz": "zoo"}, "foo", "bar") + + # custom rendering skipped if column not included + grid.remove("foo") + renderer.reset_mock() + context = grid.get_vue_context() + self.assertEqual( + context, + { + "data": [{"baz": "zoo", "_action_url_view": "/blarg"}], + "row_classes": {}, + }, + ) + renderer.assert_not_called() # can set row class grid.row_class = "whatever" @@ -2029,7 +2051,7 @@ class TestGrid(WebTestCase): self.assertEqual( context, { - "data": [{"foo": "blah blah", "_action_url_view": "/blarg"}], + "data": [{"baz": "zoo", "_action_url_view": "/blarg"}], "row_classes": {"0": "whatever"}, }, )