Coverage for src / lexigram / admin / actions / header_manager / types.py: 0%

78 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-08-11 02:25 +0800

1""" 

2Types and data structures for header action management. 

3 

4Provides enums, dataclasses, and protocols for header actions. 

5""" 

6 

7from __future__ import annotations 

8 

9from collections.abc import Callable 

10from dataclasses import dataclass, field 

11from enum import StrEnum 

12from typing import Any, Protocol, TypeVar 

13 

14T = TypeVar("T") 

15 

16 

17class HeaderActionStyle(StrEnum): 

18 """Visual style for header actions.""" 

19 

20 PRIMARY = "primary" 

21 SECONDARY = "secondary" 

22 SUCCESS = "success" 

23 DANGER = "danger" 

24 WARNING = "warning" 

25 INFO = "info" 

26 

27 

28class TableDensity(StrEnum): 

29 """Table row density options.""" 

30 

31 COMPACT = "compact" 

32 NORMAL = "normal" 

33 COMFORTABLE = "comfortable" 

34 

35 

36@dataclass(slots=True) 

37class HeaderAction: 

38 """Configuration for a header action.""" 

39 

40 name: str 

41 """Action identifier.""" 

42 

43 label: str 

44 """Display label.""" 

45 

46 handler: Callable[[], Any] | None = None 

47 """Async function to execute the action.""" 

48 

49 icon: str | None = None 

50 """Icon name.""" 

51 

52 style: HeaderActionStyle = HeaderActionStyle.SECONDARY 

53 """Visual style.""" 

54 

55 url: str | None = None 

56 """URL for link-based actions.""" 

57 

58 method: str = "GET" 

59 """HTTP method for URL-based actions.""" 

60 

61 open_in_modal: bool = False 

62 """Whether to open URL in a modal.""" 

63 

64 keyboard_shortcut: str | None = None 

65 """Keyboard shortcut.""" 

66 

67 visible: Callable[[], bool] = lambda: True 

68 """Function to determine if action is visible.""" 

69 

70 disabled: Callable[[], bool] = lambda: False 

71 """Function to determine if action is disabled.""" 

72 

73 tooltip: str | None = None 

74 """Tooltip text.""" 

75 

76 badge: str | None = None 

77 """Badge text.""" 

78 

79 position: str = "end" 

80 """Position in header (start/end).""" 

81 

82 metadata: dict[str, Any] = field(default_factory=dict) 

83 """Additional metadata.""" 

84 

85 

86@dataclass(slots=True) 

87class ColumnVisibilityConfig: 

88 """Configuration for column visibility toggle.""" 

89 

90 enabled: bool = True 

91 """Whether column visibility is enabled.""" 

92 

93 default_visible: list[str] = field(default_factory=list) 

94 """Columns visible by default.""" 

95 

96 always_visible: list[str] = field(default_factory=list) 

97 """Columns that cannot be hidden.""" 

98 

99 save_preference: bool = True 

100 """Whether to save user preferences.""" 

101 

102 storage_key: str = "table_column_visibility" 

103 """Storage key for saving preferences.""" 

104 

105 

106@dataclass(slots=True) 

107class DensityConfig: 

108 """Configuration for table density toggle.""" 

109 

110 enabled: bool = True 

111 """Whether density toggle is enabled.""" 

112 

113 default: TableDensity = TableDensity.NORMAL 

114 """Default density.""" 

115 

116 options: list[TableDensity] = field( 

117 default_factory=lambda: [ 

118 TableDensity.COMPACT, 

119 TableDensity.NORMAL, 

120 TableDensity.COMFORTABLE, 

121 ] 

122 ) 

123 """Available density options.""" 

124 

125 save_preference: bool = True 

126 """Whether to save user preferences.""" 

127 

128 storage_key: str = "table_density" 

129 """Storage key for saving preferences.""" 

130 

131 

132class IHeaderDataSource(Protocol[T]): 

133 """Protocol for data sources that support header operations.""" 

134 

135 async def create(self, data: dict[str, Any]) -> T: 

136 """Create a new record.""" 

137 ... 

138 

139 async def import_data(self, file_path: str, file_format: str = "csv") -> int: 

140 """Import data from file.""" 

141 ... 

142 

143 async def export_all(self, file_format: str = "csv") -> str: 

144 """Export all data to file.""" 

145 ... 

146 

147 async def refresh(self) -> list[T]: 

148 """Refresh table data.""" 

149 ...