"""Logic for Physical Product Change History. Perform CRUD operations against the physical product change history schema. """ from oto.response import create_error_response from oto.response import Response from ows_product_physical import config from ows_product_physical.constant import error from ows_product_physical.constant import field from ows_product_physical.logic import ownership from ows_product_physical.models import persister from ows_product_physical.utils import image_util from ows_product_physical.validation import validation from ows_product_physical.validation.validation import json_validator POST_PRODUCT_CHANGE_HISTORY_VALIDATOR = json_validator( config.POST_PRODUCT_CHANGE_HISTORY_SCHEMA) def create_price_change(product_id, fields): """Create a history record for a price change. Args: product_id (int): product_id fields (dict): containing the post request JSON payload as a dict Return: Response: the response of the create operation """ new_price = fields.get('new_price') store_id = fields.get('store_id') history_response = persister.create_history( product_id=product_id, field_name=field.WHOLESALE_PRICE, new_price=new_price, store_id=store_id) if not history_response: return create_error_response( code=error.BAD_REQUEST_ERROR, message='bad request', status=500) return Response(status=201) def create_artwork_change(product_id, fields): """Helper to create a history record for artwork updates. Args: product_id (int): product id fields (dict): containing the post request json payload as a dict Return: Response: the response of the create operation """ upc = fields.get('upc') upc_result = persister.get_upc_by_product_id(product_id) if not upc_result: return upc_result result_upc = upc_result.message if not result_upc == upc: return create_error_response( code=error.BAD_REQUEST_ERROR, message='bad request', status=400) artwork_url = image_util.get_asset_path().format(upc=result_upc) history_response = persister.create_history( product_id=product_id, field_name=field.ARTWORK, artworkpath=artwork_url) if not history_response: return create_error_response( code=error.BAD_REQUEST_ERROR, message='bad request', status=400) return Response(status=201) def create_sale_start_date_change(product_id, fields): """Create a history record for a sale_start_date change. Args: product_id (int): product_id fields (dict): containing the post request JSON payload as a dict Return: Response: the response of the create operation """ old_sale_start_date = fields.get('old_sale_start_date') new_sale_start_date = fields.get('new_sale_start_date') store_id = fields.get('store_id') history_response = persister.create_history( product_id=product_id, field_name=field.SALE_START_DATE, old_sale_start_date=old_sale_start_date, store_id=store_id, new_sale_start_date=new_sale_start_date) if not history_response: return create_error_response( code=error.BAD_REQUEST_ERROR, message='bad request', status=500) return Response(status=201) def create_embargo_date_change(product_id, fields): """Create a history record for a embargo_date change. Args: product_id (int): product_id fields (dict): containing the post request JSON payload as a dict Return: Response: the response of the create operation """ old_embargo_date = fields.get('old_embargo_date') new_embargo_date = fields.get('new_embargo_date') store_id = fields.get('store_id') history_response = persister.create_history( product_id=product_id, field_name=field.EMBARGO_DATE, old_embargo_date=old_embargo_date, store_id=store_id, new_embargo_date=new_embargo_date) if not history_response: return create_error_response( code=error.BAD_REQUEST_ERROR, message='bad request', status=500) return Response(status=201) def create_release_date_change(product_id, fields): """Create a history record for a release_date change. Args: product_id (int): product_id fields (dict): containing the post request JSON payload as a dict Return: Response: the response of the create operation """ old_release_date = fields.get('old_release_date') new_release_date = fields.get('new_release_date') store_id = fields.get('store_id') history_response = persister.create_history( product_id=product_id, field_name=field.RELEASE_DATE, old_release_date=old_release_date, store_id=store_id, new_release_date=new_release_date) if not history_response: return create_error_response( code=error.BAD_REQUEST_ERROR, message='bad request', status=500) return Response(status=201) def create(product_id, account_type=None, account_id=None, **fields): """Create history handler. Args: product_id (int): product id account_type (str|None): a grass header parameter (subaccount|vendor) account_id (str|None): a grass header parameter fields (dict): containing the post request json payload as a dict Return: Response: the response of the create operation """ result = persister.get_product_by_id(product_id) if not result: return result release_status = result.message.get(field.RELEASE_STATUS) if release_status != field.IN_CONTENT: return create_error_response( status=400, code=error.RELEASE_STATUS_ERROR, message=error.RELEASE_IN_CONTENT_MESSAGE) project_id = result.message.get(field.PROJECT_ID) ownership_response = ownership.check_project_ownership( project_id, account_type, account_id) if not ownership_response: return ownership_response validation_response = validation.validate( fields, POST_PRODUCT_CHANGE_HISTORY_VALIDATOR) if not validation_response: return create_error_response( code=error.VALIDATION_ERROR, message=validation_response.errors.get('message')) field_name = fields.get('field_name') if field_name == field.ARTWORK: artwork_history_change = create_artwork_change(product_id, fields) return artwork_history_change elif field_name == field.WHOLESALE_PRICE: wholesale_price_change = create_price_change(product_id, fields) return wholesale_price_change return Response(status=200)