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): ...@@ -240,11 +240,19 @@ class EntityMapper(ETypeMapper):
data = {} data = {}
for mapper in self._object_mappers(): for mapper in self._object_mappers():
data[] = mapper.serialize(entity) data[] = mapper.serialize(entity)
eschema = entity.e_schema
for rtype, role, target_types in self.relations(VIEW_ROLE): for rtype, role, target_types in self.relations(VIEW_ROLE):
relation_mapper = self._relation_mapper(rtype, role, target_types) relation_mapper = self._relation_mapper(rtype, role, target_types)
value = relation_mapper.serialize(entity) if eschema.rdef(rtype, role=role, takefirst=True).final:
if value is None: value = relation_mapper.serialize(entity)
continue if value is None:
rset = entity.related(
rtype, role, targettypes=tuple(target_types))
if not rset:
value = relation_mapper.serialize(rset.entities())
data[relation_mapper.orm_rtype] = value data[relation_mapper.orm_rtype] = value
return data return data
...@@ -459,12 +459,7 @@ class InlinedRelationMapper(_RelationMapper): ...@@ -459,12 +459,7 @@ class InlinedRelationMapper(_RelationMapper):
result.append(self._cw.create_entity(target_type, **subvalues)) result.append(self._cw.create_entity(target_type, **subvalues))
return {self.orm_rtype: result} return {self.orm_rtype: result}
def serialize(self, entity): def serialize(self, related_entities):
rset = entity.related(
self.rtype, self.role, targettypes=tuple(self.target_types))
if not rset:
return None
def serialize(entity): def serialize(entity):
mapper = self.select_mapper( mapper = self.select_mapper(
'jsonschema.entity', entity=entity, 'jsonschema.entity', entity=entity,
...@@ -473,7 +468,7 @@ class InlinedRelationMapper(_RelationMapper): ...@@ -473,7 +468,7 @@ class InlinedRelationMapper(_RelationMapper):
) )
return mapper.serialize() return mapper.serialize()
return [serialize(related) for related in rset.entities()] return [serialize(related) for related in related_entities]
class InlinedRelationItemMapper(BaseRelationMapper): class InlinedRelationItemMapper(BaseRelationMapper):
...@@ -532,12 +527,8 @@ class RelationMapper(_RelationMapper): ...@@ -532,12 +527,8 @@ class RelationMapper(_RelationMapper):
raise ValidationError(entity, {self.rtype: msg}) raise ValidationError(entity, {self.rtype: msg})
return {self.orm_rtype: values} return {self.orm_rtype: values}
def serialize(self, entity): def serialize(self, entities):
rset = entity.related( return [{"id": text_type(x.eid)} for x in entities]
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()]
class ETypeRelationItemMapper(BaseRelationMapper): 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