"""Logic for release corrections.""" from content_utils.exceptions import IneligibleEventError from owsrequest import request from src import config from src import constants def get_release_correction(release_correction_id): """Get a release correction.""" ows_response = request.process( config.APPLICATION_NAME, config.ENVIRONMENT, 'GET', constants.SERVICE_OWS_PRODUCT_WORKFLOW, f'/correction/{release_correction_id}' ) if ows_response.status_code == 404: raise IneligibleEventError('Release correction not found.') elif ows_response.status_code != 200: raise Exception('Failed fetching release correction', ows_response.text) data = ows_response.json() if data['status'] != constants.RELEASE_CORRECTION_STATUS_SUBMITTED: raise IneligibleEventError('Release correction not in a submitted state.') return data def _name(field_name): return constants.REMAPPED_FIELD_NAMES.get(field_name, field_name) def get_release_correction_updates(release_correction_id): """Get release revisions.""" release_correction = get_release_correction(release_correction_id) updates = {} artists = {} for item in release_correction['items']: if item['table_name'] != 'releases': continue elif item['field_name'] in constants.IGNORED_FIELDS: continue elif item['field_name'] not in constants.KNOWN_FIELDS: raise Exception('Unknown field name found', item['field_name']) elif item['field_name'] == constants.FIELD_RELEASE_SUBGENRE: updates[_name(item['field_name'])] = item['key_value'][0] elif item['field_name'] in constants.ARTIST_FIELDS: artists[item['field_name']] = item['key_value'] else: updates[_name(item['field_name'])] = item['key_value'] if not updates and not artists: raise IneligibleEventError('No release metadata to update.') return updates, artists