-
Notifications
You must be signed in to change notification settings - Fork 2
/
emberify.py
77 lines (65 loc) · 2.47 KB
/
emberify.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
import copy
import sqlalchemy
def emberify(collection, model=None, many=True):
def emberify_record(record=None):
"""
Ember expects the reference field to have the ID. What is being
recieved by restless is the reference field has the de-referenced data
and the field that stores the id to the reference is being thrown in as
well, which causes errors.
Ember expects:
{
posts: [
{id: 1, user: 27, ...}
],
users: {id: 27, ...} // Optional sideloading
}
We're getting:
{
posts: [
{id: 1, user_id: 27, user: {id: 27, name: ...} ...}
}
"""
# Go through each column and see if there's a foreign key involved
del_columns = []
for key, val in record.iteritems():
column = model.__dict__.get(key)
# Some sort of relationship
property = getattr(column, 'property', None)
if property and type(property) is sqlalchemy.orm.relationships.RelationshipProperty:
id_column = next(iter(column.property.local_columns))
# This model is being referenced by something else
if id_column.primary_key:
#TODO: sideload
other_ids = []
for other_item in val:
#FIXME: assuming id is primary key
other_ids.append(other_item['id'])
record[key] = other_ids
# This model is referencing something else
else:
#TODO: Sideload
record[key] = record[id_column.name]
del_columns.append(id_column.name)
#TODO: sideload instead of delete
for col in del_columns:
del(record[col])
def emberify_single(result=None, **kw):
emberify_record(result)
result[collection] = copy.deepcopy(result)
for key in result.keys():
if key != collection:
del(result[key])
def emberify_many(result=None, **kw):
# Remove pagination
for key in result.keys():
if key != 'objects':
del(result[key])
result[collection] = result['objects']
del(result['objects'])
# Handle foreign keys
for item in result[collection]:
emberify_record(item)
if many:
return emberify_many
return emberify_single