"""Participant Handlers.""" from switchboard_consumer.constants import entity_type, system from switchboard_consumer.constants.participant_roles import ( CONTRIBUTOR_ROLE_MAPPING, DISPLAY_ARTIST_ROLE_MAPPING) from switchboard_consumer.logic.participant.base_participant_handler \ import BaseParticipantHandler from switchboard_consumer.utils.message import get_system_local_id class ProductParticipantHandler(BaseParticipantHandler): """"Handle participant creation for products.""" def __init__( self, logger, orchard_client, message, vendor_id, subaccount_id): """Create a ProductParticipantHandler instance. Args: logger: Logger instance orchard_client: OrchardClient instance message: Incoming KafkaMessage vendor_id: target vendor id subaccount_id: target subaccount id """ super().__init__(logger, orchard_client, message, vendor_id, subaccount_id) def process(self, product_data): """Process product participants.""" processing_results = [] artists = [] members = product_data['displayArtist'].get('members', []) contributors = product_data.get('contributors', []) if self._validate_display_artist_members(members): # We have members, process each member and return results to a list handled_members = \ self._process_display_artist_members(members) for (processing_result, artist) in handled_members: if processing_result: processing_results.append(processing_result) artists.append(artist) else: # Simply handle root display artist if all else fails processing_result, artist = self._process_display_artist( product_data) if processing_result: processing_results.append(processing_result) artists.append(artist) if contributors: handled_contributors = \ self._process_contributors(contributors) for (processing_result, artist) in handled_contributors: if processing_result: processing_results.append(processing_result) artists.append(artist) return processing_results, artists def _process_display_artist_members(self, members): handled_members = [] for member in members: if DISPLAY_ARTIST_ROLE_MAPPING.get(member['role']): participant = member['participant'] role = DISPLAY_ARTIST_ROLE_MAPPING.get(member['role']) source_mapping = get_system_local_id( member['participant'], system.SONY) target_mapping = get_system_local_id( member['participant']) handled_members.append( self.handle_participant(participant, role, source_mapping, target_mapping) ) else: self.logger.info( 'Skipped handling participant - unsupported role', custom_fields={ 'role': member['role'], 'entity_type': entity_type.PRODUCT, 'participant_type': 'display_artist_member' }) return handled_members def _process_display_artist(self, product_data): participant = product_data['displayArtist'] role = 'primary_artist' source_mapping = get_system_local_id( product_data['displayArtist'], system.SONY) target_mapping = get_system_local_id( product_data['displayArtist']) return self.handle_participant(participant, role, source_mapping, target_mapping) def _process_contributors(self, contributors): handled_contributors = [] for contributor in contributors: if CONTRIBUTOR_ROLE_MAPPING.get(contributor['role']): participant = contributor['participant'] role = CONTRIBUTOR_ROLE_MAPPING.get(contributor['role']) source_mapping = get_system_local_id( contributor['participant'], system.SONY) target_mapping = get_system_local_id( contributor['participant']) handled_contributors.append( self.handle_participant(participant, role, source_mapping, target_mapping) ) else: self.logger.info( 'Skipped handling participant - unsupported role', custom_fields={ 'role': contributor['role'], 'entity_type': entity_type.PRODUCT, 'participant_type': 'contributor' }) return handled_contributors def _validate_display_artist_members(self, members): """Check if we have members of valid roles.""" roles = DISPLAY_ARTIST_ROLE_MAPPING.keys() return any(member['role'] in roles for member in members if members)