forked from kolypto/py-mongosql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud.py
387 lines (304 loc) · 14.2 KB
/
crud.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
from sqlalchemy.orm.attributes import flag_modified
from . import MongoModel, MongoQuery
from .hist import ModelHistoryProxy
class CrudHelper(object):
""" Crud helper functions """
def __init__(self, model):
""" Init CRUD helper
:param model: The model to work with
:type model: type
"""
self.model = model
self.mongomodel = MongoModel.get_for(self.model)
def mquery(self, query, query_obj=None):
""" Construct a MongoQuery for the model.
If `query` is provided, it's used for initial filtering
:param query: Query to start with
:type query: sqlalchemy.orm.Query
:param query_obj: Apply initial filtering with the Query Object
:type query_obj: dict|None
:rtype: mongosql.MongoQuery
:raises AssertionError: unknown operations specified in query_obj
"""
assert query_obj is None or isinstance(query_obj, dict), 'Query Object should be a dict or None'
mq = MongoQuery(self.mongomodel, query)
if query_obj:
mq = mq.query(**query_obj)
return mq
def check_columns(self, names):
""" Test if all column names are known
:param names: Column names
:type names: Iterable
:return: Set of unknown names
:rtype: set
"""
model_colnames = self.mongomodel.model_bag.columns.names
names = set(names)
return names - model_colnames
def nullify_empty_fields(self, entity):
""" Walk through the entity dict and handle nullable fields:
- If a field has a value of '', set it to None
:param entity: Entity
:type entity:
:return: Altered entity
:rtype: dict
"""
for k in self.mongomodel.model_bag.nullable.keys():
if k in entity and entity[k] == '':
entity[k] = None
return entity
def create_model(self, entity):
""" Create an instance from entity dict.
This only allows to assign column properties and not relations.
:param entity: Entity dict
:type entity: dict
:return: Created instance
:rtype: sqlalchemy.ext.declarative.DeclarativeMeta
:raises AssertionError: validation errors
"""
assert isinstance(entity, dict), 'Create model: entity should be a dict'
# Check columns
unk_cols = self.check_columns(entity.keys())
assert not unk_cols, 'Create model: unknown fields: {}'.format(unk_cols)
# Create
return self.model(**entity)
def update_model(self, entity, instance):
""" Update an instance from entity dict by merging the fields
- Properties are copied over
- JSON dicts are shallowly merged
:param entity: Entity dict
:type entity: dict
:param instance: The instance to update
:type instance: sqlalchemy.ext.declarative.DeclarativeMeta
:return: New instance, updated
:rtype: sqlalchemy.ext.declarative.DeclarativeMeta
:raises AssertionError: validation errors
"""
assert isinstance(entity, dict), 'Update model: entity should be a dict'
# Check columns
unk_cols = self.check_columns(entity.keys())
assert not unk_cols, 'Update model: unknown fields: {}'.format(unk_cols)
# Update
for name, val in entity.items():
if isinstance(val, dict) and self.mongomodel.model_bag.columns.is_column_json(name):
# JSON column with a dict: do a shallow merge
getattr(instance, name).update(val)
# Tell SqlAlchemy that a mutable collection was updated
flag_modified(instance, name)
else:
# Other columns: just assign
setattr(instance, name, val)
# Finish
return instance
class StrictCrudHelper(CrudHelper):
""" Crud helper with limitations
- Read-only fields can not be set
- Only allowed relationships can be loaded
- Default Query Object is used
- Limits the maximum number of items that can be retrieved when listing
"""
def __init__(self, model, ro_fields=(), allow_relations=(), query_defaults=None, maxitems=None):
""" Init Strict CRUD helper
:param model: The model to work with
:type model: sqlalchemy.ext.declarative.DeclarativeMeta
:param ro_fields: List of read-only properties.
Also can be a callable which decides on the read-only properties at runtime.
:type ro_fields: Iterable[str|sqlalchemy.Column|sqlalchemy.orm.properties.ColumnProperty]|Callable
:param allow_relations: List of relations allowed to join to.
Specify relation names or relationship properties.
To allow joining to second-level relations, use dot-notation.
:type allow_relations: Iterable[str|sqlalchemy.orm.relationships.RelationshipProperty]
:param query_defaults: Default Query Object used when nothing is specified
:type query_defaults: dict|None
:param maxitems: Hard limit on the number of entities that can be loaded (max value for QueryObject['limit'])
:type maxitems: int|None
"""
super(StrictCrudHelper, self).__init__(model)
self._ro_fields = ro_fields if callable(ro_fields) else set(c if isinstance(c, basestring) else c.key for c in ro_fields)
self._allowed_relations = set(c if isinstance(c, basestring) else c.key for c in allow_relations)
self._query_defaults = query_defaults or {}
self._maxitems = maxitems or None
assert callable(self._ro_fields) or all(isinstance(x, basestring) for x in self._ro_fields), 'Some values in `ro_fields` were not converted to string'
assert all(isinstance(x, str) for x in self._allowed_relations), 'Some values in `allowed_relations` were not converted to string'
assert isinstance(self._query_defaults, dict), '`query_defaults` was not a dict'
assert self._maxitems is None or isinstance(self._maxitems, int), '`maxitems` must be an integer'
@property
def ro_fields(self):
""" Get the set of read-only property names
:rtype: set[str]
"""
return set(self._ro_fields()) if callable(self._ro_fields) else self._ro_fields
@property
def allowed_relations(self):
""" Get the set of relations that are allowed to join to
:rtype: set[str]
"""
return set(self._allowed_relations)
@classmethod
def _check_relations(cls, allowed_relations, qo, _prefix=''):
""" Test Query Object joins against `allowed_relations`, supporting dot-notation
:param allowed_relations: Set of allowed relations
:type allowed_relations: set
:param qo: Query Object
:type qo: dict | None
:returns: Banned relationships
:rtype: set[str]
"""
if not isinstance(qo, dict) or 'join' not in qo:
return set()
joinspec = qo['join']
relnames = {_prefix + name for name in joinspec}
disallowed_relations = relnames - allowed_relations
# Deeper
if isinstance(joinspec, dict):
for relname, qo in joinspec.items():
disallowed_relations |= cls._check_relations(allowed_relations, qo, _prefix=relname + '.')
# Finish
return disallowed_relations
def mquery(self, query, query_obj=None):
assert query_obj is None or isinstance(query_obj, dict), 'Query Object should be a dict or None'
# Query defaults
if self._query_defaults:
query_obj = dict(self._query_defaults.items() + (query_obj.items() if query_obj else []))
# Max items
if self._maxitems:
query_obj = query_obj or {}
if not (query_obj.get('count', 0) or query_obj.get('aggregate', 0)): # no limits in count() and aggregate() modes
query_obj['limit'] = min(self._maxitems, query_obj.get('limit', self._maxitems))
# Allowed relations
disallowed_relations = self._check_relations(self._allowed_relations, query_obj)
assert not disallowed_relations, 'Joining to these relations is not allowed: {}'.format(disallowed_relations)
# Finish
return super(StrictCrudHelper, self).mquery(query, query_obj)
def create_model(self, entity):
assert isinstance(entity, dict), 'Create model: entity should be a dict'
# Remove ro fields
for k in set(entity.keys()) & self.ro_fields:
entity.pop(k)
# Super
return super(StrictCrudHelper, self).create_model(entity)
def update_model(self, entity, instance):
assert isinstance(entity, dict), 'Update model: entity should be a dict'
# Remove ro fields
for k in set(entity.keys()) & self.ro_fields:
entity.pop(k)
# Super
return super(StrictCrudHelper, self).update_model(entity, instance)
class CrudViewMixin(object):
""" Base class for CRUD implementations """
#: Set the CRUD helper object
crudhelper = None
@classmethod
def _getCrudHelper(cls):
""" Get the CRUD helper assigned for this class
:rtype: mongosql.CrudHelper
"""
assert isinstance(cls.crudhelper, CrudHelper), '{}: {} should be set to an instance of {}'.format(cls, 'crudhelper', CrudHelper)
return cls.crudhelper
def _query(self):
""" Get a Query object to be used for queries
:rtype: sqlalchemy.orm.Query
"""
raise NotImplementedError('query() method not defined on {}'.format(type(self)))
def _mquery(self, query_obj=None, *filter, **filter_by):
""" Get a MongoQuery with initial filtering applied
:param query_obj: Query Object
:type query_obj: dict|None
:param filter: Additional filter() criteria
:param filter_by: Additional filter_by() criteria
:rtype: mongosql.MongoQuery
"""
return self._getCrudHelper().mquery(
self._query().filter(*filter).filter_by(**filter_by),
query_obj
)
def _get_one(self, query_obj, *filter, **filter_by):
""" Utility method that fetches a single entity.
You will probably want to override it with custom error handling
:param query_obj: Query Object
:param filter: Additional filter() criteria
:param filter_by: Additional filter_by() criteria
:rtype: sqlalchemy.ext.declarative.DeclarativeMeta
:raises sqlalchemy.orm.exc.NoResultFound: Nothing found
:raises sqlalchemy.orm.exc.MultipleResultsFound: Multiple found
:raises AssertionError: validation errors
"""
return self._mquery(query_obj, *filter, **filter_by).end().one()
def _save_hook(self, new, prev=None):
""" Hook into create(), update() methods.
This allows to make some changes to the instance before it's actually saved.
:param new: New version
:type new: sqlalchemy.ext.declarative.DeclarativeMeta
:param prev: Previously persisted version (only when updating).
:type prev: mongosql.hist.ModelHistoryProxy
"""
pass
def _method_list(self, query_obj=None, *filter, **filter_by):
""" Fetch the list of entitites
:param query_obj: Query Object
:param filter: Additional filter() criteria
:param filter_by: Additional filter_by() criteria
:rtype: list
:raises AssertionError: validation errors
"""
res = self._mquery(query_obj, *filter, **filter_by).end().all()
# Count?
if query_obj and query_obj.get('count', 0):
return res[0][0] # Scalar count query
# Convert KeyedTuples to dicts (when aggregating)
if query_obj and 'aggregate' in query_obj:
return [dict(zip(row.keys(), row)) for row in res]
return res
def _method_create(self, entity):
""" Create a new entity
:param entity: Entity dict
:type entity: dict
:return: The created instance (to be saved)
:rtype: sqlalchemy.ext.declarative.DeclarativeMeta
:raises AssertionError: validation errors
"""
instance = self._getCrudHelper().create_model(entity)
self._save_hook(instance)
return instance
def _method_get(self, query_obj=None, *filter, **filter_by):
""" Fetch a single entity
:param query_obj: Query Object
:param filter: Additional filter() criteria
:param filter_by: Additional filter_by() criteria
:rtype: sqlalchemy.ext.declarative.DeclarativeMeta
:raises sqlalchemy.orm.exc.NoResultFound: Nothing found
:raises sqlalchemy.orm.exc.MultipleResultsFound: Multiple found
:raises AssertionError: validation errors
"""
return self._get_one(query_obj, *filter, **filter_by)
def _method_update(self, entity, *filter, **filter_by):
""" Update an existing entity by merging the fields
:param entity: Entity dict
:type entity: dict
:param filter: Criteria to find the previous entity
:param filter_by: Criteria to find the previous entity
:return: The updated instance (to be saved)
:rtype: sqlalchemy.ext.declarative.DeclarativeMeta
:raises sqlalchemy.orm.exc.NoResultFound: Nothing found
:raises sqlalchemy.orm.exc.MultipleResultsFound: Multiple found
:raises AssertionError: validation errors
"""
instance = self._get_one(None, *filter, **filter_by)
instance = self._getCrudHelper().update_model(entity, instance)
self._save_hook(
instance,
ModelHistoryProxy(instance)
)
return instance
def _method_delete(self, *filter, **filter_by):
""" Delete an existing entity
Loads the entity prior to deletion.
:param filter: Criteria to find the previous entity
:param filter_by: Criteria to find the previous entity
:return: The instance to be deleted
:rtype: sqlalchemy.ext.declarative.DeclarativeMeta
:raises sqlalchemy.orm.exc.NoResultFound: Nothing found
:raises sqlalchemy.orm.exc.MultipleResultsFound: Multiple found
:raises AssertionError: validation errors
"""
return self._get_one(None, *filter, **filter_by)