"""Logic for Physical Products Distribution. Perform CRUD operations against the product_dstribution schema. """ from oto import response from ows_product_physical.constant import error from ows_product_physical.models import ows_product from ows_product_physical.models import persister from ows_product_physical.models import product_distribution def fetch_product_distribution(product_id, account_type=None, account_id=None): """Retrieves list of product distribution objects for given product_id. Args: product_id (int): primary key for the product we want to fetch. account_type (str): a grass header parameter (subaccount|vendor). account_id (str): a grass header parameter. Return: Response: List of product distribution objects for given product_id. """ product_response = persister.get_product_by_id(product_id) if not product_response: return product_response if account_type and account_id: ownership_check = ows_product.check_product_ownership( product_id, account_type, account_id) if not ownership_check.message: return response.create_error_response( message=error.OWNERSHIP_ERROR_MESSAGE.format(account_type), status=error.FORBIDDEN_CODE, code=error.OWNERSHIP_ERROR) product_distribution_response = \ product_distribution.get_product_distribution(product_id) return product_distribution_response def delete_product_distribution( product_id, account_type=None, account_id=None): """Delete all the existing product distribution for given product_id. Args: product_id (int): primary key for the product account_type (str): a grass header parameter (subaccount|vendor). account_id (str): a grass header parameter. Returns: Response: A response object with the result of the delete request """ product_response = persister.get_product_by_id(product_id) if not product_response: return product_response if account_type and account_id: ownership_check = ows_product.check_product_ownership( product_id, account_type, account_id) if not ownership_check.message: return response.create_error_response( message=error.OWNERSHIP_ERROR_MESSAGE.format(account_type), status=error.FORBIDDEN_CODE, code=error.OWNERSHIP_ERROR) product_distribution_response = \ product_distribution.get_product_distribution(product_id) if not product_distribution_response.message['items']: return response.create_error_response( message=error.PRODUCT_DISTRIBUTION_NOT_FOUND_MESSAGE, status=error.NOT_FOUND, code=error.NOT_FOUND_MESSAGE) return product_distribution.delete_product_distribution(product_id) def save_product_distribution( product_id, request_data, user, account_type=None, account_id=None): """Save product distribution data for product_id. Args: product_id (int): Unique id of product. request_data (dict): contains product_distribution_id and product_id. user (string): orchard_user_id. account_type (string): a grass header parameter (subaccount|vendor). account_id (int): a grass header parameter. Returns: Response: Response object """ if not user: return response.create_error_response( code=error.ERROR_CODE_MISSING_ORCHARD_USER_ID, message=error.ERROR_MESSAGE_MISSING_ORCHARD_USER_ID) if not request_data.get('distribute_to'): return response.create_error_response( code=error.ERROR_CODE_MISSING_DISTRIBUTE_TO, message=error.ERROR_MESSAGE_MISSING_DISTRIBUTE_TO) product_response = persister.get_product_by_id(product_id) if not product_response: return product_response if account_type and account_id: ownership_check = ows_product.check_product_ownership( product_id, account_type, account_id) if not ownership_check.message: return response.create_error_response( message=error.OWNERSHIP_ERROR_MESSAGE.format(account_type), status=error.FORBIDDEN_CODE, code=error.OWNERSHIP_ERROR) user_type, user_id = user.split(':') return product_distribution.set_product_distribution( product_id, request_data, user_type, user_id)