"""Module that contains product-level asset operations.""" from oto import response from assets.constants import asset_legacy from assets.constants import asset_types from assets.constants import error from assets.logic import image_location from assets.logic.legacy import stream_info from assets.models import ows_product from assets.models.legacy import import_asset from assets.models.legacy import asset_received_queue from assets.models.legacy import asset_type def _get_asset_url(asset_info, product_id, user_info): """Return asset url info (stream url or image url). Args: asset_info(dict): Dictionary with asset info from the model. product_id(int): Product id. user_info(str): String with orchard user info. Returns: response.Response: Response with asset url info or error. """ import_asset_type = asset_info['import_asset']['asset_type'] if (import_asset_type == asset_types.TYPE_IMPORT_AUDIO): url_response = stream_info.get_stream( asset_info['import_asset_detail']['destination_track_id'], user_info) return url_response url_response = image_location.get_image_location( product_id, 'product', 'cover') if not url_response: return url_response return response.Response({'url': url_response.message}) def _format_asset(asset_info): """Return properly formatted asset item for response. Args: asset_info(dict): Dictionary with asset info from the model. Returns: dict: Properly formatted asset data """ result = { 'filename': asset_info['import_asset']['filename'], 'asset_type': asset_info['import_asset']['asset_type'], 'status': asset_info['import_asset']['status'], } if (result['asset_type'] == asset_types.TYPE_IMPORT_AUDIO): result['track_unique_id'] = ( asset_info['import_asset_detail']['destination_track_id']) return result def _find_asset_received_by_import_asset_id(queue_items, import_asset_id): """Get asset received queue item by import_asset_id. Args: queue_items (dict): Assets received queue info from database import_asset_id (int): Import asset id. Returns: dict: Asset received queue item or None """ for item in queue_items: if item['asset_received_item']['import_asset_id'] == import_asset_id: return item return None def _get_import_asset_type(asset_type_id): """Return import asset type by asset type id. Args: asset_type_id (int): Asset type id. Returns: response.Response: Import asset type info or error. """ asset_type_response = asset_type.get_asset_type_by_id(asset_type_id) if not asset_type_response: return asset_type_response import_asset_type = import_asset.convert_filetype( asset_type_response.message['asset']) if import_asset_type is None: return response.create_error_response( code=error.ERROR_CODE_WRONG_VALUE, message=error.ERROR_WRONG_ASSET_TYPE) return response.Response(import_asset_type) def get_product_assets(product_id, user_info): """Return info about all product assets. Args: product_id(int): Product Id. user_info(str): String with orchard user info. Returns: response.Response: Response object with asset info or error message. """ product_response = ows_product.get_product_by_id(product_id) if not product_response: return product_response product = product_response.message asset_received_response = asset_received_queue.get_assets_by_upc( product['upc']) if not asset_received_response: return asset_received_response asset_received_items = asset_received_response.message ids = [ item['asset_received_item']['import_asset_id'] for item in asset_received_items] import_assets_response = import_asset.get_assets_by_ids(ids) if not import_assets_response: return import_assets_response processed_assets = [] assets = [] for asset in import_assets_response.message: asset_received_item = _find_asset_received_by_import_asset_id( asset_received_items, asset['import_asset']['id']) if asset_received_item is None: continue track_id = asset_received_item['asset_received_details']['track_id'] if track_id in processed_assets: continue processed_assets.append(track_id) asset['import_asset_detail']['destination_track_id'] = track_id asset_type_response = _get_import_asset_type( asset_received_item['asset_received_item']['asset_type_id']) if not asset_type_response: return asset_type_response asset['import_asset']['asset_type'] = asset_type_response.message asset_record = _format_asset(asset) if asset_record['status'] == asset_legacy.STATUS_FINISHED: asset_url_response = _get_asset_url( asset, product_id, user_info) if not asset_url_response: return asset_url_response asset_record.update(asset_url_response.message) assets.append(asset_record) return response.Response({ 'product_id': product_id, 'upc': product['upc'], 'assets': assets })