Commit 461e8140 authored by Denis Laxalde's avatar Denis Laxalde
Browse files

Let "relation" mappers serialize related entities directly

Previously, the set of related entities was computed within
"jsonschema.relation" mappers and their serialize() method accepted only
the main "entity" from which this "related" set was computed. This is
asymmetric with "jsonschema.collection" which serialize() method handles
a list of entities. So we make the former mappers accept the set of
related directly as an argument to their serialize() method. This set is
computed by "jsonschema.entity" mapper.

This is a step forward merging "jsonschema.relation" mappers (for
non-final relation) with "jsonschema.collection" mappers.
parent e80e72ab7600
......@@ -240,11 +240,19 @@ class EntityMapper(ETypeMapper):
data = {}
for mapper in self._object_mappers():
data[mapper.name] = mapper.serialize(entity)
eschema = entity.e_schema
for rtype, role, target_types in self.relations(VIEW_ROLE):
relation_mapper = self._relation_mapper(rtype, role, target_types)
if eschema.rdef(rtype, role=role, takefirst=True).final:
value = relation_mapper.serialize(entity)
if value is None:
continue
else:
rset = entity.related(
rtype, role, targettypes=tuple(target_types))
if not rset:
continue
value = relation_mapper.serialize(rset.entities())
data[relation_mapper.orm_rtype] = value
return data
......
......@@ -459,12 +459,7 @@ class InlinedRelationMapper(_RelationMapper):
result.append(self._cw.create_entity(target_type, **subvalues))
return {self.orm_rtype: result}
def serialize(self, entity):
rset = entity.related(
self.rtype, self.role, targettypes=tuple(self.target_types))
if not rset:
return None
def serialize(self, related_entities):
def serialize(entity):
mapper = self.select_mapper(
'jsonschema.entity', entity=entity,
......@@ -473,7 +468,7 @@ class InlinedRelationMapper(_RelationMapper):
)
return mapper.serialize()
return [serialize(related) for related in rset.entities()]
return [serialize(related) for related in related_entities]
class InlinedRelationItemMapper(BaseRelationMapper):
......@@ -532,12 +527,8 @@ class RelationMapper(_RelationMapper):
raise ValidationError(entity, {self.rtype: msg})
return {self.orm_rtype: values}
def serialize(self, entity):
rset = entity.related(
self.rtype, self.role, targettypes=tuple(self.target_types))
if not rset:
return None
return [{"id": text_type(x.eid)} for x in rset.entities()]
def serialize(self, entities):
return [{"id": text_type(x.eid)} for x in entities]
class ETypeRelationItemMapper(BaseRelationMapper):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment