"""Logic for Release Spatial.""" from oto import response from product_digital.constants import error from product_digital.models import ows_product from product_digital.models import product_additional_upc from product_digital.models import release_spatial def get_release_spatial(product_id): """Get spatial UPC data for a release. Args: product_id (int): release primary key Returns: Response: release spatial data or error """ return release_spatial.get_release_spatial(product_id) def delete_release_spatial(product_id): """Delete the spatial UPC record for a release. Args: product_id (int): release primary key Returns: Response: success or not found error """ return release_spatial.delete_release_spatial(product_id) def create_release_spatial(product_id, data): """Create a new spatial UPC record for a release. If no UPC is provided, reuse the product's existing additional UPC, or provision a new one if it has none. Args: product_id (int): release primary key data (dict): spatial data, optionally containing spatial_upc Returns: Response: release spatial data or error """ upc = data.get('upc') if not upc: # Reuse the product's permanent additional UPC if it already has one, so a # re-add doesn't reserve a fresh UPC from the pool. existing_response = product_additional_upc.get_atmos_upc(product_id) if not existing_response: return existing_response upc = existing_response.message if not upc: upc_response = ows_product.get_provisioned_upc(mark_used=True) if not upc_response: return response.create_error_response( code=error.INTERNAL_ERROR, message=error.ERROR_MESSAGE_UPC_FETCH_FAILED, status=500) upc = upc_response.message return release_spatial.create_release_spatial( {'release_id': product_id, 'upc': upc})