14def convert_to_dict(db, obj):
15 dal_dict = {
16 "__type": obj.className(),
17 "_id": {
18 "$oid": hash_function(obj),
19 }
20 }
21
22 for attribute_name, attribute_value in db.attributes(obj.className(), all=True).items():
23 dal_dict[attribute_name] = getattr(obj, attribute_name)
24
25 for relation_name, relation_value in db.relations(obj.className(), all=True).items():
26 relation_object = getattr(obj, relation_name, None)
27
28 if relation_object is None:
29 dal_dict[relation_name] = None
30
31 elif not relation_value.get('multivalue', False):
32 dal_dict[relation_name] = {
33
34 '$id': hash_function(relation_object),
35 }
36
37 else:
38 dal_dict[relation_name] = [
39 {
40 "$id": hash_function(one_relation_object)
41
42 }
43 for one_relation_object in relation_object
44 ]
45
46 return dict(sorted(dal_dict.items()))
47
48