"""ISRC model.""" from collections import OrderedDict from backend.connectors import mysql from backend.models.sql import isrc as isrc_sql @mysql.db_session_wrap def get_isrc_usage_stats(product_id, session): """Get ISRC usage statistics. Return tracks (of in_content products) that share the same ISRCs as tracks of product identified by product_id. """ db_query_result_cursor = session.execute( isrc_sql.ISRC_USAGE_STATS_QUERY, {'product_id': product_id}) db_query_results = [ OrderedDict([(key, getattr(row, key)) for key in row.keys()]) for row in db_query_result_cursor ] mutable_response = OrderedDict([ ('product', OrderedDict([ ('id', product_id), ('tracks', OrderedDict()) ])) ]) for db_query_result in db_query_results: build_json( mutable_response['product']['tracks'], db_query_result, ) finalize_response_structure(mutable_response) return mutable_response @mysql.db_session_wrap def get_tracks_with_isrc(isrc, limit, offset, session): """Get a paginated list of tracks with the same ISRC. Args: isrc (str): The ISRC to search for. limit (int): The number of tracks to return per page. offset (int): The offset to return a page of results from. Returns: dict: A standard pagination object with pagination.total_records and items[]. """ db_query_result_cursor = session.execute( isrc_sql.TRACKS_WITH_ISRC_QUERY, {'isrc': isrc}) db_query_results = [ OrderedDict([(key, getattr(row, key)) for key in row.keys()]) for row in db_query_result_cursor ] return { 'pagination': { 'total_records': len(db_query_results), 'type': None}, 'items': db_query_results[offset: limit] } def build_json( mutable_path, db_query_result, ): """Append formatted data from db_query_result to mutable_path.""" validatee_track_id = db_query_result['validatee_track_id'] matched_isrc_product_type = db_query_result['matched_isrc_product_type'] matched_isrc_product_id = db_query_result['matched_isrc_product_id'] matched_isrc_track_id = db_query_result['matched_isrc_track_id'] new_matched_isrc_track = OrderedDict([ ('id', matched_isrc_track_id), ('name', db_query_result['matched_isrc_track_name']), ]) new_matched_isrc_product = OrderedDict([ ('id', matched_isrc_product_id), ('name', db_query_result['matched_isrc_product_name']), ('tracks', OrderedDict([ (matched_isrc_track_id, new_matched_isrc_track) ])) ]) new_matched_isrc_product_type = OrderedDict([ ('products', OrderedDict([ (matched_isrc_product_id, new_matched_isrc_product) ])) ]) new_validatee_track = OrderedDict([ ('id', validatee_track_id), ('isrc', db_query_result['isrc']), ('isrc_usage', OrderedDict([ (matched_isrc_product_type, new_matched_isrc_product_type) ])), ]) isrc_usage_path = ( mutable_path .get(validatee_track_id, {}) .get('isrc_usage', {})) matched_isrc_products_path = ( isrc_usage_path .get(matched_isrc_product_type, {}) .get('products', {})) matched_isrc_tracks_path = ( matched_isrc_products_path .get(matched_isrc_product_id, {}) .get('tracks', {})) if not mutable_path.get(validatee_track_id): mutable_path[validatee_track_id] = new_validatee_track elif not isrc_usage_path.get(matched_isrc_product_type): isrc_usage_path[matched_isrc_product_type] = ( new_matched_isrc_product_type) elif not matched_isrc_products_path.get(matched_isrc_product_id): matched_isrc_products_path[matched_isrc_product_id] = ( new_matched_isrc_product) elif not matched_isrc_tracks_path.get(matched_isrc_track_id): matched_isrc_tracks_path[matched_isrc_track_id] = ( new_matched_isrc_track) def finalize_response_structure(mutable_path): """Convert mutable_path to a finalized response structure with counts.""" mutable_path['product']['tracks'] = list( mutable_path['product']['tracks'].values()) for track in mutable_path['product']['tracks']: isrc_usage = track['isrc_usage'] isrc_usage['count'] = 0 for product_type, products in isrc_usage.items(): if product_type == 'count': continue products['count'] = 0 products['products'] = list(products['products'].values()) for product in products['products']: product['tracks'] = list(product['tracks'].values()) num_tracks = len(product['tracks']) isrc_usage['count'] = isrc_usage['count'] + num_tracks isrc_usage[product_type]['count'] = ( isrc_usage[product_type]['count'] + num_tracks)