"""Logic for Validating assets.""" from oto import response from assets.constants import error from assets.constants import success from assets.constants import asset_legacy from assets.models import ows_product from assets.models.legacy import asset def _is_bit_depth_uniform(upc, physical_location_id, asset_type_id): """Check for bit depth value for each of the asset for release. Args: upc (int): upc to check for valid release. physical_location_id (int): original physical location of asset to check. asset_type_id (int): asset type to check. Returns: Response: validation message and code for validating bit-depth values of all assets for provided upc. """ asset_type_error_correction_list = [ asset_legacy.ASSET_TYPE_WAV, asset_legacy.ASSET_TYPE_WAV_CORRECTION] if int(asset_type_id) in asset_type_error_correction_list: asset_type_list = asset_type_error_correction_list else: asset_type_list = [asset_type_id] bitdepth_track_data = asset.get_bits_per_sample_data_for_tracks( upc, physical_location_id, asset_type_list) if not bitdepth_track_data: return response.Response( message=bitdepth_track_data.errors['message'], status=bitdepth_track_data.status) error_correction_files = [] for row in bitdepth_track_data.message: if row['asset_type_id'] == asset_legacy.ASSET_TYPE_WAV_CORRECTION: error_correction_files.append(row['filename']) bit_depth_list = [] for row in bitdepth_track_data.message: if (row['filename'] not in error_correction_files or (row['asset_type_id'] == asset_legacy.ASSET_TYPE_WAV_CORRECTION)): bit_depth_list.append(row['bits_per_sample']) count_for_bits_per_sample = len(set(bit_depth_list)) if count_for_bits_per_sample == 1: return response.Response( message=success.SAME_BITS_PER_SAMPLE_FOUND, status=200) return response.Response( message=error.MIXED_BITS_PER_SAMPLE_FOUND, status=400) def validate_bit_depth_by_upc(product_id, physical_location_id, asset_type_id): """Validate bit depth value for all assets for provided UPC. Args: product_id (Integer): Unique identification for product. physical_location_id (int): original physical location of asset to check. asset_type_id (int): asset type to check. Returns: Response: Bit depth validation messages for assets if any. """ ows_product_details = ows_product.get_product_by_id(product_id) if not ows_product_details: return response.Response( message=ows_product_details.errors.get('message'), status=ows_product_details.status) upc = ows_product_details.message['upc'] # Validate bit depth values for all assets. bit_depth_response = _is_bit_depth_uniform( upc, physical_location_id, asset_type_id) return response.Response( message=bit_depth_response.message, status=bit_depth_response.status) def _is_desired_bit_depth_found( upc, desired_bit_depth, physical_location_id, asset_type_id): """Check for desired bit depth value. Args: upc (int): upc to check for valid release. desired_bit_depth(Interger): Desired bit depth value to match with. physical_location_id (int): original physical location of asset to check. asset_type_id (int): asset type to check. Returns: Response: validation message and code for validating bit-depth values of all assets for provided upc. """ bitrates_list = asset.get_bitrates_for_upc( upc, physical_location_id, asset_type_id) if not bitrates_list: return response.Response( message=bitrates_list.errors.get('message'), status=bitrates_list.status) if len(bitrates_list.message) != 1 or \ int(bitrates_list.message[0]) != int(desired_bit_depth): return response.Response( message=error.DESIRED_BIT_DEPTH_NOT_FOUND, status=400) return response.Response( message=success.DESIRED_BIT_DEPTH_FOUND, status=200) def check_for_desired_bit_depth( product_id, desired_bit_depth, physical_location_id, asset_type_id): """Check for desired bit depth for provided product. Args: product_id (Integer): Unique identification for product. desired_bit_depth (Integer): Desired bit depth to match. physical_location_id (int): original physical location of asset to check. asset_type_id (int): asset type to check. Returns: Response: Bit depth validation messages for assets if any. """ ows_product_details = ows_product.get_product_by_id(product_id) if not ows_product_details: return response.Response( message=ows_product_details.errors.get('message'), status=ows_product_details.status) upc = ows_product_details.message['upc'] # Validate bit depth values for all assets. bit_depth_response = _is_desired_bit_depth_found( upc, desired_bit_depth, physical_location_id, asset_type_id) return response.Response( message=bit_depth_response.message, status=bit_depth_response.status)