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
|
.. 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
|
.. attribute:: centered
|
||||||
|
|
||||||
|
|
@ -434,6 +442,7 @@ class Grid: # pylint: disable=too-many-instance-attributes,too-many-public-meth
|
||||||
self.key = key
|
self.key = key
|
||||||
self.data = data
|
self.data = data
|
||||||
self.labels = labels or {}
|
self.labels = labels or {}
|
||||||
|
self.column_labels = {}
|
||||||
self.checkable = checkable
|
self.checkable = checkable
|
||||||
self.row_class = row_class
|
self.row_class = row_class
|
||||||
self.actions = actions or []
|
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):
|
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
|
:param column_only: Boolean indicating whether the label
|
||||||
should be applied *only* to the column header (if
|
should be applied *only* to the column header (if
|
||||||
``True``), vs. applying also to the filter (if ``False``).
|
``True``), vs. applying also to the filter (if ``False``).
|
||||||
|
|
||||||
See also :meth:`get_label()`. Label overrides are tracked via
|
See also :meth:`get_column_label()` and
|
||||||
:attr:`labels`.
|
:meth:`get_filter_label()`. Label overrides are tracked via
|
||||||
|
:attr:`labels` and :attr:`column_labels`.
|
||||||
"""
|
"""
|
||||||
self.labels[key] = label
|
if column_only:
|
||||||
|
self.column_labels[key] = label
|
||||||
|
else:
|
||||||
|
self.labels[key] = label
|
||||||
|
if key in self.filters:
|
||||||
|
self.filters[key].label = label
|
||||||
|
|
||||||
if not column_only and key in self.filters:
|
def get_label(self, key): # pylint: disable=missing-function-docstring
|
||||||
self.filters[key].label = label
|
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_label(self, 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.
|
Returns the label text for a given column.
|
||||||
|
|
||||||
If no override is defined, the label is derived from ``key``.
|
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:
|
if key in self.column_labels:
|
||||||
return self.labels[key]
|
return self.column_labels[key]
|
||||||
return self.app.make_title(key)
|
|
||||||
|
return self.get_filter_label(key)
|
||||||
|
|
||||||
def set_centered(self, key, centered=True):
|
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(
|
columns.append(
|
||||||
{
|
{
|
||||||
"field": name,
|
"field": name,
|
||||||
"label": self.get_label(name),
|
"label": self.get_column_label(name),
|
||||||
"hidden": self.is_hidden(name),
|
"hidden": self.is_hidden(name),
|
||||||
"sortable": self.is_sortable(name),
|
"sortable": self.is_sortable(name),
|
||||||
"searchable": self.is_searchable(name),
|
"searchable": self.is_searchable(name),
|
||||||
|
|
|
||||||
|
|
@ -198,17 +198,48 @@ class TestGrid(WebTestCase):
|
||||||
# can replace label
|
# can replace label
|
||||||
grid.set_label("name", "Different")
|
grid.set_label("name", "Different")
|
||||||
self.assertEqual(grid.labels["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
|
# can update only column, not filter
|
||||||
self.assertEqual(grid.labels, {"name": "Different"})
|
self.assertEqual(grid.labels, {"name": "Different"})
|
||||||
|
self.assertEqual(grid.column_labels, {})
|
||||||
self.assertIn("name", grid.filters)
|
self.assertIn("name", grid.filters)
|
||||||
self.assertEqual(grid.filters["name"].label, "Different")
|
self.assertEqual(grid.filters["name"].label, "Different")
|
||||||
grid.set_label("name", "COLUMN ONLY", column_only=True)
|
grid.set_label("name", "COLUMN ONLY", column_only=True)
|
||||||
self.assertEqual(grid.get_label("name"), "COLUMN ONLY")
|
self.assertEqual(grid.get_column_label("name"), "COLUMN ONLY")
|
||||||
self.assertEqual(grid.filters["name"].label, "Different")
|
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):
|
def test_get_label(self):
|
||||||
|
# nb. the get_label() method is deprecated; can remove this
|
||||||
|
# test eventually
|
||||||
|
|
||||||
grid = self.make_grid(columns=["foo", "bar"])
|
grid = self.make_grid(columns=["foo", "bar"])
|
||||||
self.assertEqual(grid.labels, {})
|
self.assertEqual(grid.labels, {})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -454,8 +454,8 @@ class TestBatchMasterView(WebTestCase):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
grid.columns, ["sequence", "status_code", "modified"]
|
grid.columns, ["sequence", "status_code", "modified"]
|
||||||
)
|
)
|
||||||
self.assertIn("sequence", grid.labels)
|
self.assertIn("sequence", grid.column_labels)
|
||||||
self.assertEqual(grid.labels["sequence"], "Seq.")
|
self.assertEqual(grid.column_labels["sequence"], "Seq.")
|
||||||
self.assertEqual(grid.tools, {})
|
self.assertEqual(grid.tools, {})
|
||||||
|
|
||||||
# missing 'sequence' column
|
# missing 'sequence' column
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue