Example #1
0
    def _serialize_entity(self, entity: ffd.Entity, add_new: bool = False):
        relationships = self._get_relationships(entity.__class__)
        if len(relationships.keys()) > 0:
            obj = entity.to_dict(force_all=True,
                                 skip=list(relationships.keys()))
            for k, v in relationships.items():
                if v['this_side'] == 'one':
                    try:
                        sub_entity = getattr(entity, k)
                        if sub_entity is not None:
                            if add_new:
                                exists = self._cache_get(
                                    f'aggregate_references.{sub_entity.__class__}.{sub_entity.id_value()}'
                                )
                                if exists is None:
                                    e = self._registry(v['target']).find(
                                        sub_entity.id_value())
                                    if e is not None:
                                        self._cache_set(
                                            f'aggregate_references.{sub_entity.__class__}.{sub_entity.id_value()}',
                                            True)
                                    else:
                                        self._registry(
                                            v['target']).append(sub_entity)
                            obj[k] = sub_entity.id_value()
                    except AttributeError:
                        obj[k] = None
                elif v['this_side'] == 'many':
                    obj[k] = []
                    for f in getattr(entity, k):
                        try:
                            if add_new:
                                exists = self._cache_get(
                                    f'aggregate_references.{f.__class__}.{f.id_value()}'
                                )
                                if exists is None:
                                    e = self._registry(v['target']).find(
                                        f.id_value())
                                    if e is not None:
                                        self._cache_set(
                                            f'aggregate_references.{f.__class__}.{f.id_value()}',
                                            True)
                                    else:
                                        self._registry(v['target']).append(f)
                            obj[k].append(f.id_value())
                        except AttributeError:
                            obj[k].append(None)
        else:
            obj = entity.to_dict(force_all=True)

        return self._serializer.serialize(obj)
 def _get_hash(self, entity: ffd.Entity):
     return hashlib.md5(self._serializer.serialize(entity.to_dict(force_all=True)).encode('utf-8')).hexdigest()