Commit 66c8f0a6 authored by Denis Laxalde's avatar Denis Laxalde
Browse files

Move InlinedRelationItemMapper into "collection" module

This mapper has "jsonschema.item" regid, so it belongs to
mappers.collection module. We also rename it changing "inlined" into
composite since this is was it is (see selector). Since we lost the base
class we have to re-implement selection argument parsing and setting as
attribute, hence the new __init__ method.
parent 5c5c54910360
......@@ -34,6 +34,7 @@ from .base import (
JSONSchemaMapper,
JSONSchemaDeserializer,
JSONSchemaSerializer,
ProtectedDict,
add_descriptive_metadata,
add_links,
object_schema,
......@@ -41,13 +42,16 @@ from .base import (
from .predicates import (
_for_workflowable_entity,
yams_component_target,
yams_final_rtype,
)
from . import _utils
__all__ = [
'EntityCollectionMapper',
'RelatedCollectionMapper',
'CollectionItemMapper',
'CompositeRelationItemMapper',
]
......@@ -262,3 +266,53 @@ class TrInfoCollectionMapper(EntityCollectionMapper):
wfobj = entity.cw_adapt_to('IWorkflowable')
entities = wfobj.workflow_history
return super(TrInfoCollectionMapper, self).serialize(entities)
class CompositeRelationItemMapper(CollectionItemMapper):
"""Mapper for items target of a composite relation."""
__select__ = (
~yams_final_rtype()
& yams_component_target()
& (match_kwargs('etype') | match_kwargs('entity'))
& match_kwargs('target_types')
)
# Per yams_component_target selector, 'rtype' and 'role' kwargs should be
# present but 'target_types' is optional.
def __init__(self, _cw, rtype, role, target_types=None,
etype=None, entity=None, **kwargs):
super(CompositeRelationItemMapper, self).__init__(_cw, **kwargs)
self.rtype = rtype
self.role = role
self.target_types = list(_utils.relation_target_types(
_cw.vreg.schema, rtype, role, target_types))
if etype is not None:
self.etype = etype
elif entity is not None:
self.etype = entity.cw_etype
def schema_and_definitions(self, schema_role=None):
items, definitions = [], ProtectedDict()
for target_type in self.target_types:
mapper = self.select_mapper(
'jsonschema.entity', etype=target_type,
rtype=self.rtype, role=neg_role(self.role),
target_types={self.etype},
)
subschema, defs = mapper.schema_and_definitions(schema_role)
items.append({
'$ref': '#/definitions/{}'.format(target_type),
})
definitions[target_type] = subschema
if defs:
definitions.update(defs)
nitems = len(items)
if nitems == 0:
return None, None
elif nitems == 1:
return items[0], definitions
else:
schema = {
'oneOf': items,
}
return schema, definitions
......@@ -51,7 +51,6 @@ from .base import (
add_descriptive_metadata,
add_links,
object_schema,
ProtectedDict,
)
from .predicates import (
_etype_from_context,
......@@ -74,7 +73,6 @@ __all__ = [
'DatetimeMapper',
'BytesMapper',
'InlinedRelationMapper',
'InlinedRelationItemMapper',
'RelationMapper',
'ETypeRelationItemMapper',
'EntityRelationItemMapper',
......@@ -466,38 +464,6 @@ class InlinedRelationMapper(_RelationMapper):
return [serialize(related) for related in related_entities]
class InlinedRelationItemMapper(BaseRelationMapper):
"""Mapper for items of 'inlined' relation."""
__regid__ = 'jsonschema.item'
__select__ = InlinedRelationMapper.__select__
def schema_and_definitions(self, schema_role=None):
items, definitions = [], ProtectedDict()
for target_type in self.target_types:
mapper = self.select_mapper(
'jsonschema.entity', etype=target_type,
rtype=self.rtype, role=neg_role(self.role),
target_types={self.etype},
)
subschema, defs = mapper.schema_and_definitions(schema_role)
items.append({
'$ref': '#/definitions/{}'.format(target_type),
})
definitions[target_type] = subschema
if defs:
definitions.update(defs)
nitems = len(items)
if nitems == 0:
return None, None
elif nitems == 1:
return items[0], definitions
else:
schema = {
'oneOf': items,
}
return schema, definitions
@JSONSchemaDeserializer.register
@JSONSchemaSerializer.register
class RelationMapper(_RelationMapper):
......
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