def serialize_relationships(data, source_obj, relationship_objs): """Convert relationship objects to dictionaries. Args: data (dict): Dictionary containing info of source object. source_obj (mbdata.models): object of source entity. relationship_objs (dict): Dictionary containing list of objects of different relations. Returns: Dictionary containing lists of dictionaries of related entities. """ for entity_type in ENTITY_MODELS: relation = '{0}-rels'.format(entity_type) if relation in relationship_objs: data[relation] = [] for obj in relationship_objs[relation]: link_data = { 'type': obj.link.link_type.name, 'type-id': obj.link.link_type.gid, 'begin-year': obj.link.begin_date_year, 'end-year': obj.link.end_date_year, } link_data[ 'direction'] = 'forward' if source_obj.id == obj.entity0_id else 'backward' if obj.link.ended: link_data['ended'] = True link_data[entity_type] = SERIALIZE_ENTITIES[entity_type]( get_link_target(obj, source_obj)) data[relation].append(link_data)
def serialize_relationships(data, source_obj, relationship_objs): """Convert relationship objects to dictionaries. Args: data (dict): Dictionary containing info of source object. source_obj (mbdata.models): object of source entity. relationship_objs (dict): Dictionary containing list of objects of different relations. Returns: Dictionary containing lists of dictionaries of related entities. """ for entity_type in ENTITY_MODELS: relation = '{0}-rels'.format(entity_type) if relation in relationship_objs: data[relation] = [] for obj in relationship_objs[relation]: link_data = { 'type': obj.link.link_type.name, 'type-id': obj.link.link_type.gid, 'begin-year': obj.link.begin_date_year, 'end-year': obj.link.end_date_year, } link_data['direction'] = 'forward' if source_obj.id == obj.entity0_id else 'backward' if obj.link.ended: link_data['ended'] = True link_data[entity_type] = SERIALIZE_ENTITIES[entity_type](get_link_target(obj, source_obj)) data[relation].append(link_data)
def serialize_relationships(data, obj, include): if include.relationships: data['relationships'] = {} for type in ENTITY_TYPES: if include.relationships.check(type): links_data = [] for link in getattr(obj, '{0}_links'.format(type), ()): link_data = {'type': link.link.link_type.name} serialize_partial_date(link_data, 'begin_data', link.link.begin_date) serialize_partial_date(link_data, 'end_data', link.link.end_date) if link.link.ended: link_data['ended'] = True link_data[type] = ENTITY_SERIALIZERS[type](get_link_target( link, obj), include.relationships.check(type)) links_data.append(link_data) if links_data: data['relationships'][type] = links_data