3
0
Fork 0

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..
This commit is contained in:
Lance Edgar 2025-01-02 22:52:32 -06:00
parent 170afe650b
commit 86ffb5d58f
2 changed files with 47 additions and 0 deletions

View file

@ -372,6 +372,20 @@ class MasterView(View):
List of columns for the row grid. 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()`.
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_sort_defaults = None
rows_paginated = True rows_paginated = True
rows_paginate_on_backend = True rows_paginate_on_backend = True
rows_viewable = False
# current action # current action
listing = False listing = False
@ -2414,6 +2429,16 @@ class MasterView(View):
kwargs.setdefault('paginated', self.rows_paginated) kwargs.setdefault('paginated', self.rows_paginated)
kwargs.setdefault('paginate_on_backend', self.rows_paginate_on_backend) 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) grid = self.make_grid(**kwargs)
self.configure_row_grid(grid) self.configure_row_grid(grid)
grid.load_settings() grid.load_settings()
@ -2532,6 +2557,16 @@ class MasterView(View):
labels.update(cls.row_labels) labels.update(cls.row_labels)
return 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 # class methods
############################## ##############################

View file

@ -1655,6 +1655,18 @@ class TestMasterView(WebTestCase):
self.assertIsNone(grid.model_class) self.assertIsNone(grid.model_class)
self.assertEqual(grid.data, []) 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): def test_get_rows_title(self):
view = self.make_view() view = self.make_view()