fix: track column-only labels separately for grid
otherwise `set_label(.., column_only=True)` would not "remember" that the caller requested column only, e.g. when extra filters are added after the fact
This commit is contained in:
parent
c4f735fda8
commit
97e5a96cd6
3 changed files with 87 additions and 21 deletions
|
|
@ -119,9 +119,17 @@ class Grid: # pylint: disable=too-many-instance-attributes,too-many-public-meth
|
|||
|
||||
.. attribute:: labels
|
||||
|
||||
Dict of column label overrides.
|
||||
Dict of column and/or filter label overrides.
|
||||
|
||||
See also :meth:`get_label()` and :meth:`set_label()`.
|
||||
See also :attr:`column_labels`, :meth:`set_label()`,
|
||||
:meth:`get_column_label()` and :meth:`get_filter_label()`.
|
||||
|
||||
.. attribute:: column_labels
|
||||
|
||||
Dict of label overrides for column only.
|
||||
|
||||
See also :attr:`labels`, :meth:`set_label()` and
|
||||
:meth:`get_column_label()`.
|
||||
|
||||
.. attribute:: centered
|
||||
|
||||
|
|
@ -434,6 +442,7 @@ class Grid: # pylint: disable=too-many-instance-attributes,too-many-public-meth
|
|||
self.key = key
|
||||
self.data = data
|
||||
self.labels = labels or {}
|
||||
self.column_labels = {}
|
||||
self.checkable = checkable
|
||||
self.row_class = row_class
|
||||
self.actions = actions or []
|
||||
|
|
@ -628,35 +637,61 @@ class Grid: # pylint: disable=too-many-instance-attributes,too-many-public-meth
|
|||
|
||||
def set_label(self, key, label, column_only=False):
|
||||
"""
|
||||
Set/override the label for a column.
|
||||
Set/override the label for a column and/or filter.
|
||||
|
||||
:param key: Name of column.
|
||||
:param key: Key for the column/filter.
|
||||
|
||||
:param label: New label for the column header.
|
||||
:param label: New label for the column and/or filter.
|
||||
|
||||
:param column_only: Boolean indicating whether the label
|
||||
should be applied *only* to the column header (if
|
||||
``True``), vs. applying also to the filter (if ``False``).
|
||||
|
||||
See also :meth:`get_label()`. Label overrides are tracked via
|
||||
:attr:`labels`.
|
||||
See also :meth:`get_column_label()` and
|
||||
:meth:`get_filter_label()`. Label overrides are tracked via
|
||||
:attr:`labels` and :attr:`column_labels`.
|
||||
"""
|
||||
if column_only:
|
||||
self.column_labels[key] = label
|
||||
else:
|
||||
self.labels[key] = label
|
||||
|
||||
if not column_only and key in self.filters:
|
||||
if key in self.filters:
|
||||
self.filters[key].label = label
|
||||
|
||||
def get_label(self, key):
|
||||
def get_label(self, key): # pylint: disable=missing-function-docstring
|
||||
warnings.warn(
|
||||
"Grid.get_label() is deprecated; please use "
|
||||
"get_filter_label() or get_column_label() instead",
|
||||
DeprecationWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
return self.get_filter_label(key)
|
||||
|
||||
def get_filter_label(self, key):
|
||||
"""
|
||||
Returns the label text for a given filter.
|
||||
|
||||
If no override is defined, the label is derived from ``key``.
|
||||
|
||||
See also :meth:`set_label()` and :meth:`get_column_label()`.
|
||||
"""
|
||||
if key in self.labels:
|
||||
return self.labels[key]
|
||||
|
||||
return self.app.make_title(key)
|
||||
|
||||
def get_column_label(self, key):
|
||||
"""
|
||||
Returns the label text for a given column.
|
||||
|
||||
If no override is defined, the label is derived from ``key``.
|
||||
|
||||
See also :meth:`set_label()`.
|
||||
See also :meth:`set_label()` and :meth:`get_filter_label()`.
|
||||
"""
|
||||
if key in self.labels:
|
||||
return self.labels[key]
|
||||
return self.app.make_title(key)
|
||||
if key in self.column_labels:
|
||||
return self.column_labels[key]
|
||||
|
||||
return self.get_filter_label(key)
|
||||
|
||||
def set_centered(self, key, centered=True):
|
||||
"""
|
||||
|
|
@ -2314,7 +2349,7 @@ class Grid: # pylint: disable=too-many-instance-attributes,too-many-public-meth
|
|||
columns.append(
|
||||
{
|
||||
"field": name,
|
||||
"label": self.get_label(name),
|
||||
"label": self.get_column_label(name),
|
||||
"hidden": self.is_hidden(name),
|
||||
"sortable": self.is_sortable(name),
|
||||
"searchable": self.is_searchable(name),
|
||||
|
|
|
|||
|
|
@ -198,17 +198,48 @@ class TestGrid(WebTestCase):
|
|||
# can replace label
|
||||
grid.set_label("name", "Different")
|
||||
self.assertEqual(grid.labels["name"], "Different")
|
||||
self.assertEqual(grid.get_label("name"), "Different")
|
||||
self.assertEqual(grid.get_filter_label("name"), "Different")
|
||||
self.assertEqual(grid.get_column_label("name"), "Different")
|
||||
|
||||
# can update only column, not filter
|
||||
self.assertEqual(grid.labels, {"name": "Different"})
|
||||
self.assertEqual(grid.column_labels, {})
|
||||
self.assertIn("name", grid.filters)
|
||||
self.assertEqual(grid.filters["name"].label, "Different")
|
||||
grid.set_label("name", "COLUMN ONLY", column_only=True)
|
||||
self.assertEqual(grid.get_label("name"), "COLUMN ONLY")
|
||||
self.assertEqual(grid.filters["name"].label, "Different")
|
||||
self.assertEqual(grid.get_column_label("name"), "COLUMN ONLY")
|
||||
self.assertEqual(grid.get_filter_label("name"), "Different")
|
||||
|
||||
def test_get_filter_label(self):
|
||||
grid = self.make_grid(columns=["foo", "bar"])
|
||||
self.assertEqual(grid.labels, {})
|
||||
|
||||
# default derived from key
|
||||
self.assertEqual(grid.get_filter_label("foo"), "Foo")
|
||||
|
||||
# can override
|
||||
grid.set_label("foo", "Different")
|
||||
self.assertEqual(grid.get_filter_label("foo"), "Different")
|
||||
|
||||
def test_get_column_label(self):
|
||||
grid = self.make_grid(columns=["foo", "bar"])
|
||||
self.assertEqual(grid.labels, {})
|
||||
|
||||
# default derived from key
|
||||
self.assertEqual(grid.get_column_label("foo"), "Foo")
|
||||
|
||||
# can override "globally"
|
||||
grid.set_label("foo", "Different")
|
||||
self.assertEqual(grid.get_column_label("foo"), "Different")
|
||||
|
||||
# can override for "just column"
|
||||
grid.set_label("foo", "Col Lbl", column_only=True)
|
||||
self.assertEqual(grid.get_column_label("foo"), "Col Lbl")
|
||||
|
||||
def test_get_label(self):
|
||||
# nb. the get_label() method is deprecated; can remove this
|
||||
# test eventually
|
||||
|
||||
grid = self.make_grid(columns=["foo", "bar"])
|
||||
self.assertEqual(grid.labels, {})
|
||||
|
||||
|
|
|
|||
|
|
@ -454,8 +454,8 @@ class TestBatchMasterView(WebTestCase):
|
|||
self.assertEqual(
|
||||
grid.columns, ["sequence", "status_code", "modified"]
|
||||
)
|
||||
self.assertIn("sequence", grid.labels)
|
||||
self.assertEqual(grid.labels["sequence"], "Seq.")
|
||||
self.assertIn("sequence", grid.column_labels)
|
||||
self.assertEqual(grid.column_labels["sequence"], "Seq.")
|
||||
self.assertEqual(grid.tools, {})
|
||||
|
||||
# missing 'sequence' column
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue