"""Module that contains track-level asset operations.""" from oto import response from assets import config from assets.constants import error from assets.constants import asset_types from assets.constants import asset_legacy from assets.logic.legacy import asset_filename from assets.logic.legacy import asset_received from assets.logic.legacy import import_asset from assets.logic.legacy import asset_location from assets.models import ows_track from assets.models import ows_product from assets.models.legacy import asset from assets.models.legacy import asset_type def copy_track_asset(from_tuid, to_tuid, full_user_id): """Create a copy of the track assets. Copy assets from track with unique id to track with unique id Args: from_tuid (int): source track id. to_tuid (int): destination track id. full_user_id (str): Full user id. Returns: response.Response: Response object with success or error message. """ if not full_user_id: return response.create_error_response( code=error.ERROR_CODE_WRONG_VALUE, message=error.ERROR_MESSAGE_USER_ID_IS_REQUIRED) source_asset_data_response = _prepare_source_track(from_tuid) if not source_asset_data_response: return source_asset_data_response source_asset_data = source_asset_data_response.message source_filename = source_asset_data['source_filename'] from_upc = source_asset_data['source_upc'] from_product = source_asset_data['product'] from_track = source_asset_data['track'] destination_asset_data_response = _prepare_destination_track( to_tuid) if not destination_asset_data_response: return destination_asset_data_response destination_asset_data = destination_asset_data_response.message destination_product = destination_asset_data['product'] if destination_product['status'] == asset_legacy.PRODUCT_STATUS_IN_CONTENT: return response.create_error_response( code=error.ERROR_CODE_WRONG_VALUE, message=error.ERROR_MESSAGE_PRODUCT_IN_CONTENT) import_asset_response = import_asset.create_import_asset_items( full_user_id=full_user_id, filename=source_filename, asset_type=asset_types.TYPE_IMPORT_AUDIO, upc=from_upc, track_unique_id=from_tuid, import_type=asset_legacy.IMPORT_TYPE_COPY, ) if not import_asset_response: return import_asset_response create_asset_items_body = import_asset_response.message import_asset_id = create_asset_items_body['import_asset_id'] track_file_path_response = _get_track_path( from_product, from_track, asset_types.TYPE_FILE_WAV) if not track_file_path_response: return track_file_path_response track_file_path_body = track_file_path_response.message uploaded_asset_filename = track_file_path_body['track_path'] to_upc = destination_asset_data['destination_upc'] destination_filename = destination_asset_data['destination_filename'] dd_items_response = asset_received.create_direct_delivery_items( asset_name=asset_types.TYPE_FILE_WAV, import_asset_id=import_asset_id, uploaded_asset_filename=uploaded_asset_filename, upc=to_upc, track_unique_id=to_tuid, destination_name=destination_filename, job_type=asset_legacy.IMPORT_TYPE_COPY, ) if not dd_items_response: return dd_items_response return response.Response({'status': error.SUCCESS_CODE}) def _prepare_source_track(unique_track_id): """Get file name and upc for source tracks asset. Args: unique_track_id (int): source track id. Returns: response.Response: Response object with file name and upc or error. """ track_and_product_response = _get_track_and_product_by_track_id( unique_track_id ) if not track_and_product_response: return track_and_product_response track = track_and_product_response.message['track'] product = track_and_product_response.message['product'] source_filename = asset_filename.get_track_filename(product, track) return response.Response({ 'source_upc': track['upc'], 'source_filename': source_filename, 'track': track, 'product': product }) def _prepare_destination_track(unique_track_id): """Get file name and upc for destination tracks asset. Args: unique_track_id (int): destination track id. Returns: response.Response: Response object with file name and upc or error. """ track_and_product_response = _get_track_and_product_by_track_id( unique_track_id ) if not track_and_product_response: return track_and_product_response to_track = track_and_product_response.message['track'] to_product = track_and_product_response.message['product'] destination_filename = asset_filename.get_track_filename( to_product, to_track, asset_types.TYPE_FILE_WAV) return response.Response({ 'destination_upc': to_track['upc'], 'destination_filename': destination_filename, 'track': to_track, 'product': to_product }) def _get_track_and_product_by_track_id(unique_track_id): """Get the track and product by track id. Args: unique_track_id: Unique track id Returns: response.Response: Response with track and product object or error. """ track_response = ows_track.get_track_by_id(unique_track_id) if not track_response: return track_response track = track_response.message product_response = ows_product.get_product_by_id(track['product_id']) if not product_response: return product_response product = product_response.message return response.Response({'track': track, 'product': product}) def _get_track_path(product, track, asset_type_name): """Get the path to the track. Args: product (dict): Dictionary with product info. track (dict): Dictionary with track info. asset_type_name (str): Asset type Returns: response.Response: Response with the path to the track or error. """ track_type_response = asset_type.get_asset_type_by_name(asset_type_name) if not track_type_response: return track_type_response track_type_id = track_type_response.message['id'] track_filename_search_string = asset_filename.get_track_search_filename( product, track) stream_data_response = asset.get_streaming_info( product['upc'], track_type_id, config.SERVER_LOCATION_AVL, track_filename_search_string) if not stream_data_response: return stream_data_response stream_data = stream_data_response.message track_path = asset_location.get_path( stream_data['local_ip'], stream_data['initial_folder'], stream_data['folder_structure'], {'upc': str(stream_data['upc'])}, stream_data['filename'], stream_data['path_structure_type']) path_response = {'track_path': track_path} return response.Response(path_response) def copy_product_track_assets( tracks_mapping, from_product, to_product, full_user_id, asset_batch_data=None): """Create a copy of the product track assets. Args: tracks_mapping (dict): Source and destination track id mapping. from_product (dict): Source product data. to_product (dict): Destination product data. full_user_id (str): Vendor contact id. asset_batch_data (dict): Asset batch info. Returns: response.Response: Response object with success or error message. """ for from_tuid, to_track_data in tracks_mapping.items(): from_track = { 'tuid': from_tuid, 'volume_number': to_track_data['volume'], 'track_number': to_track_data['number'] } to_track = { 'tuid': to_track_data['to_tuid'], 'volume_number': to_track_data['volume'], 'track_number': to_track_data['number'] } source_filename = asset_filename.get_track_filename( from_product, from_track, asset_types.TYPE_FILE_WAV) destination_filename = asset_filename.get_track_filename( to_product, to_track, asset_types.TYPE_FILE_WAV) import_asset_response = import_asset.create_import_asset_items( full_user_id=full_user_id, filename=source_filename, asset_type=asset_types.TYPE_IMPORT_AUDIO, upc=from_product['upc'], track_unique_id=from_track['tuid'], import_type=asset_legacy.IMPORT_TYPE_COPY, import_asset_batch_body=asset_batch_data ) if not import_asset_response: return import_asset_response create_asset_items_body = import_asset_response.message import_asset_id = create_asset_items_body['import_asset_id'] track_file_path_response = _get_track_path( from_product, from_track, asset_types.TYPE_FILE_WAV) if not track_file_path_response: return track_file_path_response track_file_path_body = track_file_path_response.message uploaded_asset_filename = track_file_path_body['track_path'] dd_items_response = asset_received.create_direct_delivery_items( asset_name=asset_types.TYPE_FILE_WAV, import_asset_id=import_asset_id, uploaded_asset_filename=uploaded_asset_filename, upc=to_product['upc'], track_unique_id=to_track['tuid'], destination_name=destination_filename, job_type=asset_legacy.IMPORT_TYPE_COPY, ) if not dd_items_response: return dd_items_response return response.Response({'status': error.SUCCESS_CODE}) def _get_image_path(upc, asset_type_name): """Get the path to the image product asset. Args: upc (int): Product upc asset_type_name (str): Asset type Returns: response.Response: Response with the path to the track or error. """ image_type_response = asset_type.get_asset_type_by_name(asset_type_name) if not image_type_response: return image_type_response image_type_id = image_type_response.message['id'] image_filename_template = asset_filename.get_image_filename( {'upc': upc}, asset_types.TYPE_FILE_TIF) stream_data_response = asset.get_streaming_info( upc, image_type_id, config.SERVER_LOCATION_AVL, image_filename_template, strict=True) if not stream_data_response: return stream_data_response stream_data = stream_data_response.message image_path = asset_location.get_path( stream_data['local_ip'], stream_data['initial_folder'], stream_data['folder_structure'], {'upc': str(stream_data['upc'])}, stream_data['filename'], stream_data['path_structure_type']) path_response = {'image_path': image_path} return response.Response(path_response) def copy_product_image_asset( from_upc, to_upc, full_user_id, asset_batch_data=None): """Copy product image asset. Args: from_upc (int): Source product upc. to_upc (int): Destination product upc. full_user_id (str): Full user id. asset_batch_data (dict): Data for import_asset_batch relation. Returns: response.Response: Response with success or error message. """ source_filename = asset_filename.get_image_filename( {'upc': from_upc}, asset_types.TYPE_FILE_TIF) destination_filename = asset_filename.get_image_filename( {'upc': to_upc}, asset_types.TYPE_FILE_TIF) import_asset_response = import_asset.create_import_asset_items( full_user_id=full_user_id, filename=source_filename, asset_type=asset_types.TYPE_IMPORT_IMAGE, upc=from_upc, track_unique_id=0, import_type=asset_legacy.IMPORT_TYPE_COPY, import_asset_batch_body=asset_batch_data ) if not import_asset_response: return import_asset_response create_asset_items_body = import_asset_response.message import_asset_id = create_asset_items_body['import_asset_id'] image_file_path_response = _get_image_path( from_upc, asset_types.TYPE_FILE_TIF ) if not image_file_path_response: return image_file_path_response track_file_path_body = image_file_path_response.message uploaded_asset_filename = track_file_path_body['image_path'] dd_items_response = asset_received.create_direct_delivery_items( asset_name=asset_types.TYPE_FILE_TIF, import_asset_id=import_asset_id, uploaded_asset_filename=uploaded_asset_filename, upc=to_upc, track_unique_id=0, destination_name=destination_filename, job_type=asset_legacy.IMPORT_TYPE_COPY, ) if not dd_items_response: return dd_items_response return response.Response({'status': error.SUCCESS_CODE})