def checkClients(self, product_name, client_names, cluster_name, release): # activate in sql side # repeat this procedure for all clients in client_names list # 1. check if client already exists in database-> if exists then set is_active = true else add a new entry # 2. find the old client and deactivate that record if to belongs to only one cluster, also deactivate cprc records # 3. find if there is cprc record for the new product # if it is there for each client and if it is inactive-> make it as active # else create new cprc records for client_name in client_names: exists_client = db.session.query(Client).filter( Client.client_name == client_name).first() print(exists_client) if exists_client: exists_client.is_active = True else: client = Client(client_name=client_name, is_active=True) db.session.add(client) db.session.commit() print("new client is added") old_clients = db.session.query(Client.client_name).filter( Client.client_id == CPRC.client_id, CPRC.cluster_id == Cluster.cluster_id).filter( Cluster.cluster_name == cluster_name).all() print("list of all old clients", old_clients) for old_client in old_clients: old_client = old_client[0] if old_client not in client_names: client = db.session.query(Client).filter( Client.client_name == old_client).first() if client: cprc = db.session.query(CPRC).filter( CPRC.product_release_id == Product_Release.product_release_id, Product.product_id == Product_Release.product_id, CPRC.cluster_id == Cluster.cluster_id, CPRC.client_id == Client.client_id).filter( Cluster.cluster_name == cluster_name).filter( Client.client_name == client_name).all() for record in cprc: record.is_active = False db.session.commit() cluster_count = db.session.query( CPRC.cluster_id).filter( CPRC.client_id == client.client_id).filter( client.is_active == True).all() if (len(cluster_count) == 1): client.is_active = False db.session.commit() addUpdateRecord = AddUpdateRecords() exists_cprc = addUpdateRecord.checkCPRCExists( client_name, cluster_name, product_name, release) if exists_cprc is not None: if exists_cprc.is_active == False: exists_cprc.is_active = True db.session.commit() print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") print("Added cprc under u[pdate client function") print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") else: addUpdateRecord.addCPRC(client_name, product_name, cluster_name, release) print("End of check client function")
def addUpdateClient(self, old_client_name, new_client_name, product_name, cluster_name, release_number): try: #check if the new client already exists or not exists_client = db.session.query(Client.client_name).filter( Client.client_name == new_client_name).scalar() is not None #if not exists then add new client record if not exists_client: client = Client(client_name=new_client_name, is_active=True) db.session.add(client) db.session.commit() print("New client is being added!!!!!!!!") # make the old client record as inactive if old_client_name != "": self.deactivateClient(old_client_name) self.deactivateCPRC(old_client_name, product_name, cluster_name, release_number) #add new cprc record with new client self.addCPRC(new_client_name, product_name, cluster_name, release_number) #if the new client already exists then mark it as active client, deactivate cprc for old and add new cprc record if exists_client: client = db.session.query(Client).filter( Client.client_name == new_client_name).first() if client: client.is_active = True db.session.commit() exists_cprc = self.checkCPRCExists(new_client_name, cluster_name, product_name, release_number) #if the cprc entry already exists then make it as active if exists_cprc is not None: if exists_cprc.is_active == False: exists_cprc.is_active = True db.session.commit() self.deactivateClient(old_client_name) self.deactivateCPRC(old_client_name, product_name, cluster_name, release_number) return "Client-cprc record already exists" #else add new cprc record and deactivate the old record else: self.deactivateCPRC(old_client_name, product_name, cluster_name, release_number) self.addCPRC(new_client_name, product_name, cluster_name, release_number) except Exception as ex: date = datetime.utcnow() tb = sys.exc_info()[2] errorMsg = str( date ) + " - File: addUpdateDB.py - Function: addUpdateClient - " + str( ex.args) + " - on line " + str(tb.tb_lineno) + " \r\n" f.write(errorMsg) f.close()