def get_ref_entity_name_to_ref_field_value( self, entity: BaseModel, entity_type: Type[BaseModel]) -> Dict[str, str]: """ Get a dictionary with name of reference entities to value of the referencing field, being id of the referencing entity :param entity: CSR entity :param entity_type: type of the entity :return: dictionary from reference entity name to value of reference field """ entity_ref_to_ref_id = dict() id_attribute = self.get_id_field_name(entity_type) entity_type_name = entity_type.schema()['title'] entity_id = entity.__getattribute__(id_attribute) entity_ref_to_ref_id[entity_type_name] = entity_id if entity_type_name == 'Individual': return entity_ref_to_ref_id # Follow reference fields to obtain identifiers of linked entities ref_fields = self.get_field_properties_by_keyword( entity_type, 'references') for field_name, ref_entity_name in ref_fields.items(): if not self.skip_reference(type(entity), ref_entity_name): # Lookup referenced entity referenced_entity_type = list([ entity for entity in SubjectEntity.__args__ if entity.schema()['title'] == ref_entity_name ])[0] referenced_id = entity.__getattribute__(field_name) if not referenced_id: continue referenced_id_attribute = self.get_id_field_name( referenced_entity_type) referenced_entities = [ e for e in self.subject_registry.entity_data[ref_entity_name] if e.__getattribute__(referenced_id_attribute) == referenced_id ] if not referenced_entities: raise MappingException( f'{entity_type_name} with id {entity_id} has reference to non-existing' f' {ref_entity_name} with id {referenced_id}.') # Recursively add identifiers from referenced entity referenced_ids = self.get_ref_entity_name_to_ref_field_value( referenced_entities[0], referenced_entity_type) entity_ref_to_ref_id.update(referenced_ids) return entity_ref_to_ref_id
def travel_major_tokens(curdat: BaseModel, current_majors: List[str]) -> List[str]: set_attributes = curdat.__fields_set__ all_keys = list(curdat.__fields__.keys()) collapsed_majors = '_'.join(current_majors) output_strings = [] for attr in set_attributes: if attr not in all_keys: all_keys.append(attr) for attr in all_keys: if curdat.__getattribute__(attr) != None: for val in curdat.__getattribute__(attr): val = val[1] if issubclass(type(val), BaseModel): # this is a major token again, so we need to recurse res = detailparser.travel_major_tokens( val, current_majors + [attr]) output_strings.extend(res) else: # we're in a minor token, so start writing coach if attr != "desc": output_strings.append( f'@{collapsed_majors}-{attr} {val}\n') else: output_strings.append( f'@{collapsed_majors} {val}\n') # if we haven't already had a newline, add a newline # ( double-newline happens if we're in a multi-major-token that is not followed by a sister major token, # e.g. : # Data-Primary asdf # Software-Primary fdsa # # instead of: # Data-Primary asdf # Data-Ref fdsa) if output_strings[-1] != "\n": output_strings.append("\n") return output_strings