-
Notifications
You must be signed in to change notification settings - Fork 68
/
serialize.py
46 lines (34 loc) · 1.4 KB
/
serialize.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
from collections.abc import Iterable
from .inspection import InspectionMixin
class SerializeMixin(InspectionMixin):
"""Mixin to make model serializable."""
__abstract__ = True
def to_dict(self,nested = False, hybrid_attributes = False, exclude = None):
"""Return dict object with model's data.
:param nested: flag to return nested relationships' data if true
:type: bool
:param hybrid_attributes: flag to include hybrid attributes if true
:type: bool
:return: dict
"""
result = dict()
if exclude is None:
view_cols = self.columns
else :
view_cols = filter(lambda e: e not in exclude, self.columns)
for key in view_cols :
result[key] = getattr(self, key)
if hybrid_attributes:
for key in self.hybrid_properties:
result[key] = getattr(self, key)
if nested:
for key in self.relations:
obj = getattr(self, key)
if isinstance(obj, SerializeMixin):
result[key] = obj.to_dict(hybrid_attributes=hybrid_attributes)
elif isinstance(obj, Iterable):
result[key] = [
o.to_dict(hybrid_attributes=hybrid_attributes) for o in obj
if isinstance(o, SerializeMixin)
]
return result