Skip to content

Commit 2658050

Browse files
committed
fields.py: Covering with docstrings
1 parent d1d681e commit 2658050

File tree

1 file changed

+74
-6
lines changed

1 file changed

+74
-6
lines changed

flask_mongoengine/wtf/fields.py

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ def __init__(
5454
self.queryset = queryset
5555

5656
def iter_choices(self):
57+
"""
58+
Provides data for choice widget rendering. Must return a sequence or
59+
iterable of (value, label, selected) tuples.
60+
"""
5761
if self.allow_blank:
5862
yield "__None", self.blank_text, self.data is None
5963

@@ -75,6 +79,14 @@ def iter_choices(self):
7579
yield obj.id, label, selected
7680

7781
def process_formdata(self, valuelist):
82+
"""
83+
Process data received over the wire from a form.
84+
85+
This will be called during form construction with data supplied
86+
through the `formdata` argument.
87+
88+
:param valuelist: A list of strings to process.
89+
"""
7890
if not valuelist or valuelist[0] == "__None" or self.queryset is None:
7991
self.data = None
8092
return
@@ -86,6 +98,11 @@ def process_formdata(self, valuelist):
8698
self.data = None
8799

88100
def pre_validate(self, form):
101+
"""
102+
Field-level validation. Runs before any other validators.
103+
104+
:param form: The form the field belongs to.
105+
"""
89106
if (not self.allow_blank or self.data is not None) and not self.data:
90107
raise wtf_validators.ValidationError(_("Not a valid choice"))
91108

@@ -95,6 +112,8 @@ def _is_selected(self, item):
95112

96113
# noinspection PyAttributeOutsideInit,PyAbstractClass
97114
class QuerySetSelectMultipleField(QuerySetSelectField):
115+
"""Same as :class:`QuerySetSelectField` but with multiselect options."""
116+
98117
widget = wtf_widgets.Select(multiple=True)
99118

100119
def __init__(
@@ -113,6 +132,14 @@ def __init__(
113132
)
114133

115134
def process_formdata(self, valuelist):
135+
"""
136+
Process data received over the wire from a form.
137+
138+
This will be called during form construction with data supplied
139+
through the `formdata` argument.
140+
141+
:param valuelist: A list of strings to process.
142+
"""
116143

117144
if not valuelist or valuelist[0] == "__None" or not self.queryset:
118145
self.data = None
@@ -156,25 +183,50 @@ def __init__(self, label="", validators=None, model=None, **kwargs):
156183

157184
# noinspection PyAttributeOutsideInit,PyAbstractClass
158185
class JSONField(wtf_fields.TextAreaField):
186+
"""Special version fo :class:`wtforms.fields.TextAreaField`."""
187+
159188
def _value(self):
160189
# TODO: Investigate why raw mentioned.
161190
if self.raw_data:
162191
return self.raw_data[0]
163192
else:
164193
return self.data and json.dumps(self.data) or ""
165194

166-
def process_formdata(self, value):
167-
if value:
195+
def process_formdata(self, valuelist):
196+
"""
197+
Process data received over the wire from a form.
198+
199+
This will be called during form construction with data supplied
200+
through the `formdata` argument.
201+
202+
:param valuelist: A list of strings to process.
203+
"""
204+
if valuelist:
168205
try:
169-
self.data = json.loads(value[0])
206+
self.data = json.loads(valuelist[0])
170207
except ValueError as error:
171208
raise ValueError(self.gettext("Invalid JSON data.")) from error
172209

173210

174211
class DictField(JSONField):
175-
def process_formdata(self, value):
176-
super(DictField, self).process_formdata(value)
177-
if value and not isinstance(self.data, dict):
212+
"""
213+
Special version fo :class:`JSONField` to be generated for
214+
:class:`flask_mongoengine.db_fields.DictField`.
215+
216+
Used in generator before flask_mongoengine version 2.0
217+
"""
218+
219+
def process_formdata(self, valuelist):
220+
"""
221+
Process data received over the wire from a form.
222+
223+
This will be called during form construction with data supplied
224+
through the `formdata` argument.
225+
226+
:param valuelist: A list of strings to process.
227+
"""
228+
super(DictField, self).process_formdata(valuelist)
229+
if valuelist and not isinstance(self.data, dict):
178230
raise ValueError(self.gettext("Not a valid dictionary."))
179231

180232

@@ -185,6 +237,14 @@ class NoneStringField(wtf_fields.StringField):
185237
"""
186238

187239
def process_formdata(self, valuelist):
240+
"""
241+
Process data received over the wire from a form.
242+
243+
This will be called during form construction with data supplied
244+
through the `formdata` argument.
245+
246+
:param valuelist: A list of strings to process.
247+
"""
188248
if valuelist:
189249
self.data = valuelist[0]
190250
if self.data == "":
@@ -198,5 +258,13 @@ class BinaryField(wtf_fields.TextAreaField):
198258
"""
199259

200260
def process_formdata(self, valuelist):
261+
"""
262+
Process data received over the wire from a form.
263+
264+
This will be called during form construction with data supplied
265+
through the `formdata` argument.
266+
267+
:param valuelist: A list of strings to process.
268+
"""
201269
if valuelist:
202270
self.data = bytes(valuelist[0], "utf-8")

0 commit comments

Comments
 (0)