-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields.py
326 lines (280 loc) · 16 KB
/
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
# -*- coding: utf-8 -*-
from decimal import Decimal
from future.moves.urllib.parse import ParseResult
from attr import attrib, NOTHING
from collections import OrderedDict
from uuid import uuid4, UUID
from datetime import date, datetime, time
from six import string_types
from . import _init_fields, types, converters, validators
def BooleanField(default=NOTHING, required=True, repr=True, cmp=True,
key=None, kw_only: bool = False):
"""
Create new bool field on a model.
:param default: any boolean value
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
:param bool kw_only: have a generated __init__ with keyword-only arguments,
relaxing the required ordering of default and non-default valued attributes.
"""
default = _init_fields.init_default(required, default, None)
validator = _init_fields.init_validator(required, bool)
return attrib(default=default, validator=validator, repr=repr, cmp=cmp,
metadata=dict(key=key), kw_only=kw_only)
def ChildField(cls, default=NOTHING, required=True, repr=True, cmp=True,
key=None, kw_only: bool = False):
"""
Create new child field on a model.
:param cls: class (or name) of the model to be related.
:param default: any object value of type cls
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
:param bool kw_only: have a generated __init__ with keyword-only arguments,
relaxing the required ordering of default and non-default valued attributes.
"""
default = _init_fields.init_default(required, default, None)
converter = converters.to_child_field(cls)
validator = _init_fields.init_validator(
required, object if isinstance(cls, str) else cls
)
return attrib(default=default, converter=converter, validator=validator,
repr=repr, cmp=cmp, metadata=dict(key=key), kw_only=kw_only)
def DateField(formatter=types.DEFAULT_DATE_FORMAT, default=NOTHING,
required=True, repr=True, cmp=True, key=None, kw_only: bool = False):
"""
Create new date field on a model.
:param formatter: date formatter string (default: "%Y-%m-%d")
:param default: any date or string that can be converted to a date value
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
:param bool kw_only: have a generated __init__ with keyword-only arguments,
relaxing the required ordering of default and non-default valued attributes.
"""
default = _init_fields.init_default(required, default, None)
validator = _init_fields.init_validator(required, date)
converter = converters.to_date_field(formatter)
return attrib(default=default, converter=converter, validator=validator,
repr=repr, cmp=cmp,
metadata=dict(formatter=formatter, key=key), kw_only=kw_only)
def DateTimeField(formatter=types.DEFAULT_DATETIME_FORMAT, default=NOTHING,
required=True, repr=True, cmp=True, key=None, kw_only: bool = False):
"""
Create new datetime field on a model.
:param formatter: datetime formatter string (default: "ISO_FORMAT")
:param default: any datetime or string that can be converted to a datetime
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
:param bool kw_only: have a generated __init__ with keyword-only arguments,
relaxing the required ordering of default and non-default valued attributes.
"""
default = _init_fields.init_default(required, default, None)
validator = _init_fields.init_validator(required, datetime)
converter = converters.to_datetime_field(formatter)
return attrib(default=default, converter=converter, validator=validator,
repr=repr, cmp=cmp,
metadata=dict(formatter=formatter, key=key), kw_only=kw_only)
def TimeField(formatter=types.DEFAULT_TIME_FORMAT, default=NOTHING,
required=True, repr=True, cmp=True, key=None, kw_only: bool = False):
"""
Create new time field on a model.
:param formatter: time formatter string (default: "%H:%M:%S")
:param default: any time or string that can be converted to a time value
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
:param bool kw_only: have a generated __init__ with keyword-only arguments,
relaxing the required ordering of default and non-default valued attributes.
"""
default = _init_fields.init_default(required, default, None)
validator = _init_fields.init_validator(required, time)
converter = converters.to_time_field(formatter)
return attrib(default=default, converter=converter, validator=validator,
repr=repr, cmp=cmp,
metadata=dict(formatter=formatter, key=key), kw_only=kw_only)
def FloatField(default=NOTHING, required=True, repr=True, cmp=True,
key=None, kw_only: bool = False):
"""
Create new float field on a model.
:param default: any float value
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
:param bool kw_only: have a generated __init__ with keyword-only arguments,
relaxing the required ordering of default and non-default valued attributes.
"""
default = _init_fields.init_default(required, default, None)
validator = _init_fields.init_validator(required, float)
return attrib(default=default, converter=converters.float_if_not_none,
validator=validator, repr=repr, cmp=cmp,
metadata=dict(key=key), kw_only=kw_only)
def IntegerField(default=NOTHING, required=True, repr=True, cmp=True,
key=None, kw_only: bool = False):
"""
Create new int field on a model.
:param default: any integer value
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
:param bool kw_only: have a generated __init__ with keyword-only arguments,
relaxing the required ordering of default and non-default valued attributes.
"""
default = _init_fields.init_default(required, default, None)
validator = _init_fields.init_validator(required, int)
return attrib(default=default, converter=converters.int_if_not_none,
validator=validator, repr=repr, cmp=cmp,
metadata=dict(key=key), kw_only=kw_only)
def MappingField(cls, child_key, default=NOTHING, required=True, repr=False,
key=None, kw_only: bool = False):
"""
Create new mapping field on a model.
:param cls: class (or name) of the model to be related in Sequence.
:param child_key: key field on the child object to be used as the map key.
:param default: any mapping type
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
:param bool kw_only: have a generated __init__ with keyword-only arguments,
relaxing the required ordering of default and non-default valued attributes.
"""
default = _init_fields.init_default(required, default, OrderedDict())
converter = converters.to_mapping_field(cls, child_key)
validator = _init_fields.init_validator(required, types.TypedMapping)
return attrib(default=default, converter=converter, validator=validator,
repr=repr, metadata=dict(key=key), kw_only=kw_only)
def RegexField(regex, default=NOTHING, required=True, repr=True, cmp=True,
key=None, kw_only: bool = False):
"""
Create new str field on a model.
:param regex: regex validation string (e.g. "[^@]+@[^@]+" for email)
:param default: any string value
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
:param bool kw_only: have a generated __init__ with keyword-only arguments,
relaxing the required ordering of default and non-default valued attributes.
"""
default = _init_fields.init_default(required, default, None)
validator = _init_fields.init_validator(required, string_types,
validators.regex(regex))
return attrib(default=default, converter=converters.str_if_not_none,
validator=validator, repr=repr, cmp=cmp,
metadata=dict(key=key), kw_only=kw_only)
def SequenceField(cls, default=NOTHING, required=True, repr=False, key=None,
kw_only: bool = False):
"""
Create new sequence field on a model.
:param cls: class (or name) of the model to be related in Sequence.
:param default: any TypedSequence or list
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
:param bool kw_only: have a generated __init__ with keyword-only arguments,
relaxing the required ordering of default and non-default valued attributes.
"""
default = _init_fields.init_default(required, default, [])
converter = converters.to_sequence_field(cls)
validator = _init_fields.init_validator(required, types.TypedSequence)
return attrib(default=default, converter=converter, validator=validator,
repr=repr, metadata=dict(key=key), kw_only=kw_only)
def SetField(cls, default=NOTHING, required=True, repr=False, key=None,
kw_only: bool = False):
"""
Create new set field on a model.
:param cls: class (or name) of the model to be related in Set.
:param default: any TypedSet or set
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
:param bool kw_only: have a generated __init__ with keyword-only arguments,
relaxing the required ordering of default and non-default valued attributes.
"""
default = _init_fields.init_default(required, default, set())
converter = converters.to_set_field(cls)
validator = _init_fields.init_validator(required, types.TypedSet)
return attrib(default=default, converter=converter, validator=validator,
repr=repr, metadata=dict(key=key), kw_only=kw_only)
def StringField(default=NOTHING, required=True, repr=True, cmp=True,
key=None, kw_only: bool = False):
"""
Create new str field on a model.
:param default: any string value
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
:param bool kw_only: have a generated __init__ with keyword-only arguments,
relaxing the required ordering of default and non-default valued attributes.
"""
default = _init_fields.init_default(required, default, None)
validator = _init_fields.init_validator(required, string_types)
return attrib(default=default, converter=converters.str_if_not_none,
validator=validator, repr=repr, cmp=cmp,
metadata=dict(key=key), kw_only=kw_only)
def URLField(default=NOTHING, required=True, repr=True, cmp=True, key=None,
kw_only: bool = False):
"""
Create new UUID field on a model.
:param default: any value
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
:param bool kw_only: have a generated __init__ with keyword-only arguments,
relaxing the required ordering of default and non-default valued attributes.
"""
cls = ParseResult
default = _init_fields.init_default(required, default, None)
validator = _init_fields.init_validator(required, cls)
return attrib(default=default, converter=converters.str_to_url,
validator=validator, repr=repr, cmp=cmp,
metadata=dict(key=key), kw_only=kw_only)
def UUIDField(default=NOTHING, required=False, repr=True, cmp=True, key=None,
kw_only: bool = False):
"""
Create new UUID field on a model.
:param default: any value
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
:param bool kw_only: have a generated __init__ with keyword-only arguments,
relaxing the required ordering of default and non-default valued attributes.
"""
cls = UUID
default = _init_fields.init_default(required, default, uuid4)
validator = _init_fields.init_validator(required, cls)
return attrib(default=default, converter=converters.str_to_uuid,
validator=validator, repr=repr, cmp=cmp,
metadata=dict(key=key), kw_only=kw_only)
def DecimalField(default=NOTHING, required=True, repr=True, cmp=True,
key=None, kw_only: bool = False):
"""
Create new decimal field on a model.
:param default: any decimal value
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
:param bool kw_only: have a generated __init__ with keyword-only arguments,
relaxing the required ordering of default and non-default valued attributes.
"""
default = _init_fields.init_default(required, default, None)
validator = _init_fields.init_validator(required, Decimal)
return attrib(default=default, converter=lambda x: Decimal(x),
validator=validator, repr=repr, cmp=cmp,
metadata=dict(key=key), kw_only=kw_only)