"""Model for the entities.""" from oto import response from owsrequest import request from marketing.utils import json # Validators ENTITY_VALIDATOR = json.create_draft_validator_from_model('entity') ENTITY_TYPES = json.get_enum_from_draft(ENTITY_VALIDATOR, 'entity') # Resources PROJECT_TYPE = 'project' PRODUCT_TYPE = 'product' RELEASE_TYPE = 'release' PRODUCT_SERVICE = 'ows-product' PROJECT_SERVICE = 'ows-project-manager' PRODUCT_OWNERSHIP_RESOURCE = ( '/{account_type}/{account_id}/product/{product_id}') PRODUCT_GETTER_RESOURCE = '/product/{product_id}' PROJECT_GETTER_RESOURCE = '/project/{project_id}' NORMALIZED_ENTITY_TYPES = {PRODUCT_TYPE: RELEASE_TYPE} def is_product_owner(product_id, account_type, account_id): """Check ownership of a product for a specific vendor. Args: product_id (int): the product id. account_type (int): the account's type. account_id (int): the account's id. Returns: Response: the result of the ownership check. """ resource = PRODUCT_OWNERSHIP_RESOURCE.format( account_type=account_type, account_id=account_id, product_id=product_id) ownership = request.head(PRODUCT_SERVICE, resource) return response.Response(status=ownership.status_code) def is_project_owner(project_id, account_type, account_id): """Check ownership of a project for a specific vendor or subaccount. Args: project_id (int): the project id. account_type (int): the account's type. account_id (int): the account's id. Returns: Response: the result of the ownership check. """ route = '/ownership/{account_type}/{account_id}/project/{project_id}' resource = route.format( account_type=account_type, account_id=account_id, project_id=project_id) ownership = request.head(PROJECT_SERVICE, resource) return response.Response(status=ownership.status_code) def get_product_by_id(product_id): """Get product by id. Args: product_id (int): the product id. Returns: Response: the product information. """ resource = PRODUCT_GETTER_RESOURCE.format(product_id=product_id) product = request.get(PRODUCT_SERVICE, resource) if product.status_code == 200: return response.Response( product.json(), status=product.status_code) return response.Response( errors=product.json(), status=product.status_code) def get_project_by_id(project_id): """Get project by id. Args: project_id (int): the project id. Returns: Response: the project information. """ resource = PROJECT_GETTER_RESOURCE.format(project_id=project_id) project = request.get(PROJECT_SERVICE, resource) if project.status_code == 200: return response.Response( project.json(), status=project.status_code) return response.Response( errors=project.json(), status=project.status_code) def get_normalized_entity_type(entity_type): """Get a normalized entity type. Args: entity_type (str): the entity type. Returns: str: the normalized entity type. """ return NORMALIZED_ENTITY_TYPES.get(entity_type, entity_type)