Coverage for src/lexigram/admin/ui/views.py: 0%

50 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-21 13:43 +0800

1""" 

2Concrete View classes for declarative Resource configuration. 

3Wraps LayoutConfig for a more user-friendly API. 

4""" 

5 

6from __future__ import annotations 

7 

8from dataclasses import dataclass 

9from typing import TYPE_CHECKING, Any 

10 

11from lexigram.admin.layout.layout_manager import LayoutConfig, LayoutType 

12 

13if TYPE_CHECKING: 

14 from collections.abc import Callable 

15 

16 from htpy import Element 

17 

18 

19class View: 

20 """Base class for all Views.""" 

21 

22 type: LayoutType 

23 label: str 

24 icon: str 

25 enabled: bool = True 

26 

27 def to_config(self) -> LayoutConfig: 

28 """Convert to LayoutConfig.""" 

29 raise NotImplementedError 

30 

31 

32@dataclass 

33class ListView(View): 

34 """Standard list/table view.""" 

35 

36 type: LayoutType = LayoutType.LIST 

37 label: str = "List" 

38 icon: str = "list" 

39 enabled: bool = True 

40 

41 def to_config(self) -> LayoutConfig: 

42 return LayoutConfig( 

43 type=self.type, 

44 label=self.label, 

45 icon=self.icon, 

46 enabled=self.enabled, 

47 ) 

48 

49 

50@dataclass 

51class GridView(View): 

52 """Grid view with cards.""" 

53 

54 type: LayoutType = LayoutType.GRID 

55 label: str = "Grid" 

56 icon: str = "grid" 

57 enabled: bool = True 

58 columns: int = 3 

59 card_template: Callable[[dict[str, Any]], Element] | None = None 

60 

61 def to_config(self) -> LayoutConfig: 

62 return LayoutConfig( 

63 type=self.type, 

64 label=self.label, 

65 icon=self.icon, 

66 enabled=self.enabled, 

67 columns=self.columns, 

68 card_template=self.card_template, 

69 ) 

70 

71 

72@dataclass 

73class CalendarView(View): 

74 """Calendar view for date-based data.""" 

75 

76 type: LayoutType = LayoutType.CALENDAR 

77 label: str = "Calendar" 

78 icon: str = "calendar" 

79 enabled: bool = True 

80 date_field: str = "created_at" 

81 title_field: str = "title" 

82 

83 def to_config(self) -> LayoutConfig: 

84 return LayoutConfig( 

85 type=self.type, 

86 label=self.label, 

87 icon=self.icon, 

88 enabled=self.enabled, 

89 date_field=self.date_field, 

90 title_field=self.title_field, 

91 ) 

92 

93 

94@dataclass 

95class MapView(View): 

96 """Map view for location data.""" 

97 

98 type: LayoutType = LayoutType.MAP 

99 label: str = "Map" 

100 icon: str = "map" 

101 enabled: bool = True 

102 latitude_field: str = "latitude" 

103 longitude_field: str = "longitude" 

104 marker_template: Callable[[dict[str, Any]], Element] | None = None 

105 

106 def to_config(self) -> LayoutConfig: 

107 return LayoutConfig( # type: ignore[call-arg] 

108 type=self.type, 

109 label=self.label, 

110 icon=self.icon, 

111 enabled=self.enabled, 

112 latitude_field=self.latitude_field, 

113 longitude_field=self.longitude_field, 

114 marker_template=self.marker_template, 

115 )