-
Notifications
You must be signed in to change notification settings - Fork 62
/
abstract.py
284 lines (234 loc) · 10.4 KB
/
abstract.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
from marshmallow import (Schema as MaSchema, fields as ma_fields,
validate as ma_validate, missing, EXCLUDE)
from .i18n import gettext as _, N_
from .marshmallow_bonus import schema_from_umongo_get_attribute
__all__ = ('BaseSchema', 'BaseField', 'BaseValidator', 'BaseDataObject')
class I18nErrorDict(dict):
def __getitem__(self, name):
raw_msg = dict.__getitem__(self, name)
return _(raw_msg)
class BaseSchema(MaSchema):
"""
All schema used in umongo should inherit from this base schema
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.error_messages = I18nErrorDict(self.error_messages)
_marshmallow_schemas_cache = {}
def map_to_field(self, func):
"""
Apply a function to every field in the schema
>>> def func(mongo_path, path, field):
... pass
"""
for name, field in self.fields.items():
mongo_path = field.attribute or name
func(mongo_path, name, field)
if hasattr(field, 'map_to_field'):
field.map_to_field(mongo_path, name, func)
def as_marshmallow_schema(self, params=None, base_schema_cls=MaSchema,
check_unknown_fields=True, mongo_world=False, meta=None):
"""
Return a pure-marshmallow version of this schema class.
:param params: Per-field dict to pass parameters to their field creation.
:param base_schema_cls: Class the schema will inherit from (
default: :class:`marshmallow.Schema`).
:param check_unknown_fields: Unknown fields are considered as errors (default: True).
:param mongo_world: If True the schema will work against the mongo world
instead of the OO world (default: False).
:param meta: Optional dict with attributes for the schema's Meta class.
"""
params = params or {}
meta = meta or {}
# Use hashable parameters as cache dict key and dict parameters for manual comparison
cache_key = (self.__class__, base_schema_cls, check_unknown_fields, mongo_world)
cache_modifiers = (params, meta)
if cache_key in self._marshmallow_schemas_cache:
for modifiers, ma_schema in self._marshmallow_schemas_cache[cache_key]:
if modifiers == cache_modifiers:
return ma_schema
nmspc = {
name: field.as_marshmallow_field(
params=params.get(name),
base_schema_cls=base_schema_cls,
check_unknown_fields=check_unknown_fields,
mongo_world=mongo_world)
for name, field in self.fields.items()
}
name = 'Marshmallow%s' % type(self).__name__
if not check_unknown_fields:
meta.setdefault('unknown', EXCLUDE)
# By default OO world returns `missing` fields as `None`,
# disable this behavior here to let marshmallow deal with it
if not mongo_world:
nmspc['get_attribute'] = schema_from_umongo_get_attribute
if meta:
nmspc['Meta'] = type('Meta', (base_schema_cls.Meta,), meta)
m_schema = type(name, (base_schema_cls, ), nmspc)
# Add i18n support to the schema
# We can't use I18nErrorDict here because __getitem__ is not called
# when error_messages is updated with _default_error_messages.
m_schema._default_error_messages = {
k: _(v) for k, v in m_schema._default_error_messages.items()}
self._marshmallow_schemas_cache.setdefault(cache_key, []).append(
(cache_modifiers, m_schema))
return m_schema
class BaseField(ma_fields.Field):
"""
All fields used in umongo should inherit from this base field.
============================== ===============
Enabled flags resulting index
============================== ===============
<no flags>
allow_none
required
required, allow_none
required, unique, allow_none unique
unique unique, sparse
unique, required unique
unique, allow_none unique, sparse
============================== ===============
.. note:: Even with allow_none flag, the unique flag will refuse duplicated
`null` value (consider unsetting the field with `del` instead)
"""
default_error_messages = {
'unique': N_('Field value must be unique.'),
'unique_compound': N_('Values of fields {fields} must be unique together.')
}
MARSHMALLOW_ARGS_PREFIX = 'marshmallow_'
def __init__(self, *args, io_validate=None, unique=False, instance=None, **kwargs):
if 'missing' in kwargs:
raise RuntimeError("uMongo doesn't use `missing` argument, use `default` "
"instead and `marshmallow_missing`/`marshmallow_default` "
"to tell `as_marshmallow_field` to use a custom value when "
"generating pure Marshmallow field.")
if 'default' in kwargs:
kwargs['missing'] = kwargs['default']
# Store attributes prefixed with marshmallow_ to use them when
# creating pure marshmallow Schema
self._ma_kwargs = {
key[len(self.MARSHMALLOW_ARGS_PREFIX):]: val
for key, val in kwargs.items()
if key.startswith(self.MARSHMALLOW_ARGS_PREFIX)
}
kwargs = {
key: val
for key, val in kwargs.items()
if not key.startswith(self.MARSHMALLOW_ARGS_PREFIX)
}
super().__init__(*args, **kwargs)
self._ma_kwargs.setdefault('missing', self.default)
self._ma_kwargs.setdefault('default', self.default)
# Overwrite error_messages to handle i18n translation
self.error_messages = I18nErrorDict(self.error_messages)
# `io_validate` will be run after `io_validate_resursive`
# only if this one doesn't returns errors. This is useful for
# list and embedded fields.
self.io_validate = io_validate
self.io_validate_recursive = None
self.unique = unique
self.instance = instance
def __repr__(self):
return ('<fields.{ClassName}(default={self.default!r}, '
'attribute={self.attribute!r}, '
'validate={self.validate}, required={self.required}, '
'load_only={self.load_only}, dump_only={self.dump_only}, '
'marshmallow_missing={self.marshmallow_missing}, '
'marshmallow_default={self.marshmallow_default}, '
'allow_none={self.allow_none}, '
'error_messages={self.error_messages}, '
'io_validate={self.io_validate}, '
'io_validate_recursive={self.io_validate_recursive}, '
'unique={self.unique}, '
'instance={self.instance})>'
.format(ClassName=self.__class__.__name__, self=self))
def _validate_missing(self, value):
# Overwrite marshmallow.Field._validate_missing given it also checks
# for missing required fields (this is done at commit time in umongo
# using `DataProxy.required_validate`).
if value is None and getattr(self, 'allow_none', False) is False:
self.fail('null')
def serialize_to_mongo(self, obj):
if obj is None and getattr(self, 'allow_none', False) is True:
return None
if obj is missing:
return missing
return self._serialize_to_mongo(obj)
# def serialize_to_mongo_update(self, path, obj):
# return self._serialize_to_mongo(attr, obj=obj, update=update)
def deserialize_from_mongo(self, value):
if value is None and getattr(self, 'allow_none', False) is True:
return None
return self._deserialize_from_mongo(value)
def _serialize_to_mongo(self, obj):
return obj
def _deserialize_from_mongo(self, value):
return value
def translate_query(self, key, query):
return {self.attribute or key: query}
def _extract_marshmallow_field_params(self, mongo_world):
params = {
attribute: getattr(self, attribute)
for attribute in (
'validate', 'required', 'allow_none',
'load_only', 'dump_only', 'error_messages'
)
}
if mongo_world and self.attribute:
params['attribute'] = self.attribute
# Override uMongo attributes with marshmallow_ prefixed attributes
params.update(self._ma_kwargs)
params.update(self.metadata)
return params
def as_marshmallow_field(self, params=None, mongo_world=False, **kwargs):
"""
Return a pure-marshmallow version of this field.
:param params: Additional parameters passed to the marshmallow field
class constructor.
:param mongo_world: If True the field will work against the mongo world
instead of the OO world (default: False)
"""
field_kwargs = self._extract_marshmallow_field_params(mongo_world)
if params:
field_kwargs.update(params)
# Retrieve the marshmallow class we inherit from
for m_class in type(self).mro():
if (not issubclass(m_class, BaseField) and
issubclass(m_class, ma_fields.Field)):
m_field = m_class(**field_kwargs)
# Add i18n support to the field
m_field.error_messages = I18nErrorDict(m_field.error_messages)
return m_field
# Cannot escape the loop given BaseField itself inherits marshmallow's Field
class BaseValidator(ma_validate.Validator):
"""
All validators in umongo should inherit from this base validator.
"""
def __init__(self, *args, **kwargs):
self._error = None
super().__init__(*args, **kwargs)
@property
def error(self):
return _(self._error)
@error.setter
def error(self, value):
self._error = value
class BaseDataObject:
"""
All data objects in umongo should inherit from this base data object.
"""
def is_modified(self):
raise NotImplementedError()
def clear_modified(self):
raise NotImplementedError()
@classmethod
def build_from_mongo(cls, data):
doc = cls()
doc.from_mongo(data)
return doc
def from_mongo(self, data):
return self(data)
def to_mongo(self, update=False):
return self
def dump(self):
return self