"""Logic layer for product workflow.""" import copy from oto import response from product_digital.constants import error from product_digital.constants import product from product_digital.models import audio_product from product_digital.models import ows_assets from product_digital.models import ows_product_workflow from product_digital.models import ows_track from product_digital.models import release from product_digital.validation import audio_product as audio_product_validator def update_rejections(rejections_data): """Update a collection of rejections.""" return ows_product_workflow.update_rejections( rejections_data) def create_release_correction( product_id, correction_data, orchard_user_id=None): """Create a new correction record for the given product.""" release_response = release.get_release(product_id) if release_response.status == 404: return release_response get_correction_response = ows_product_workflow.get_last_release_correction( product_id) if get_correction_response.status not in [200, 404]: return get_correction_response if get_correction_response and get_correction_response.message.get( 'status') in ['active', 'submitted']: return response.create_error_response( code=error.ERROR_CODE_CORRECTION_CONFLICT, message='Product has a correction in "{}" status'.format( get_correction_response.message.get('status')), status=409) if orchard_user_id: correction_data = copy.copy(correction_data) correction_data.update(_get_last_updated_properties(orchard_user_id)) return ows_product_workflow.create_release_correction( product_id, correction_data) def _get_last_updated_properties(orchard_user_id): orchard_user_type, parsed_user_id = orchard_user_id.split(':') last_updated_properties = {'last_updated_by': int(parsed_user_id)} if orchard_user_type == 'alw': last_updated_properties['last_updated_type'] = 'vendor' elif orchard_user_type == 'oa': last_updated_properties['last_updated_type'] = 'oa' return last_updated_properties def delete_release_correction(product_id, release_correction_id): """Delete a release correction for the given product and its correction.""" valid_correction, correction = audio_product_validator.validate_release_correction( product_id=product_id, release_correction_id=release_correction_id, ) if not valid_correction: return response.create_error_response( code=error.VALIDATION_ERROR, message=error.ERROR_MESSAGE_INVALID_RELEASE_CORRECTION, status=400) ows_assets_response = ows_assets.delete_product_corrections_v2(product_id) if not ows_assets_response: return ows_assets_response return ows_product_workflow.delete_release_correction( release_correction_id) def create_correction_details_records( product_id, release_correction_id, correction_data, orchard_user_id=None): """Create a collection of correction details.""" valid_correction, _ = audio_product_validator.validate_release_correction( product_id=product_id, release_correction_id=release_correction_id, ) if not valid_correction: return response.create_error_response( code=error.VALIDATION_ERROR, message=error.ERROR_MESSAGE_INVALID_RELEASE_CORRECTION, status=400) if orchard_user_id: correction_data = copy.copy(correction_data) for correction_detail in correction_data.get('items'): correction_detail.update(_get_last_updated_properties( orchard_user_id)) correction_genre_changed = False release_subgenre = False genre_id = None subgenre_id = None existing_subgenre_id = None correction_genre_changed = _search_field_key_in_object('field_name', 'genre_id', correction_data['items']) if correction_genre_changed: genre_id = correction_genre_changed[0]['key_value'] release_subgenre = _search_field_key_in_object('field_name', 'release_subgenre', correction_data['items']) if release_subgenre: subgenre_id = release_subgenre[0]['key_value'][0] if subgenre_id and not genre_id: correction_response = ows_product_workflow.get_release_correction( release_correction_id=release_correction_id, ) for item in correction_response.message.get('items'): if (item.get('table_name') == 'releases' and item.get('field_name') == 'genre_id'): genre_id = item.get('key_value') if (item.get('table_name') == 'releases' and item.get('field_name') == 'release_subgenre'): existing_subgenre_id = item.get('key_value')[0] product_response = audio_product.get_product(product_id) if not genre_id: genre_id = product_response.message.get('genre_id') if not existing_subgenre_id: existing_subgenre_id = product_response.message.get('subgenre_id') if (genre_id and subgenre_id) or \ (subgenre_id in product.SOUNDTRACK_SUBGENRE_IDS or existing_subgenre_id in product.SOUNDTRACK_SUBGENRE_IDS): request_data = { 'genre_id': genre_id, 'subgenre_id': subgenre_id, 'correction_id': release_correction_id } ows_track_response = ows_track.change_genre( product_id, request_data, orchard_user_id) if not ows_track_response: return ows_track_response return ows_product_workflow.create_correction_details_records( release_correction_id, correction_data) def _search_field_key_in_object(key_name, key_value, data): return list(filter(lambda item: item[key_name] == key_value, data))