def get_entity_ref(db, entity_id): """ Make an EntityRef by looking up the entity_id in the database. entity_id -> EntityRef(<entity_id>, <entity_type>, <entity_name>) """ # TODO: Include entity names in change_params, so that we don't have to # look them up. constants = Factory.get("Constants")(db) entity = Factory.get("Entity")(db) entity_ident = entity_type = None # Lookup type try: ent = entity.get_subclassed_object(id=entity_id) entity_type = six.text_type(constants.EntityType(ent.entity_type)) try: namespace = constants.ValueDomain( ENTITY_TYPE_NAMESPACE.get(entity_type, None)) entity_ident = ent.get_name(namespace) except (AttributeError, TypeError, NotFoundError): pass # Handling ValueError here is a hack for handling entities that can't # be accessed trough entity.get_subclassed_object() except (NotFoundError, ValueError): pass # We *have* entity_id, might have entity_type, and if so, may also have # an entity_ident. return EntityRef(entity_id, entity_type, entity_ident or six.text_type(entity_id))
def get_entity_ref(db, entity_id): """ Make an EntityRef by looking up the entity_id in the database. entity_id -> EntityRef(<entity_id>, <entity_type>, <entity_name>) """ # TODO: Include entity names in change_params, so that we don't have to # look them up. constants = Factory.get("Constants")(db) entity = Factory.get("Entity")(db) entity_ident = entity_type = None # Lookup type try: ent = entity.get_subclassed_object(id=entity_id) entity_type = six.text_type(constants.EntityType(ent.entity_type)) try: namespace = constants.ValueDomain( ENTITY_TYPE_NAMESPACE.get(entity_type, None)) entity_ident = ent.get_name(namespace) except (AttributeError, TypeError, NotFoundError): pass # Handling ValueError here is a hack for handling entities that can't # be accessed trough entity.get_subclassed_object() except (NotFoundError, ValueError): pass # We *have* entity_id, might have entity_type, and if so, may also have # an entity_ident. return EntityRef( entity_id, entity_type, entity_ident or six.text_type(entity_id))
def get_entity_name(entity_id): try: (_, en, en_type) = get_entity_type(entity_id) from cereconf import ENTITY_TYPE_NAMESPACE namespace = constants.ValueDomain( ENTITY_TYPE_NAMESPACE.get(en_type, None)) return en.get_name(namespace) except (AttributeError, TypeError, Errors.NotFoundError): return None
def get_entity_name(entity_id): try: (_, en, en_type) = get_entity_type(entity_id) from cereconf import ENTITY_TYPE_NAMESPACE namespace = constants.ValueDomain( ENTITY_TYPE_NAMESPACE.get(en_type, None)) return en.get_name(namespace) except (AttributeError, TypeError, Errors.NotFoundError): return None
def get_entity_name(db, entity_id): """Fetch the entity name associated with an id.""" try: ent = get_entity(db, entity_id) from cereconf import ENTITY_TYPE_NAMESPACE namespace = ent.const.ValueDomain( ENTITY_TYPE_NAMESPACE.get( text_type(ent.const.EntityType(ent.entity_type)), None)) return ent.get_name(namespace) except (AttributeError, TypeError, Errors.NotFoundError): return None
def get_entity_name(db, entity_id): """Fetch the entity name associated with an id.""" try: ent = get_entity(db, entity_id) from cereconf import ENTITY_TYPE_NAMESPACE namespace = ent.const.ValueDomain( ENTITY_TYPE_NAMESPACE.get( text_type(ent.const.EntityType(ent.entity_type)), None)) return ent.get_name(namespace) except (AttributeError, TypeError, Errors.NotFoundError): return None