hivesolutions / appier

Joyful Python Web App development
http://appier.hive.pt
Apache License 2.0
127 stars 22 forks source link

Support for recursive serialization to maps #4

Closed tsilva closed 8 years ago

tsilva commented 8 years ago

Description

Appier has support for converting a model to a dictionary, however, it has no support for recursively doing so for the model's relations.

Solution

Here's a sample solution (checking for relations is not correct):

def map_full(self):
    cls = self.__class__
    map = self.map()

    for name, value in appier.legacy.items(map):
        _definition = cls.definition_n(name)
        _type = _definition.get("type", None)
        if not _type: continue

        type_s = str(_type)
        if type_s == "<class 'appier.typesf._Reference'>":
            value = getattr(self, name)
            map[name] = value.map_full()
        elif type_s == "<class 'appier.typesf._References'>":
            value = getattr(self, name)
            map[name] = [_value.map_full() for _value in value]

    return map