-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathdb_fields.py
505 lines (331 loc) · 12.6 KB
/
db_fields.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
"""Responsible for mongoengine fields extension, if WTFForms integration used."""
__all__ = [
"WtfFieldMixin",
"BinaryField",
"BooleanField",
"CachedReferenceField",
"ComplexDateTimeField",
"DateField",
"DateTimeField",
"DecimalField",
"DictField",
"DynamicField",
"EmailField",
"EmbeddedDocumentField",
"EmbeddedDocumentListField",
"EnumField",
"FileField",
"FloatField",
"GenericEmbeddedDocumentField",
"GenericLazyReferenceField",
"GenericReferenceField",
"GeoJsonBaseField",
"GeoPointField",
"ImageField",
"IntField",
"LazyReferenceField",
"LineStringField",
"ListField",
"LongField",
"MapField",
"MultiLineStringField",
"MultiPointField",
"MultiPolygonField",
"ObjectIdField",
"PointField",
"PolygonField",
"ReferenceField",
"SequenceField",
"SortedListField",
"StringField",
"URLField",
"UUIDField",
]
import functools
import logging
from typing import Callable, List, Optional, Union
from mongoengine import fields
try:
from wtforms import fields as wtf_fields
from wtforms import validators as wtf_validators
except ImportError:
wtf_fields = None
wtf_validators = None
logger = logging.getLogger("flask_mongoengine")
def wtf_required(func):
"""Special decorator to warn user on incorrect installation."""
@functools.wraps(func)
def wrapped(*args, **kwargs):
if wtf_validators is None or wtf_fields is None:
logger.error(f"WTForms not installed. Function '{func.__name__}' aborted.")
return None
return func(*args, **kwargs)
return wrapped
class WtfFieldMixin:
"""
Extension wrapper class for mongoengine BaseField.
This enables flask-mongoengine wtf to extend the
number of field parameters, and settings on behalf
of document model form generator for WTForm.
:param validators: wtf model form field validators.
:param filters: wtf model form field filters.
:param kwargs: keyword arguments silently bypassed to normal mongoengine fields
"""
def __init__(
self,
*,
validators: Optional[Union[List, Callable]] = None,
filters: Optional[Union[List, Callable]] = None,
**kwargs,
):
self.validators = self._ensure_callable_or_list(validators, "validators")
self.filters = self._ensure_callable_or_list(filters, "filters")
super().__init__(**kwargs)
@staticmethod
def _ensure_callable_or_list(argument, msg_flag: str) -> Optional[List]:
"""
Ensure submitted argument value is a callable object or valid list value.
:param argument: Argument input to make verification on.
:param msg_flag: Argument string name for error message.
"""
if argument is None:
return None
if callable(argument):
return [argument]
elif not isinstance(argument, list):
raise TypeError(f"Argument '{msg_flag}' must be a list value")
return argument
class BinaryField(WtfFieldMixin, fields.BinaryField):
"""
Extends :class:`mongoengine.fields.BinaryField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class BooleanField(WtfFieldMixin, fields.BooleanField):
"""
Extends :class:`mongoengine.fields.BooleanField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class CachedReferenceField(WtfFieldMixin, fields.CachedReferenceField):
"""
Extends :class:`mongoengine.fields.CachedReferenceField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class ComplexDateTimeField(WtfFieldMixin, fields.ComplexDateTimeField):
"""
Extends :class:`mongoengine.fields.ComplexDateTimeField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class DateField(WtfFieldMixin, fields.DateField):
"""
Extends :class:`mongoengine.fields.DateField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class DateTimeField(WtfFieldMixin, fields.DateTimeField):
"""
Extends :class:`mongoengine.fields.DateTimeField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class DecimalField(WtfFieldMixin, fields.DecimalField):
"""
Extends :class:`mongoengine.fields.DecimalField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class DictField(WtfFieldMixin, fields.DictField):
"""
Extends :class:`mongoengine.fields.DictField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class DynamicField(WtfFieldMixin, fields.DynamicField):
"""
Extends :class:`mongoengine.fields.DynamicField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class EmailField(WtfFieldMixin, fields.EmailField):
"""
Extends :class:`mongoengine.fields.EmailField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class EmbeddedDocumentField(WtfFieldMixin, fields.EmbeddedDocumentField):
"""
Extends :class:`mongoengine.fields.EmbeddedDocumentField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class EmbeddedDocumentListField(WtfFieldMixin, fields.EmbeddedDocumentListField):
"""
Extends :class:`mongoengine.fields.EmbeddedDocumentListField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class EnumField(WtfFieldMixin, fields.EnumField):
"""
Extends :class:`mongoengine.fields.EnumField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class FileField(WtfFieldMixin, fields.FileField):
"""
Extends :class:`mongoengine.fields.FileField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class FloatField(WtfFieldMixin, fields.FloatField):
"""
Extends :class:`mongoengine.fields.FloatField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class GenericEmbeddedDocumentField(WtfFieldMixin, fields.GenericEmbeddedDocumentField):
"""
Extends :class:`mongoengine.fields.GenericEmbeddedDocumentField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class GenericLazyReferenceField(WtfFieldMixin, fields.GenericLazyReferenceField):
"""
Extends :class:`mongoengine.fields.GenericLazyReferenceField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class GenericReferenceField(WtfFieldMixin, fields.GenericReferenceField):
"""
Extends :class:`mongoengine.fields.GenericReferenceField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class GeoJsonBaseField(WtfFieldMixin, fields.GeoJsonBaseField):
"""
Extends :class:`mongoengine.fields.GeoJsonBaseField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class GeoPointField(WtfFieldMixin, fields.GeoPointField):
"""
Extends :class:`mongoengine.fields.GeoPointField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class ImageField(WtfFieldMixin, fields.ImageField):
"""
Extends :class:`mongoengine.fields.ImageField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class IntField(WtfFieldMixin, fields.IntField):
"""
Extends :class:`mongoengine.fields.IntField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class LazyReferenceField(WtfFieldMixin, fields.LazyReferenceField):
"""
Extends :class:`mongoengine.fields.LazyReferenceField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class LineStringField(WtfFieldMixin, fields.LineStringField):
"""
Extends :class:`mongoengine.fields.LineStringField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class ListField(WtfFieldMixin, fields.ListField):
"""
Extends :class:`mongoengine.fields.ListField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class LongField(WtfFieldMixin, fields.LongField):
"""
Extends :class:`mongoengine.fields.LongField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class MapField(WtfFieldMixin, fields.MapField):
"""
Extends :class:`mongoengine.fields.MapField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class MultiLineStringField(WtfFieldMixin, fields.MultiLineStringField):
"""
Extends :class:`mongoengine.fields.MultiLineStringField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class MultiPointField(WtfFieldMixin, fields.MultiPointField):
"""
Extends :class:`mongoengine.fields.MultiPointField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class MultiPolygonField(WtfFieldMixin, fields.MultiPolygonField):
"""
Extends :class:`mongoengine.fields.MultiPolygonField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class ObjectIdField(WtfFieldMixin, fields.ObjectIdField):
"""
Extends :class:`mongoengine.fields.ObjectIdField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class PointField(WtfFieldMixin, fields.PointField):
"""
Extends :class:`mongoengine.fields.PointField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class PolygonField(WtfFieldMixin, fields.PolygonField):
"""
Extends :class:`mongoengine.fields.PolygonField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class ReferenceField(WtfFieldMixin, fields.ReferenceField):
"""
Extends :class:`mongoengine.fields.ReferenceField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class SequenceField(WtfFieldMixin, fields.SequenceField):
"""
Extends :class:`mongoengine.fields.SequenceField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class SortedListField(WtfFieldMixin, fields.SortedListField):
"""
Extends :class:`mongoengine.fields.SortedListField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class StringField(WtfFieldMixin, fields.StringField):
"""
Extends :class:`mongoengine.fields.StringField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class URLField(WtfFieldMixin, fields.URLField):
"""
Extends :class:`mongoengine.fields.URLField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass
class UUIDField(WtfFieldMixin, fields.UUIDField):
"""
Extends :class:`mongoengine.fields.UUIDField` with wtf required parameters.
For full list of arguments and keyword arguments, look parent field docs.
"""
pass