"""Artist formatting logic.""" from content_utils.exceptions import IneligibleEventError from owsrequest import request from src import config from src import constants def get_current_product_data(product_id): """Get current product artists.""" ows_response = request.process( config.APPLICATION_NAME, config.ENVIRONMENT, 'GET', constants.SERVICE_OWS_PRODUCT_DIGITAL, f'/product/audio/{product_id}' ) if ows_response.status_code == 404: raise IneligibleEventError('Release not found.') if ows_response.status_code != 200: raise Exception('ows-product-digital service error.', ows_response.text) return ows_response.json() def format_artists(product_id, release_correction_changes, artist_changes): """Format artists.""" if not artist_changes: return current_product = get_current_product_data(product_id) role_list = _get_role_list_for_product( release_correction_changes, current_product['genre_id'], current_product['subgenre_id'] ) return _format_all_artists(artist_changes, current_product['product_artists'], role_list) def _get_role_list_for_product(release_correction_changes, current_genre, current_subgenre): latest_genre = release_correction_changes.get(constants.FIELD_GENRE_ID, current_genre) latest_subgenre = release_correction_changes.get( 'subgenre_id', current_subgenre) if latest_genre == constants.GENRE_ID_CLASSICAL: return constants.CLASSICAL_ARTISTS elif latest_subgenre in constants.SUBGENRE_WITH_COMPOSER_IDS: return constants.STANDARD_ARTISTS_AND_COMPOSER return constants.STANDARD_ARTISTS def _format_all_artists(artist_changes, current_artists, role_list): result = [] for role in role_list: if role in artist_changes: result.extend(artist_changes[role]) for artist in current_artists: artist_role = artist['role'] if artist['role'] != 'primary_artist' else 'performer' if artist_role in role_list and artist_role not in artist_changes: result.append(artist) return result