Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/wtforms/fields/simple.py: 64%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

67 statements  

1from .. import widgets 

2from .core import Field 

3 

4__all__ = ( 

5 "ButtonField", 

6 "BooleanField", 

7 "TextAreaField", 

8 "PasswordField", 

9 "FileField", 

10 "MultipleFileField", 

11 "HiddenField", 

12 "SearchField", 

13 "SubmitField", 

14 "StringField", 

15 "TelField", 

16 "URLField", 

17 "EmailField", 

18 "ColorField", 

19) 

20 

21 

22class BooleanField(Field): 

23 """ 

24 Represents an :mdn-input:`checkbox`. Set the ``checked``-status by using 

25 the ``default``-option. Any value for ``default``, e.g. 

26 ``default="checked"``, puts ``checked`` into the HTML element and sets 

27 the ``data`` to ``True`` 

28 

29 :param false_values: 

30 If provided, a sequence of strings each of which is an exact match 

31 string of what is considered a "false" value. Defaults to the tuple 

32 ``(False, "false", "")`` 

33 """ 

34 

35 widget = widgets.CheckboxInput() 

36 false_values = (False, "false", "") 

37 

38 def __init__(self, label=None, validators=None, false_values=None, **kwargs): 

39 super().__init__(label, validators, **kwargs) 

40 if false_values is not None: 

41 self.false_values = false_values 

42 

43 def process_data(self, value): 

44 self.data = bool(value) 

45 

46 def process_formdata(self, valuelist): 

47 if not valuelist or valuelist[0] in self.false_values: 

48 self.data = False 

49 else: 

50 self.data = True 

51 

52 def _value(self): 

53 if self.raw_data: 

54 return str(self.raw_data[0]) 

55 return "y" 

56 

57 

58class StringField(Field): 

59 """ 

60 This field is the base for most of the more complicated fields, and 

61 represents an :mdn-input:`text`. 

62 """ 

63 

64 widget = widgets.TextInput() 

65 

66 def process_formdata(self, valuelist): 

67 if valuelist: 

68 self.data = valuelist[0] 

69 

70 def _value(self): 

71 return str(self.data) if self.data is not None else "" 

72 

73 

74class ButtonField(StringField): 

75 """ 

76 Represents a :mdn-tag:`button` with ``type="submit"``. 

77 

78 The field's label is used as the visible text of the button, not as the 

79 submitted value. If the button is used to submit the form, the submitted 

80 value is stored as a string. 

81 

82 The rendered ``value`` attribute comes from the field data passed at form 

83 construction time, or defaults to an empty string. If the button is not 

84 clicked, the field data is `None`. Pass ``label=`` when rendering to 

85 override the visible button text. 

86 

87 The label is HTML-escaped at render time. To embed HTML in the button 

88 content (an icon, formatted text), pass a :class:`markupsafe.Markup` 

89 instance — at declaration or as the render-time ``label=``:: 

90 

91 from markupsafe import Markup 

92 

93 class F(Form): 

94 save = ButtonField(Markup('<i class="icon-save"></i> Save')) 

95 

96 # or at render time: 

97 form.save(label=Markup('<i class="icon-save"></i> Save')) 

98 """ 

99 

100 widget = widgets.Button() 

101 

102 def process_data(self, value): 

103 self.data = None 

104 

105 def process_formdata(self, valuelist): 

106 if valuelist: 

107 self.data = valuelist[0] 

108 else: 

109 self.data = None 

110 

111 def _value(self): 

112 if self.raw_data: 

113 return str(self.raw_data[0]) 

114 if self.object_data is not None: 

115 return str(self.object_data) 

116 return "" 

117 

118 

119class TextAreaField(StringField): 

120 """ 

121 This field represents an HTML :mdn-tag:`textarea` and can be used to take 

122 multi-line input. 

123 """ 

124 

125 widget = widgets.TextArea() 

126 

127 

128class PasswordField(StringField): 

129 """ 

130 A StringField, except renders an :mdn-input:`password`. 

131 

132 Also, whatever value is accepted by this field is not rendered back 

133 to the browser like normal fields. 

134 """ 

135 

136 widget = widgets.PasswordInput() 

137 

138 

139class FileField(Field): 

140 """Renders an :mdn-input:`file` field. 

141 

142 By default, the value will be the filename sent in the form data. 

143 WTForms **does not** deal with frameworks' file handling capabilities. 

144 A WTForms extension for a framework may replace the filename value 

145 with an object representing the uploaded data. 

146 """ 

147 

148 widget = widgets.FileInput() 

149 

150 def _value(self): 

151 # browser ignores value of file input for security 

152 return False 

153 

154 

155class MultipleFileField(FileField): 

156 """A :class:`FileField` that allows choosing multiple files.""" 

157 

158 widget = widgets.FileInput(multiple=True) 

159 

160 def process_formdata(self, valuelist): 

161 self.data = valuelist 

162 

163 

164class HiddenField(StringField): 

165 """ 

166 HiddenField is a convenience for a StringField with a 

167 :mdn-input:`hidden` widget. 

168 

169 It will render as an :mdn-input:`hidden` but otherwise coerce to a string. 

170 """ 

171 

172 widget = widgets.HiddenInput() 

173 

174 

175class SubmitField(BooleanField): 

176 """ 

177 Represents an :mdn-input:`submit`. 

178 

179 The field's label is also used as the rendered HTML ``value`` of the submit 

180 control. Its WTForms data is boolean, following :class:`BooleanField` 

181 semantics: the field is ``True`` when the submitted value is not a falsy 

182 value, and ``False`` otherwise. 

183 """ 

184 

185 widget = widgets.SubmitInput() 

186 

187 

188class SearchField(StringField): 

189 """ 

190 Represents an :mdn-input:`search`. 

191 """ 

192 

193 widget = widgets.SearchInput() 

194 

195 

196class TelField(StringField): 

197 """ 

198 Represents an :mdn-input:`tel`. 

199 """ 

200 

201 widget = widgets.TelInput() 

202 

203 

204class URLField(StringField): 

205 """ 

206 Represents an :mdn-input:`url`. 

207 """ 

208 

209 widget = widgets.URLInput() 

210 

211 

212class EmailField(StringField): 

213 """ 

214 Represents an :mdn-input:`email`. 

215 """ 

216 

217 widget = widgets.EmailInput() 

218 

219 

220class ColorField(StringField): 

221 """ 

222 Represents an :mdn-input:`color`. 

223 """ 

224 

225 widget = widgets.ColorInput()