-
Notifications
You must be signed in to change notification settings - Fork 16
/
alchemy.py
888 lines (695 loc) · 35.2 KB
/
alchemy.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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
"""This module is a CRUD interface between resource managers and the sqlalchemy ORM"""
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.orm.collections import InstrumentedList
from sqlalchemy.inspection import inspect
from sqlalchemy.orm.attributes import QueryableAttribute
from sqlalchemy.orm import joinedload, ColumnProperty, RelationshipProperty
from marshmallow import class_registry
from marshmallow.base import SchemaABC
from flask import current_app
from flask_combo_jsonapi.data_layers.base import BaseDataLayer
from flask_combo_jsonapi.data_layers.sorting.alchemy import create_sorts
from flask_combo_jsonapi.exceptions import (
RelationNotFound,
RelatedObjectNotFound,
JsonApiException,
ObjectNotFound,
InvalidInclude,
InvalidType,
PluginMethodNotImplementedError,
)
from flask_combo_jsonapi.data_layers.filtering.alchemy import create_filters
from flask_combo_jsonapi.schema import (
get_model_field,
get_related_schema,
get_relationships,
get_nested_fields,
get_schema_field,
)
from flask_combo_jsonapi.utils import SPLIT_REL
class SqlalchemyDataLayer(BaseDataLayer):
"""Sqlalchemy data layer"""
def __init__(self, kwargs):
"""Initialize an instance of SqlalchemyDataLayer
:param dict kwargs: initialization parameters of an SqlalchemyDataLayer instance
"""
super().__init__(kwargs)
if not hasattr(self, "session"):
raise Exception(
f"You must provide a session in data_layer_kwargs to use sqlalchemy data layer in {self.resource.__name__}"
)
if not hasattr(self, "model"):
raise Exception(
f"You must provide a model in data_layer_kwargs to use sqlalchemy data layer in {self.resource.__name__}"
)
self.disable_collection_count: bool = False
self.default_collection_count: int = -1
def post_init(self):
"""
Checking some props here
:return:
"""
if self.resource is None:
# if working outside the resource, it's not assigned here
return
if not hasattr(self.resource, "disable_collection_count") or self.resource.disable_collection_count is False:
return
params = self.resource.disable_collection_count
if isinstance(params, (bool, int)):
self.disable_collection_count = bool(params)
if isinstance(params, (tuple, list)):
try:
self.disable_collection_count, self.default_collection_count = params
except ValueError:
raise ValueError(
"Resource's attribute `disable_collection_count` "
"has to be bool or list/tuple with exactly 2 values!\n"
"For example `disable_collection_count = (True, 999)`"
)
# just ignoring other types, we don't know how to process them
def create_object(self, data, view_kwargs):
"""Create an object through sqlalchemy
:param dict data: the data validated by marshmallow
:param dict view_kwargs: kwargs from the resource view
:return DeclarativeMeta: an object from sqlalchemy
"""
for i_plugins in self.resource.plugins:
try:
i_plugins.data_layer_before_create_object(data=data, view_kwargs=view_kwargs, self_json_api=self)
except PluginMethodNotImplementedError:
pass
self.before_create_object(data, view_kwargs)
relationship_fields = get_relationships(self.resource.schema, model_field=True)
nested_fields = get_nested_fields(self.resource.schema, model_field=True)
join_fields = relationship_fields + nested_fields
for i_plugins in self.resource.plugins:
try:
data = i_plugins.data_layer_create_object_clean_data(
data=data, view_kwargs=view_kwargs, join_fields=join_fields, self_json_api=self,
)
except PluginMethodNotImplementedError:
pass
obj = self.model(**{key: value for (key, value) in data.items() if key not in join_fields})
self.apply_relationships(data, obj)
self.apply_nested_fields(data, obj)
for i_plugins in self.resource.plugins:
try:
i_plugins.data_layer_after_create_object(
data=data, view_kwargs=view_kwargs, obj=obj, self_json_api=self,
)
except PluginMethodNotImplementedError:
pass
self.session.add(obj)
try:
self.session.commit()
except JsonApiException as e:
self.session.rollback()
raise e
except Exception as e:
self.session.rollback()
raise JsonApiException(f"Object creation error: {e}", source={"pointer": "/data"})
self.after_create_object(obj, data, view_kwargs)
return obj
def get_object(self, view_kwargs, qs=None):
"""Retrieve an object through sqlalchemy
:params dict view_kwargs: kwargs from the resource view
:return DeclarativeMeta: an object from sqlalchemy
"""
# Нужно выталкивать из sqlalchemy Закешированные запросы, иначе не удастся загрузить данные о current_user
self.session.expire_all()
self.before_get_object(view_kwargs)
id_field = getattr(self, "id_field", inspect(self.model).primary_key[0].key)
try:
filter_field = getattr(self.model, id_field)
except Exception:
raise Exception(f"{self.model.__name__} has no attribute {id_field}")
url_field = getattr(self, "url_field", "id")
filter_value = view_kwargs[url_field]
query = self.retrieve_object_query(view_kwargs, filter_field, filter_value)
if self.resource is not None:
for i_plugins in self.resource.plugins:
try:
query = i_plugins.data_layer_get_object_update_query(
query=query, qs=qs, view_kwargs=view_kwargs, self_json_api=self,
)
except PluginMethodNotImplementedError:
pass
if qs is not None:
query = self.eagerload_includes(query, qs)
try:
obj = query.one()
except NoResultFound:
obj = None
self.after_get_object(obj, view_kwargs)
return obj
def get_collection_count(self, query, qs, view_kwargs) -> int:
"""
:param query: SQLAlchemy query
:param qs: QueryString
:param view_kwargs: view kwargs
:return:
"""
if self.disable_collection_count is True:
return self.default_collection_count
return query.count()
def get_collection(self, qs, view_kwargs):
"""Retrieve a collection of objects through sqlalchemy
:param QueryStringManager qs: a querystring manager to retrieve information from url
:param dict view_kwargs: kwargs from the resource view
:return tuple: the number of object and the list of objects
"""
# Нужно выталкивать из sqlalchemy Закешированные запросы, иначе не удастся загрузить данные о current_user
self.session.expire_all()
self.before_get_collection(qs, view_kwargs)
query = self.query(view_kwargs)
for i_plugins in self.resource.plugins:
try:
query = i_plugins.data_layer_get_collection_update_query(
query=query, qs=qs, view_kwargs=view_kwargs, self_json_api=self,
)
except PluginMethodNotImplementedError:
pass
if qs.filters:
query = self.filter_query(query, qs.filters, self.model)
if qs.sorting:
query = self.sort_query(query, qs.sorting)
objects_count = self.get_collection_count(query, qs, view_kwargs)
if getattr(self, "eagerload_includes", True):
query = self.eagerload_includes(query, qs)
query = self.paginate_query(query, qs.pagination)
collection = query.all()
collection = self.after_get_collection(collection, qs, view_kwargs)
return objects_count, collection
def update_object(self, obj, data, view_kwargs):
"""Update an object through sqlalchemy
:param DeclarativeMeta obj: an object from sqlalchemy
:param dict data: the data validated by marshmallow
:param dict view_kwargs: kwargs from the resource view
:return boolean: True if object have changed else False
"""
if obj is None:
url_field = getattr(self, "url_field", "id")
filter_value = view_kwargs[url_field]
raise ObjectNotFound(f"{self.model.__name__}: {filter_value} not found", source={"parameter": url_field})
self.before_update_object(obj, data, view_kwargs)
relationship_fields = get_relationships(self.resource.schema, model_field=True)
nested_fields = get_nested_fields(self.resource.schema, model_field=True)
join_fields = relationship_fields + nested_fields
for i_plugins in self.resource.plugins:
try:
data = i_plugins.data_layer_update_object_clean_data(
data=data, obj=obj, view_kwargs=view_kwargs, join_fields=join_fields, self_json_api=self,
)
except PluginMethodNotImplementedError:
pass
for key, value in data.items():
if hasattr(obj, key) and key not in join_fields:
setattr(obj, key, value)
self.apply_relationships(data, obj)
self.apply_nested_fields(data, obj)
try:
self.session.commit()
except JsonApiException as e:
self.session.rollback()
raise e
except Exception as e:
self.session.rollback()
orig_e = getattr(e, "orig", object)
message = getattr(orig_e, "args", [])
message = message[0] if message else None
e = message if message else e
raise JsonApiException("Update object error: " + str(e), source={"pointer": "/data"})
self.after_update_object(obj, data, view_kwargs)
def delete_object(self, obj, view_kwargs):
"""Delete an object through sqlalchemy
:param DeclarativeMeta item: an item from sqlalchemy
:param dict view_kwargs: kwargs from the resource view
"""
if obj is None:
url_field = getattr(self, "url_field", "id")
filter_value = view_kwargs[url_field]
raise ObjectNotFound(f"{self.model.__name__}: {filter_value} not found", source={"parameter": url_field})
self.before_delete_object(obj, view_kwargs)
for i_plugins in self.resource.plugins:
try:
i_plugins.data_layer_delete_object_clean_data(obj=obj, view_kwargs=view_kwargs, self_json_api=self)
except PluginMethodNotImplementedError:
pass
self.session.delete(obj)
try:
self.session.commit()
except JsonApiException as e:
self.session.rollback()
raise e
except Exception as e:
self.session.rollback()
raise JsonApiException("Delete object error: " + str(e))
self.after_delete_object(obj, view_kwargs)
def create_relationship(self, json_data, relationship_field, related_id_field, view_kwargs):
"""Create a relationship
:param dict json_data: the request params
:param str relationship_field: the model attribute used for relationship
:param str related_id_field: the identifier field of the related model
:param dict view_kwargs: kwargs from the resource view
:return boolean: True if relationship have changed else False
"""
self.before_create_relationship(json_data, relationship_field, related_id_field, view_kwargs)
obj = self.get_object(view_kwargs)
if obj is None:
url_field = getattr(self, "url_field", "id")
filter_value = view_kwargs[url_field]
raise ObjectNotFound(f"{self.model.__name__}: {filter_value} not found", source={"parameter": url_field})
if not hasattr(obj, relationship_field):
raise RelationNotFound(f"{obj.__class__.__name__} has no attribute {relationship_field}")
related_model = getattr(obj.__class__, relationship_field).property.mapper.class_
updated = False
if isinstance(json_data["data"], list):
obj_ids = {str(getattr(obj__, related_id_field)) for obj__ in getattr(obj, relationship_field)}
for obj_ in json_data["data"]:
if obj_["id"] not in obj_ids:
getattr(obj, relationship_field).append(
self.get_related_object(related_model, related_id_field, obj_)
)
updated = True
else:
related_object = None
if json_data["data"] is not None:
related_object = self.get_related_object(related_model, related_id_field, json_data["data"])
obj_id = getattr(getattr(obj, relationship_field), related_id_field, None)
new_obj_id = getattr(related_object, related_id_field, None)
if obj_id != new_obj_id:
setattr(obj, relationship_field, related_object)
updated = True
try:
self.session.commit()
except JsonApiException as e:
self.session.rollback()
raise e
except Exception as e:
self.session.rollback()
raise JsonApiException("Create relationship error: " + str(e))
self.after_create_relationship(obj, updated, json_data, relationship_field, related_id_field, view_kwargs)
return obj, updated
def get_relationship(self, relationship_field, related_type_, related_id_field, view_kwargs):
"""Get a relationship
:param str relationship_field: the model attribute used for relationship
:param str related_type_: the related resource type
:param str related_id_field: the identifier field of the related model
:param dict view_kwargs: kwargs from the resource view
:return tuple: the object and related object(s)
"""
self.before_get_relationship(relationship_field, related_type_, related_id_field, view_kwargs)
obj = self.get_object(view_kwargs)
if obj is None:
url_field = getattr(self, "url_field", "id")
filter_value = view_kwargs[url_field]
raise ObjectNotFound(f"{self.model.__name__}: {filter_value} not found", source={"parameter": url_field})
if not hasattr(obj, relationship_field):
raise RelationNotFound(f"{obj.__class__.__name__} has no attribute {relationship_field}")
related_objects = getattr(obj, relationship_field)
if related_objects is None:
return obj, related_objects
self.after_get_relationship(
obj, related_objects, relationship_field, related_type_, related_id_field, view_kwargs,
)
if isinstance(related_objects, InstrumentedList):
return obj, [{"type": related_type_, "id": getattr(obj_, related_id_field)} for obj_ in related_objects]
else:
return obj, {"type": related_type_, "id": getattr(related_objects, related_id_field)}
def update_relationship(self, json_data, relationship_field, related_id_field, view_kwargs):
"""Update a relationship
:param dict json_data: the request params
:param str relationship_field: the model attribute used for relationship
:param str related_id_field: the identifier field of the related model
:param dict view_kwargs: kwargs from the resource view
:return boolean: True if relationship have changed else False
"""
self.before_update_relationship(json_data, relationship_field, related_id_field, view_kwargs)
obj = self.get_object(view_kwargs)
if obj is None:
url_field = getattr(self, "url_field", "id")
filter_value = view_kwargs[url_field]
raise ObjectNotFound(f"{self.model.__name__}: {filter_value} not found", source={"parameter": url_field})
if not hasattr(obj, relationship_field):
raise RelationNotFound(f"{obj.__class__.__name__} has no attribute {relationship_field}")
related_model = getattr(obj.__class__, relationship_field).property.mapper.class_
updated = False
if isinstance(json_data["data"], list):
related_objects = []
for obj_ in json_data["data"]:
related_objects.append(self.get_related_object(related_model, related_id_field, obj_))
obj_ids = {getattr(obj__, related_id_field) for obj__ in getattr(obj, relationship_field)}
new_obj_ids = {getattr(related_object, related_id_field) for related_object in related_objects}
if obj_ids != new_obj_ids:
setattr(obj, relationship_field, related_objects)
updated = True
else:
related_object = None
if json_data["data"] is not None:
related_object = self.get_related_object(related_model, related_id_field, json_data["data"])
obj_id = getattr(getattr(obj, relationship_field), related_id_field, None)
new_obj_id = getattr(related_object, related_id_field, None)
if obj_id != new_obj_id:
setattr(obj, relationship_field, related_object)
updated = True
try:
self.session.commit()
except JsonApiException as e:
self.session.rollback()
raise e
except Exception as e:
self.session.rollback()
raise JsonApiException("Update relationship error: " + str(e))
self.after_update_relationship(obj, updated, json_data, relationship_field, related_id_field, view_kwargs)
return obj, updated
def delete_relationship(self, json_data, relationship_field, related_id_field, view_kwargs):
"""Delete a relationship
:param dict json_data: the request params
:param str relationship_field: the model attribute used for relationship
:param str related_id_field: the identifier field of the related model
:param dict view_kwargs: kwargs from the resource view
"""
self.before_delete_relationship(json_data, relationship_field, related_id_field, view_kwargs)
obj = self.get_object(view_kwargs)
if obj is None:
url_field = getattr(self, "url_field", "id")
filter_value = view_kwargs[url_field]
raise ObjectNotFound(f"{self.model.__name__}: {filter_value} not found", source={"parameter": url_field})
if not hasattr(obj, relationship_field):
raise RelationNotFound(f"{obj.__class__.__name__} has no attribute {relationship_field}")
related_model = getattr(obj.__class__, relationship_field).property.mapper.class_
updated = False
if isinstance(json_data["data"], list):
obj_ids = {str(getattr(obj__, related_id_field)) for obj__ in getattr(obj, relationship_field)}
for obj_ in json_data["data"]:
if obj_["id"] in obj_ids:
getattr(obj, relationship_field).remove(
self.get_related_object(related_model, related_id_field, obj_)
)
updated = True
else:
setattr(obj, relationship_field, None)
updated = True
try:
self.session.commit()
except JsonApiException as e:
self.session.rollback()
raise e
except Exception as e:
self.session.rollback()
raise JsonApiException("Delete relationship error: " + str(e))
self.after_delete_relationship(obj, updated, json_data, relationship_field, related_id_field, view_kwargs)
return obj, updated
def get_related_object(self, related_model, related_id_field, obj):
"""Get a related object
:param Model related_model: an sqlalchemy model
:param str related_id_field: the identifier field of the related model
:param DeclarativeMeta obj: the sqlalchemy object to retrieve related objects from
:return DeclarativeMeta: a related object
"""
try:
related_object = (
self.session.query(related_model).filter(getattr(related_model, related_id_field) == obj["id"]).one()
)
except NoResultFound:
raise RelatedObjectNotFound(f"{related_model.__name__}.{related_id_field}: {obj['id']} not found")
return related_object
def apply_relationships(self, data, obj):
"""Apply relationship provided by data to obj
:param dict data: data provided by the client
:param DeclarativeMeta obj: the sqlalchemy object to plug relationships to
:return boolean: True if relationship have changed else False
"""
relationships_to_apply = []
relationship_fields = get_relationships(self.resource.schema, model_field=True)
for key, value in data.items():
if key in relationship_fields:
related_model = getattr(obj.__class__, key).property.mapper.class_
schema_field = get_schema_field(self.resource.schema, key)
related_id_field = self.resource.schema._declared_fields[schema_field].id_field
if isinstance(value, list):
related_objects = []
for identifier in value:
related_object = self.get_related_object(related_model, related_id_field, {"id": identifier})
related_objects.append(related_object)
relationships_to_apply.append({"field": key, "value": related_objects})
else:
related_object = None
if value is not None:
related_object = self.get_related_object(related_model, related_id_field, {"id": value})
relationships_to_apply.append({"field": key, "value": related_object})
for relationship in relationships_to_apply:
setattr(obj, relationship["field"], relationship["value"])
def apply_nested_fields(self, data, obj):
nested_fields_to_apply = []
nested_fields = get_nested_fields(self.resource.schema, model_field=True)
for key, value in data.items():
if key in nested_fields:
nested_field_inspection = inspect(getattr(obj.__class__, key))
if not isinstance(nested_field_inspection, QueryableAttribute):
raise InvalidType("Unrecognized nested field type: not a queryable attribute.")
if isinstance(nested_field_inspection.property, RelationshipProperty):
nested_model = getattr(obj.__class__, key).property.mapper.class_
if isinstance(value, list):
nested_objects = []
for identifier in value:
nested_object = nested_model(**identifier)
nested_objects.append(nested_object)
nested_fields_to_apply.append({"field": key, "value": nested_objects})
else:
nested_fields_to_apply.append({"field": key, "value": nested_model(**value)})
elif isinstance(nested_field_inspection.property, ColumnProperty):
nested_fields_to_apply.append({"field": key, "value": value})
else:
raise InvalidType("Unrecognized nested field type: not a RelationshipProperty or ColumnProperty.")
for nested_field in nested_fields_to_apply:
setattr(obj, nested_field["field"], nested_field["value"])
def filter_query(self, query, filter_info, model):
"""Filter query according to jsonapi 1.0
:param Query query: sqlalchemy query to sort
:param filter_info: filter information
:type filter_info: dict or None
:param DeclarativeMeta model: an sqlalchemy model
:return Query: the sorted query
"""
if filter_info:
filters, joins = create_filters(model, filter_info, self.resource)
for i_join in joins:
query = query.join(*i_join)
query = query.filter(*filters)
return query
def sort_query(self, query, sort_info):
"""Sort query according to jsonapi 1.0
:param Query query: sqlalchemy query to sort
:param list sort_info: sort information
:return Query: the sorted query
"""
if sort_info:
sorts, joins = create_sorts(self.model, sort_info, self.resource if hasattr(self, "resource") else None)
for i_join in joins:
query = query.join(*i_join)
for i_sort in sorts:
query = query.order_by(i_sort)
return query
def paginate_query(self, query, paginate_info):
"""Paginate query according to jsonapi 1.0
:param Query query: sqlalchemy queryset
:param dict paginate_info: pagination information
:return Query: the paginated query
"""
if int(paginate_info.get("size", 1)) == 0:
return query
page_size = int(paginate_info.get("size", 0)) or current_app.config["PAGE_SIZE"]
query = query.limit(page_size)
if paginate_info.get("number"):
query = query.offset((int(paginate_info["number"]) - 1) * page_size)
return query
def eagerload_includes(self, query, qs):
"""Use eagerload feature of sqlalchemy to optimize data retrieval for include querystring parameter
:param Query query: sqlalchemy queryset
:param QueryStringManager qs: a querystring manager to retrieve information from url
:return Query: the query with includes eagerloaded
"""
for include in qs.include:
joinload_object = None
if SPLIT_REL in include:
current_schema = self.resource.schema
for obj in include.split(SPLIT_REL):
try:
field = get_model_field(current_schema, obj)
except Exception as e:
raise InvalidInclude(str(e))
if joinload_object is None:
joinload_object = joinedload(field)
else:
joinload_object = joinload_object.joinedload(field)
related_schema_cls = get_related_schema(current_schema, obj)
if isinstance(related_schema_cls, SchemaABC):
related_schema_cls = related_schema_cls.__class__
else:
related_schema_cls = class_registry.get_class(related_schema_cls)
current_schema = related_schema_cls
else:
try:
field = get_model_field(self.resource.schema, include)
except Exception as e:
raise InvalidInclude(str(e))
joinload_object = joinedload(field)
query = query.options(joinload_object)
return query
def retrieve_object_query(self, view_kwargs, filter_field, filter_value):
"""Build query to retrieve object
:param dict view_kwargs: kwargs from the resource view
:params sqlalchemy_field filter_field: the field to filter on
:params filter_value: the value to filter with
:return sqlalchemy query: a query from sqlalchemy
"""
return self.session.query(self.model).filter(filter_field == filter_value)
def query(self, view_kwargs):
"""Construct the base query to retrieve wanted data
:param dict view_kwargs: kwargs from the resource view
"""
return self.session.query(self.model)
def before_create_object(self, data, view_kwargs):
"""Provide additional data before object creation
:param dict data: the data validated by marshmallow
:param dict view_kwargs: kwargs from the resource view
"""
pass
def after_create_object(self, obj, data, view_kwargs):
"""Provide additional data after object creation
:param obj: an object from data layer
:param dict data: the data validated by marshmallow
:param dict view_kwargs: kwargs from the resource view
"""
pass
def before_get_object(self, view_kwargs):
"""Make work before to retrieve an object
:param dict view_kwargs: kwargs from the resource view
"""
pass
def after_get_object(self, obj, view_kwargs):
"""Make work after to retrieve an object
:param obj: an object from data layer
:param dict view_kwargs: kwargs from the resource view
"""
pass
def before_get_collection(self, qs, view_kwargs):
"""Make work before to retrieve a collection of objects
:param QueryStringManager qs: a querystring manager to retrieve information from url
:param dict view_kwargs: kwargs from the resource view
"""
pass
def after_get_collection(self, collection, qs, view_kwargs):
"""Make work after to retrieve a collection of objects
:param iterable collection: the collection of objects
:param QueryStringManager qs: a querystring manager to retrieve information from url
:param dict view_kwargs: kwargs from the resource view
"""
return collection
def before_update_object(self, obj, data, view_kwargs):
"""Make checks or provide additional data before update object
:param obj: an object from data layer
:param dict data: the data validated by marshmallow
:param dict view_kwargs: kwargs from the resource view
"""
pass
def after_update_object(self, obj, data, view_kwargs):
"""Make work after update object
:param obj: an object from data layer
:param dict data: the data validated by marshmallow
:param dict view_kwargs: kwargs from the resource view
"""
pass
def before_delete_object(self, obj, view_kwargs):
"""Make checks before delete object
:param obj: an object from data layer
:param dict view_kwargs: kwargs from the resource view
"""
pass
def after_delete_object(self, obj, view_kwargs):
"""Make work after delete object
:param obj: an object from data layer
:param dict view_kwargs: kwargs from the resource view
"""
pass
def before_create_relationship(self, json_data, relationship_field, related_id_field, view_kwargs):
"""Make work before to create a relationship
:param dict json_data: the request params
:param str relationship_field: the model attribute used for relationship
:param str related_id_field: the identifier field of the related model
:param dict view_kwargs: kwargs from the resource view
:return boolean: True if relationship have changed else False
"""
pass
def after_create_relationship(self, obj, updated, json_data, relationship_field, related_id_field, view_kwargs):
"""Make work after to create a relationship
:param obj: an object from data layer
:param bool updated: True if object was updated else False
:param dict json_data: the request params
:param str relationship_field: the model attribute used for relationship
:param str related_id_field: the identifier field of the related model
:param dict view_kwargs: kwargs from the resource view
:return boolean: True if relationship have changed else False
"""
pass
def before_get_relationship(self, relationship_field, related_type_, related_id_field, view_kwargs):
"""Make work before to get information about a relationship
:param str relationship_field: the model attribute used for relationship
:param str related_type_: the related resource type
:param str related_id_field: the identifier field of the related model
:param dict view_kwargs: kwargs from the resource view
:return tuple: the object and related object(s)
"""
pass
def after_get_relationship(
self, obj, related_objects, relationship_field, related_type_, related_id_field, view_kwargs,
):
"""Make work after to get information about a relationship
:param obj: an object from data layer
:param iterable related_objects: related objects of the object
:param str relationship_field: the model attribute used for relationship
:param str related_type_: the related resource type
:param str related_id_field: the identifier field of the related model
:param dict view_kwargs: kwargs from the resource view
:return tuple: the object and related object(s)
"""
pass
def before_update_relationship(self, json_data, relationship_field, related_id_field, view_kwargs):
"""Make work before to update a relationship
:param dict json_data: the request params
:param str relationship_field: the model attribute used for relationship
:param str related_id_field: the identifier field of the related model
:param dict view_kwargs: kwargs from the resource view
:return boolean: True if relationship have changed else False
"""
pass
def after_update_relationship(self, obj, updated, json_data, relationship_field, related_id_field, view_kwargs):
"""Make work after to update a relationship
:param obj: an object from data layer
:param bool updated: True if object was updated else False
:param dict json_data: the request params
:param str relationship_field: the model attribute used for relationship
:param str related_id_field: the identifier field of the related model
:param dict view_kwargs: kwargs from the resource view
:return boolean: True if relationship have changed else False
"""
pass
def before_delete_relationship(self, json_data, relationship_field, related_id_field, view_kwargs):
"""Make work before to delete a relationship
:param dict json_data: the request params
:param str relationship_field: the model attribute used for relationship
:param str related_id_field: the identifier field of the related model
:param dict view_kwargs: kwargs from the resource view
"""
pass
def after_delete_relationship(self, obj, updated, json_data, relationship_field, related_id_field, view_kwargs):
"""Make work after to delete a relationship
:param obj: an object from data layer
:param bool updated: True if object was updated else False
:param dict json_data: the request params
:param str relationship_field: the model attribute used for relationship
:param str related_id_field: the identifier field of the related model
:param dict view_kwargs: kwargs from the resource view
"""
pass