Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# Imports 

2 

3from django.utils.safestring import mark_safe 

4from .base import Element 

5 

6# Exports 

7 

8__all__ = ( 

9 "Column", 

10 "Row", 

11 "Table", 

12) 

13 

14# Classes 

15 

16 

17class Column(Element): 

18 """A column (header) in an HTML table.""" 

19 

20 def __init__(self, name, help_text=None, label=None, **kwargs): 

21 """Initialize the column. 

22 

23 :param name: The programmatic name of the column. For example, the field name. 

24 :type name: str 

25 

26 :param help_text: Help text for the column. 

27 :type help_text: str 

28 

29 :param label: The column label. If omitted, the name is converted into a label. 

30 :type label: str 

31 

32 """ 

33 self.help_text = help_text 

34 self.label = label or name.replace("_", " ").title() 

35 self.name = name 

36 

37 super().__init__("th", content=str(self.label), **kwargs) 

38 

39 @property 

40 def title(self): 

41 """Get the column label. Alias for ``label``.""" 

42 return self.label 

43 

44 

45class Row(Element): 

46 """A row in an HTML table.""" 

47 

48 def __init__(self, data, **kwargs): 

49 """Initialize the row. 

50 

51 :param data: The data to be included in the row. 

52 :type data: list | tuple 

53 

54 """ 

55 super().__init__("tr", **kwargs) 

56 

57 self._data = data 

58 

59 @property 

60 def data(self): 

61 """Alias for ``_data``.""" 

62 return self._data 

63 

64 @mark_safe 

65 def to_html(self): 

66 """Export the row to HTML.""" 

67 a = list() 

68 a.append(self.get_open_tag()) 

69 for d in self._data: 

70 a.append("<td>%s</td>" % d) 

71 

72 a.append(self.get_close_tag()) 

73 

74 return "\n".join(a) 

75 

76 

77class Table(Element): 

78 """An HTML table.""" 

79 

80 def __init__(self, caption=None, columns=None, rows=None, **kwargs): 

81 """Initialize the table. 

82 

83 :param caption: Optional caption for the table. 

84 :type caption: str 

85 

86 :param columns: The columns to be included in the table. 

87 :type columns: list[superdjango.html.library.Column] 

88 

89 :param rows: The rows to be included in the table. 

90 :type rows: list[superdjango.html.library.Row] 

91 

92 """ 

93 super().__init__("table", **kwargs) 

94 

95 self.caption = caption 

96 self.columns = columns or list() 

97 self.rows = rows or list() 

98 

99 def __iter__(self): 

100 return iter(self.rows) 

101 

102 def column(self, name, help_text=None, label=None, **kwargs): 

103 """Add a column to the table. 

104 

105 :param name: The programmatic name of the column. For example, the field name. 

106 :type name: str 

107 

108 :param help_text: Help text for the column. 

109 :type help_text: str 

110 

111 :param label: The column label. If omitted, the name is converted into a label. 

112 :type label: str 

113 

114 :rtype: superdjango.html.library.Column 

115 

116 """ 

117 column = Column(name, help_text=help_text, label=label, **kwargs) 

118 self.columns.append(column) 

119 

120 return column 

121 

122 def row(self, data, **kwargs): 

123 """Add a row to the table. 

124 

125 :param data: The data to be included in the row. 

126 :type data: list | tuple 

127 

128 :rtype: superdjango.html.library.Row 

129 

130 """ 

131 row = Row(data, **kwargs) 

132 self.rows.append(row) 

133 

134 return row 

135 

136 @mark_safe 

137 def to_html(self): 

138 """Export the table as HTML. 

139 

140 :rtype: str 

141 

142 """ 

143 a = list() 

144 a.append(self.get_open_tag()) 

145 

146 if self.caption is not None: 

147 caption = Element("caption", content=self.caption) 

148 a.append(caption.to_html()) 

149 

150 a.append("<thead><tr>") 

151 for column in self.columns: 

152 a.append(column.to_html()) 

153 a.append("</tr></thead>") 

154 

155 a.append("<tbody>") 

156 

157 for row in self.rows: 

158 a.append(row.to_html()) 

159 

160 a.append("</tbody>") 

161 

162 a.append(self.get_close_tag()) 

163 

164 return "\n".join(a)