Coverage for src / lexigram / admin / ui / columns / column / visibility.py: 48%
25 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-08-11 17:07 +0800
« prev ^ index » next coverage.py v7.13.5, created at 2026-08-11 17:07 +0800
1"""
2Column visibility control methods.
3"""
5from __future__ import annotations
7from collections.abc import Callable
8from typing import Any, Self
11class ColumnVisibilityMixin:
12 """Mixin class containing visibility control methods."""
14 def visible(self, visible: bool | Callable = True) -> Self:
15 """
16 Control column visibility.
18 Can accept a boolean or a callable that returns a boolean,
19 allowing for dynamic visibility based on record data.
21 Args:
22 visible: Boolean or callable that returns boolean
24 Returns:
25 Self for method chaining
27 Example:
28 >>> # Static visibility
29 >>> TextColumn("internal_id").visible(False)
30 >>>
31 >>> # Dynamic visibility
32 >>> def show_if_admin(record):
33 ... return record.get("role") == "admin"
34 >>> TextColumn("secret").visible(show_if_admin)
35 """
36 if callable(visible):
37 self._visible_callback = visible
38 else:
39 self._visible = visible
40 return self
42 def visible_from(self, breakpoint_name: str) -> Self:
43 """
44 Show column starting from breakpoint (hidden on smaller screens).
46 Args:
47 breakpoint_name: 'sm', 'md', 'lg', 'xl', '2xl'
48 """
49 self._visibility_classes.append("hidden") # type: ignore[attr-defined]
50 self._visibility_classes.append(f"{breakpoint_name}:table-cell") # type: ignore[attr-defined]
51 return self
53 def hidden_from(self, breakpoint_name: str) -> Self:
54 """
55 Hide column starting from breakpoint (visible on smaller screens).
57 Args:
58 breakpoint_name: 'sm', 'md', 'lg', 'xl', '2xl'
59 """
60 self._visibility_classes.append(f"{breakpoint_name}:hidden") # type: ignore[attr-defined]
61 return self
63 def hidden_on_mobile(self) -> Self:
64 """Shorthand for hidden on mobile, visible on desktop (md breakpoint)."""
65 return self.visible_from("md")
67 def is_visible(
68 self,
69 user: Any = None,
70 resource_name: str | None = None,
71 record: dict | Any | None = None,
72 permission_service: Any = None,
73 ) -> bool:
74 """Check if column should be visible."""
75 # 1. Check callback if set
76 if self._visible_callback: # type: ignore[truthy-function]
77 return self._visible_callback(record)
79 # 2. Check PermissionService if user, resource_name, and service are provided
80 if user and resource_name and permission_service is not None:
81 # Ensure we are not accidentally treating record as user
82 if (
83 hasattr(user, "roles") or hasattr(user, "user_id")
84 ) and not permission_service.can_view_field(user, resource_name, self.name):
85 return False
87 return self._visible