"""Logic used by index.py as well as release_correction.py.""" from typing import Dict, List import config from constants.constants import ARTIST_NAME from ddex_ingester_common.constants.ddex_providers import SME from ddex_ingester_common.constants.roles import ( DDEX_ARTIST_ROLE_REQUIRED_GENRES, DDEX_ARTIST_ROLE_TO_ORCHARD_ARTIST_TYPE, DDEX_DISPLAY_ARTIST_ROLE_TO_ORCHARD_PRODUCT_ARTIST, DDEX_PRODUCT_ARTIST_ROLE_REQUIRED_GENRES) from ddex_ingester_common.helpers.list import have_common_elements from ddex_ingester_common.helpers.metadata import get_language_id from ddex_ingester_common.logging import utils as logging_utils from ddex_ingester_common.models.s3.genre import Genre as S3Genre from ddex_ingester_common.models.s3.label_participant import LabelParticipant from ddex_ingester_common.models.s3.participant import ( Participant as S3Participant) from ddex_ingester_common.models.s3.product import ( Product as S3Product) from ddex_ingester_common.models.state_machine.body import ( Body as StateMachineContext) logger = logging_utils.get_logger(config.app_logger) def get_participations( display_artists: List[S3Participant], label_participants: List[LabelParticipant], genres: List[S3Genre], context: StateMachineContext, include_name=False) -> List[Dict]: """Build participations payload for create or update product mutation.""" if not display_artists: return [] # AWAL always maps every role regardless of genre skip_genre_check = context.ddex_provider != SME participations = [] for artist in display_artists: for role in artist.roles: if is_product_role(role, genres, skip_genre_check=skip_genre_check): # noqa: E501 participation = { 'labelParticipantUuid': retrieve_label_participants( label_participants, artist ).label_participant_uuid, 'role': DDEX_DISPLAY_ARTIST_ROLE_TO_ORCHARD_PRODUCT_ARTIST[role.lower()], 'localizations': get_artist_localizations(artist), } # Name is required for release corrections but not creating # when creating or updating products. if include_name: participation[ARTIST_NAME] = artist.name participations.append(participation) return participations def is_product_role( role: str, genres: List[S3Genre], skip_genre_check: bool = False) -> bool: """Check if a role is an artist role considering the product genres.""" # Check if it's an artist role if role.lower() not in DDEX_DISPLAY_ARTIST_ROLE_TO_ORCHARD_PRODUCT_ARTIST: return False if skip_genre_check: return True # Check if this role only applies to certain genres if role not in DDEX_PRODUCT_ARTIST_ROLE_REQUIRED_GENRES: return True required_genres = DDEX_PRODUCT_ARTIST_ROLE_REQUIRED_GENRES[role] genre_names = [genre.genre for genre in genres] # If any product genre matches one of the required genres the roles applies return have_common_elements(genre_names, required_genres) def is_track_role( role: str, genres: List[S3Genre], skip_genre_check: bool = False) -> bool: """Check if a role is an artist role considering the product genres.""" # Check if it's an artist role if role not in DDEX_ARTIST_ROLE_TO_ORCHARD_ARTIST_TYPE: return False if skip_genre_check: return True # Check if this role only applies to certain genres if role not in DDEX_ARTIST_ROLE_REQUIRED_GENRES: return True required_genres = DDEX_ARTIST_ROLE_REQUIRED_GENRES[role] genre_names = [genre.genre for genre in genres] # If any product genre matches one of the required genres the roles applies return have_common_elements(genre_names, required_genres) def get_artist_localizations(artist: S3Participant) -> List[Dict]: """Build localizations field for the participations payload.""" localizations = [] if artist.localized_names: for localized_name in artist.localized_names: localizations.append({ 'name': localized_name.name, 'languageId': get_language_id(localized_name.language_code), }) return localizations def get_title_localizations(s3_product: S3Product) -> List[Dict]: """Get localizations payload for create or update product.""" payload = [] if s3_product.localized_titles: for localized_title in s3_product.localized_titles: language_id = get_language_id(localized_title.language_code) new_title = { 'languageId': language_id, 'productName': localized_title.title } if localized_title.version: new_title['deliveredVersion'] = localized_title.version payload.append(new_title) return payload def retrieve_label_participants(label_participants, artist): """Retrieve project artist from artist list in S3 data.""" if label_participants and artist: for label_participant in label_participants: if label_participant.name == artist.name: return label_participant