"""Validate Contract.""" import re import ast from adjustment.data_class import DataClass from connectors.art_relation_db import ARMysqlConnection from adjustment.sql_templates import GET_VENDOR_UPC_OWNERSHIPS class Contract: def __init__(self, contract_id: int, upc: int): self.__contract_id = contract_id self.__upc = str(upc) def get_product_term_contract_by_upc(self, upc: str): """Get contract product term for a specified upc.""" contracts = DataClass.product_terms if not contracts: return False for contract in contracts: if (upc in ast.literal_eval(contract['attachments'])): return contract return False def check_contract_has_upc_attached(self): """check specified upc attached to contract.""" return self.get_product_term_contract_by_upc(self.__upc) def check_contract_has_upc_attached_ignore_leading_zero(self): """remove leading zero and check specified upc attached to contract.""" upc = re.sub('^0', '', self.__upc) return self.get_product_term_contract_by_upc(str(upc)) def check_contract_has_upc_attached_all_ignore_leading_zero(self): """remove all leading zeros and check specified upc attached to contract.""" upc = re.sub('^0+', '', self.__upc) return self.get_product_term_contract_by_upc(str(upc)) def check_upc_display_upc_mapping(self): """check if specified upc matches with the display upc's.""" release_upcs = DataClass.upcs for upcs in release_upcs: if upcs['display_upc'] == self.__upc: return self.get_product_term_contract_by_upc(str(upcs['upc'])) return False def get_label_term_for_contract(self): """get label term for a specified contract.""" contracts = DataClass.label_terms if not contracts: return [] for contract in contracts: if contract['contract_id'] == self.__contract_id: return ast.literal_eval(contract['attachments']) return [] def check_contract_label_owns_upc(self): """check if upc belongs to any contract label.""" labels = self.get_label_term_for_contract() if not labels: return False for label_id in labels: if self.check_label_ows_upc(label_id, self.__upc): return True if self.label_owns_upc_ignore_leading_zero(label_id, self.__upc): return True return False def label_owns_upc_ignore_leading_zero(self, label_id, upc): """remove leading zero and check if upc belongs to any contract label.""" upc = re.sub('^0', '', self.__upc) if self.check_label_ows_upc(label_id, upc): return True return False def check_label_ows_upc(self, label_id, upc): """get upcs by label_id and specified upc, and store it in upc_labels.""" upc_label = DataClass.upc_labels.get(upc, None) if upc_label: return True results = ARMysqlConnection.execute_sql_query( GET_VENDOR_UPC_OWNERSHIPS, tuple([str(label_id), upc, upc]) ) if results: # mapped upc and display_upc with vendor_id DataClass.upc_labels.update({ str(results[0]['upc']): results[0]['vendor_id'], str(results[0]['display_upc']): results[0]['vendor_id'] }) return True return False