-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
outputmixin.py
40 lines (33 loc) · 1.4 KB
/
outputmixin.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
# Taken from: http://blog.mmast.net/sqlalchemy-serialize-json
import json
from sqlalchemy.ext.declarative import DeclarativeMeta
class OutputMixin(object):
RELATIONSHIPS_TO_DICT = False
def __iter__(self):
return self.to_dict().iteritems()
def to_dict(self, rel=None, backref=None):
if rel is None:
rel = self.RELATIONSHIPS_TO_DICT
res = {column.key: getattr(self, attr)
for attr, column in self.__mapper__.c.items()}
if rel:
for attr, relation in self.__mapper__.relationships.items():
# Avoid recursive loop between to tables.
if backref == relation.table:
continue
value = getattr(self, attr)
if value is None:
res[relation.key] = None
elif isinstance(value.__class__, DeclarativeMeta):
res[relation.key] = value.to_dict(backref=self.__table__)
else:
res[relation.key] = [i.to_dict(backref=self.__table__)
for i in value]
return res
def to_json(self, rel=None):
def extended_encoder(x):
if isinstance(x, datetime):
return x.isoformat()
if rel is None:
rel = self.RELATIONSHIPS_TO_DICT
return json.dumps(self.to_dict(rel), default=extended_encoder)