""" Validate the list of upcs. """ from oto import response from masters_registry.constant import db_const from masters_registry.constant import error from masters_registry.constant import field_const def compare(origin_upcs, from_db_upcs): """Compare UPCs lists. First one - original input list. Second - returned from DB. Args: origin_upcs (list(str)): list of input upc's. from_db_upcs (dict): dict of group by upc records. Returns: Response: 200 - if validation success. 400 - if validation failed. Response.errors.message contains list of failed UPC with description of reason. """ error_messages = [] for upc in origin_upcs: single_upc = int(upc) error_message = compare_single_upc( single_upc, from_db_upcs) if not error_message: continue error_dict = { field_const.UPC: upc, field_const.ERROR_COUNT: 1, field_const.SUCCESS_COUNT: 0, field_const.ERROR_MESSAGE: error_message, field_const.STATUS_REPORT: [] } error_messages.append(error_dict) if not error_messages: return response.Response() return response.Response( message=error_messages, status=400) def compare_single_upc(upc, upcs_from_db): """Check whether UPC is ready for import. Args: upc (str): Universal product code upcs_from_db (dict(list)): {upc: [related isrc dicts from DB]} Returns: str: error message if UPC isn't ready """ if upc not in upcs_from_db.keys(): return error.UPC_NOT_FOUND upc_from_db = upcs_from_db[upc] is_music_type_upc = ( upc_from_db[0][field_const.PRODUCT_TYPE_ID] == db_const.MUSIC_PRODUCT_TYPE_ID) is_digital_distribution = ( upc_from_db[0][field_const.DISTRIBUTION_CONTEXT_TYPE] == db_const.DIGITAL_DISTRIBUTION_FORMAT) if not all((is_music_type_upc, is_digital_distribution)): error_message = error.INVALID_UPC_TYPE return error_message # All ISRCs of given UPC are supposed to have same `release_status` # and `not_for_distribution` values. It's ok to check only first upc_in_content = ( upc_from_db[0][field_const.RELEASE_STATUS] == db_const.IN_CONTENT) if not upc_in_content: return error.UPC_INVALID_STATUS valid_for_distribution = ( upc_from_db[0][field_const.NOT_FOR_DISTRIBUTION] == db_const.FOR_DISTRIBUTION) if not valid_for_distribution: return error.UPC_NOT_FOR_DISTRIBUTION has_only_music_isrcs = all( isrc[field_const.TRACK_TYPE] == db_const.MUSIC_TRACK_TYPE for isrc in upc_from_db) if not has_only_music_isrcs: return error.UPC_BUNDLED return ''