"""Logic for UPCs.""" from oto import response from product.constants import error from product.models import product as product_model from product.models import upc as upc_model from product.models import upc_provisioner def upc_exists(upc): """Check if the given UPC exists in our datastore. Args: upc (int): UPC value for which to check. Returns: response.Response: containing bool for whether UPC was found. """ upc_response = upc_model.get_upcs_by_values([upc]) if upc_response.status == 500: return upc_response if upc_response: return response.Response(message=True) product_response = product_model.get_product_by_upc(upc) if product_response: return response.Response(message=True) return response.Response(message=False, status=404) def upc_available(upc): """Check if the given upc is available for assignment. Args: upc (int): UPC value for which to check. Returns: response.Response: containing status 200 if UPC available. """ if not upc: return True upc_response = upc_model.get_upcs_by_values([upc]) if upc_response.status == 500: return upc_response if upc_response: return response.Response(status=403) product_response = product_model.filter_upcs_with_existing_products([upc]) if product_response.message and upc in product_response.message: return response.Response(status=403) return response.Response(status=200) def upcs_available(upcs): """Check if a given list of UPCs are available for assignment. Args: upcs (int[]): UPC values to check Returns: response.Response: containing payload of UPCs that were available. """ if not upcs: return response.Response(status=200, message={'available_upcs': []}) upc_response = upc_model.get_upcs_by_values(upcs) if upc_response.status == 500: return upc_response product_response = product_model.filter_upcs_with_existing_products(upcs) if product_response.status == 500: return product_response upc_matches = [ upc_match['upc'] for upc_match in upc_response.message if upc_match['upc'] in upcs ] product_matches = product_response.message return response.Response(status=200, message={ 'available_upcs': list(set(upcs) - set(upc_matches) - set(product_matches)) }) def is_orchard_upc(upc): """Check if the given UPC is an Orchard UPC. Args: upc (int): UPC value for which to check. Returns: response.Response: containing result of check. """ upc_response = upc_model.get_upcs_by_values([upc]) if upc_response.status == 500: return upc_response return response.Response(message={'is_orchard_upc': bool(upc_response)}) def retrieve_upc(mark_used): """Retrieve a UPC using the UPC Provisioner. Update UPC status to used when mark_used is True. Args: mark_used (boolean): mark_used boolean parameter for which to check. Returns: response.Response: containing retrieved upc. """ upc_provisioner_response = upc_provisioner.retrieve_upc() if not upc_provisioner_response: return upc_provisioner_response if mark_used: upc_value = int(upc_provisioner_response.message['upc']) upc_set_to_used = upc_provisioner.set_claimed_upc_to_used(upc_value) if not upc_set_to_used: error_response = _status_update_error() return error_response return upc_provisioner_response def _status_update_error(): """Log status update failure and return response containing status 500.""" upc_provisioner.log_upc_status_info() response_object = response.create_error_response( code=error.ERROR_CODE_FAILED_UPC_STATUS_UPDATE, message=error.ERROR_MESSAGE_UPC_STATUS_NOT_UPDATED, status=500) return response_object