From 86ffb5d58ff9b72954808b9152824df9e6b90161 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Thu, 2 Jan 2025 22:52:32 -0600 Subject: [PATCH] fix: add basic support for row grid "view" action links still no actual "view row" support just yet, but subclass can implement however they like.. --- src/wuttaweb/views/master.py | 35 +++++++++++++++++++++++++++++++++++ tests/views/test_master.py | 12 ++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/wuttaweb/views/master.py b/src/wuttaweb/views/master.py index ec7cdf8..3701980 100644 --- a/src/wuttaweb/views/master.py +++ b/src/wuttaweb/views/master.py @@ -372,6 +372,20 @@ class MasterView(View): List of columns for the row grid. This is optional; see also :meth:`get_row_grid_columns()`. + + This is optional; see also :meth:`get_row_grid_columns()`. + + .. attribute:: rows_viewable + + Boolean indicating whether the row model supports "viewing" - + i.e. it should have a "View" action in the row grid. + + (For now) If you enable this, you must also override + :meth:`get_row_action_url_view()`. + + .. note:: + This eventually will cause there to be a ``row_view`` route + to be configured as well. """ ############################## @@ -409,6 +423,7 @@ class MasterView(View): rows_sort_defaults = None rows_paginated = True rows_paginate_on_backend = True + rows_viewable = False # current action listing = False @@ -2414,6 +2429,16 @@ class MasterView(View): kwargs.setdefault('paginated', self.rows_paginated) kwargs.setdefault('paginate_on_backend', self.rows_paginate_on_backend) + if 'actions' not in kwargs: + actions = [] + + if self.rows_viewable: + actions.append(self.make_grid_action('view', icon='eye', + url=self.get_row_action_url_view)) + + if actions: + kwargs['actions'] = actions + grid = self.make_grid(**kwargs) self.configure_row_grid(grid) grid.load_settings() @@ -2532,6 +2557,16 @@ class MasterView(View): labels.update(cls.row_labels) return labels + def get_row_action_url_view(self, row, i): + """ + Must return the "view" action url for the given row object. + + Only relevant if :attr:`rows_viewable` is true. + + There is no default logic; subclass must override if needed. + """ + raise NotImplementedError + ############################## # class methods ############################## diff --git a/tests/views/test_master.py b/tests/views/test_master.py index 8e674eb..3d06f49 100644 --- a/tests/views/test_master.py +++ b/tests/views/test_master.py @@ -1655,6 +1655,18 @@ class TestMasterView(WebTestCase): self.assertIsNone(grid.model_class) self.assertEqual(grid.data, []) + # view action + with patch.object(view, 'rows_viewable', new=True): + with patch.object(view, 'get_row_action_url_view', return_value='#'): + grid = view.make_row_model_grid(person, data=[]) + self.assertEqual(len(grid.actions), 1) + self.assertEqual(grid.actions[0].key, 'view') + + def test_get_row_action_url_view(self): + view = self.make_view() + row = MagicMock() + self.assertRaises(NotImplementedError, view.get_row_action_url_view, row, 0) + def test_get_rows_title(self): view = self.make_view()