"""Logic for entities.""" from oto import response from oto import status from marketing.constants import error from marketing.models import entities def is_owner(entity_type, entity_id, account_type=None, account_id=None): """Verify ownership of an entity. Args: entity_type (str): the entity type. entity_id (str): the entity id. account_type (str) : the account_type. account_id (str): the account information. Returns: Response: whether or not a user is owner of this entity. """ entity_type = entities.get_normalized_entity_type(entity_type) if entity_type == entities.RELEASE_TYPE: return entities.is_product_owner(entity_id, account_type, account_id) if entity_type == entities.PROJECT_TYPE: return entities.is_project_owner(entity_id, account_type, account_id) return response.create_error_response( code=error.ERROR_CODE_VALIDATION, message=error.ERROR_MESSAGE_UNSUPPORTED_PRODUCT.format(entity_type), status=status.BAD_REQUEST) def verify_existence(entity_type, entity_id): """Verify if an entity exists. Args: entity_type (str): an entity type. entity_id (str): an entity id. Returns: Response: whether the product exists or not. """ entity_type = entities.get_normalized_entity_type(entity_type) if entity_type == entities.RELEASE_TYPE: return entities.get_product_by_id(entity_id) if entity_type == entities.PROJECT_TYPE: return entities.get_project_by_id(entity_id) return response.create_error_response( code=error.ERROR_CODE_VALIDATION, message=error.ERROR_MESSAGE_UNSUPPORTED_PRODUCT.format(entity_type), status=status.BAD_REQUEST)