def get_client_db_list(self): try: self._k_db.begin() query = "select t1.client_id, t1.legal_entity_id, " + \ " t2.database_username, t2.database_password, t2.database_name, " + \ " t3.database_ip, t3.database_port, " + \ " t4.ip as file_ip, t4.port as file_port " + \ " from tbl_client_database as t1 " + \ " inner join tbl_client_database_info as t2 " + \ " on t1.client_database_id = t2.client_database_id and t2.is_group = 0 " + \ " inner join tbl_database_server as t3 " + \ " on t1.database_server_id = t3.database_server_id " + \ " inner join tbl_file_server as t4 on " + \ " t1.file_server_id = t4.file_server_id " logProcessInfo("client_db_list", str(query)) rows = self._k_db.select_all(query) self._k_db.commit() if rows: return rows else: return [] except Exception, e: print e logProcessError("get_clients", str(e)) self._k_db.rollback() logProcessError("get_clients", str(traceback.format_exc()))
def get_countries(self): try: self._k_db.begin() q = "SELECT country_id, country_name FROM tbl_countries" rows = self._k_db.select_all(q) self._k_db.commit() return rows except Exception, e: logProcessError("get_countries", str(e)) self._k_db.rollback()
def check_service_provider_contract_period(self): query = "UPDATE tbl_service_providers set is_active = 0 WHERE " + \ " contract_from >= now() and contract_to <= now() " try: self.execute(query) logProcessInfo( "check_service_provider_contract_period %s" % self.client_id, str(query)) except Exception, e: logProcessError( "check_service_provider_contract_period %s" % self.client_id, str(e))
def update(self, table, columns, values, condition): query = "UPDATE " + table + " set " for index, column in enumerate(columns): if index < len(columns) - 1: query += column + " = %s, " else: query += column + " = %s " query += " WHERE " + condition # print query try: status = self.execute(query, values) return status except Exception, e: logProcessError("update", str(e)) print query, values print e return False
def start_next_due_date_task(d, due_date, approval_person): next_due_date, trigger_before = self.calculate_next_due_date( int(d["frequency_id"]), d["statutory_dates"], d["repeats_type_id"], d["repeats_every"], due_date) if trigger_before is None: trigger_before = int(d["trigger_before_days"]) c_h_id = notify(d, due_date, next_due_date, approval_person, trigger_before) if c_h_id is False: logProcessError( "next_due_date_task %s" % self.client_id, "compliance history save failed for %s" % (next_due_date)) logProcessError("next_due_date_task %s " % self.client_id, str(d)) return None, trigger_before return next_due_date, trigger_before
def execute_insert(self, query, param): cursor = self.cursor() assert cursor is not None try: if type(param) is tuple: cursor.execute(query, param) elif type(param) is list: # print "inside elif " cursor.execute(query, param) else: cursor.execute(query) print cursor.lastrowid return int(cursor.lastrowid) except Exception, e: print e logProcessError("insert", str(e)) # print query # print param return False
def bulk_insert(self, table, columns, valueList): stringValue = [] for i in range(len(columns)): stringValue.append('%s') if type(columns) is list: columns = ", ".join(columns) query = "INSERT INTO %s (%s) " % (table, columns) query += " VALUES (%s) " % (",".join(stringValue)) try: cursor = self.cursor() assert cursor is not None cursor.executemany(query, valueList) return True except Exception, e: print e logProcessError("bulk-insert", str(e)) return False
def insert(self, table, columns, values): # columns = ", ".join(columns) stringValue = [] for i in range(len(values)): stringValue.append('%s') if type(columns) is list: columns = ", ".join(columns) columns = "(%s)" % columns query = """INSERT INTO %s %s """ % (table, columns) query += " VALUES (%s) " % (",".join(stringValue)) print query try: n_id = self.execute_insert(query, values) return n_id except Exception, e: print e logProcessError("insert", str(e)) # print query, values return False
def begin_process(self): current_date = datetime.datetime.utcnow().date() client_info = self.get_client_db_list() logProcessInfo("DailyProcess", "process begin") logProcessInfo("DailyProcess", str(current_date)) logProcessInfo("begin_process", client_info) try: for c in client_info: print c try: task = AutoStart(c["database_ip"], c["database_username"], c["database_password"], c["database_name"], c["database_port"], c["client_id"], c["legal_entity_id"], current_date) task.start_process() except Exception, e: logProcessError("DailyProcess", e) logProcessError("DailyProcess", (traceback.format_exc())) except Exception, e: print e
def begin_process(self): logProcessInfo("AutoDeletionProcess", "Process Begin") country_time_zones = sorted(countries) country_list = self.get_countries() print country_list for c in country_list: name = c["country_name"].replace(" ", "") name = name.replace("_", "") name = name.replace("-", "") info = None for ct in country_time_zones: ct = ct.replace(" ", "") if name.lower() == ct.lower(): info = countries.get(ct) print info break if info: current_date = return_date( time_convertion(info.get("timezones")[0])) print "country --", c["country_name"] country_id = c["country_id"] print current_date client_info = self.get_client_db_list() logProcessInfo("AutoDeletionProcess", "Process Begin") for ci in client_info: print ci["client_id"] try: delp = AutoDeletionStart( ci["database_ip"], ci["database_username"], ci["database_password"], ci["database_name"], ci["database_port"], ci["client_id"], ci["legal_entity_id"], current_date, country_id) delp.start_process() except Exception, e: logProcessError("AutoDeletionProcess", e) logProcessError("AutoDeletionProcess", (traceback.format_exc()))
def select_one(self, query, param=None): cursor = self.cursor() assert cursor is not None try: if param is None: cursor.execute(query) else: if type(param) is tuple: cursor.execute(query, param) elif type(param) is list: cursor.execute(query, param) else: cursor.execute(query) res = cursor.fetchone() return res except Exception, e: print "Exception" # print query # print param print e logProcessError("select_one", str(e)) raise fetch_error()
def start_process(self): if self._connection is None: details = "%s, %s" % (self._c_db_ip, self._c_db_name) logProcessInfo("connection not found %s" % self.client_id, details) return try: self.begin() self.run_deletion_process() self.commit() except Exception, e: logProcessError("start_deletion_process %s" % self.client_id, str(e)) logProcessError("start_deletion_process", str(traceback.format_exc())) logProcessError("start_deletion_process", (traceback.format_exc())) print e
def start_process(self): if self._connection is None: details = "%s, %s" % (self._c_db_ip, self._c_db_name) logProcessInfo("connection not found %s" % self.client_id, details) return try: self.begin() self.start_new_task() self.check_service_provider_contract_period() self.update_unit_wise_task_status() self.update_user_wise_task_status() self.update_duedate_in_calendar_view() self.update_upcoming_in_calendar_view() self.commit() self.close() except Exception, e: logProcessError("start_process %s" % self.client_id, str(e)) logProcessError("start_process", str(traceback.format_exc())) logProcessError("start_process", (traceback.format_exc())) self.rollback() self.close()
def start_new_task(self): def notify(d, due_date, next_due_date, approval_person, trigger_before): start_date = self.actual_start_date(due_date, trigger_before) compliance_history_id = self.save_in_compliance_history( int(d["unit_id"]), int(d["compliance_id"]), start_date, due_date, next_due_date, int(d["assignee"]), d["concurrence_person"], int(approval_person)) if compliance_history_id is False: return False else: self.started_unit_id.append(d["unit_id"]) self.started_user_id.append(d["assignee"]) self.started_user_id.append(approval_person) self.started_user_id.append(d["concurrence_person"]) if d["document_name"]: compliance_name = d["document_name"] + " - " + d[ "compliance_task"] else: compliance_name = d["compliance_task"] unit_name = d["unit_code"] + " - " + d["unit_name"] notification_text = "Compliance task %s started" % ( compliance_name) extra_details = " %s - Compliance Started" % ( compliance_history_id) notification_type_id = 4 # 4 = messages self.save_in_notification(d["country_id"], d["domain_id"], d["business_group_id"], d["legal_entity_id"], d["division_id"], d["unit_id"], d["compliance_id"], d["assignee"], d["concurrence_person"], d["approval_person"], notification_text, extra_details, notification_type_id) a_name, assignee_email = self.get_email_id_for_users(d["assignee"]) notify_new_task = threading.Thread( target=email.notify_compliance_start, args=[ a_name, compliance_name, unit_name, d["due_date"], assignee_email ]) notify_new_task.start() return True def start_next_due_date_task(d, due_date, approval_person): next_due_date, trigger_before = self.calculate_next_due_date( int(d["frequency_id"]), d["statutory_dates"], d["repeats_type_id"], d["repeats_every"], due_date) if trigger_before is None: trigger_before = int(d["trigger_before_days"]) c_h_id = notify(d, due_date, next_due_date, approval_person, trigger_before) if c_h_id is False: logProcessError( "next_due_date_task %s" % self.client_id, "compliance history save failed for %s" % (next_due_date)) logProcessError("next_due_date_task %s " % self.client_id, str(d)) return None, trigger_before return next_due_date, trigger_before data = self.get_compliance_to_start() count = 0 for d in data: print d["unit_id"] try: approval_person = int(d["approval_person"]) if d["frequency_id"] == 1: next_due_date = "0000-00-00" trigger_before = d["trigger_before_days"] if trigger_before is None: continue c_h_id = notify(d, d["due_date"], next_due_date, approval_person, trigger_before) if c_h_id is False: # compliance history id is false so continue further continue else: if d["frequency_id"] in [3, 4]: cnt, statutory_date, repeats_every, repeats_type_id = self.is_this_first_task_of_year( d["unit_id"], d["country_id"], d["domain_id"], d["compliance_id"]) if statutory_date is None and repeats_every is None and repeats_type_id is None: continue else: d["statutory_dates"] = statutory_date d["repeats_type_id"] = repeats_type_id d["repeats_every"] = repeats_every next_due_date = trigger_before = None due_date = d["due_date"] print due_date next_due_date, trigger_before = start_next_due_date_task( d, due_date, approval_person) print next_due_date if next_due_date is not None: self.update_assign_compliance_due_date( trigger_before, next_due_date, d["unit_id"], d["compliance_id"]) while (next_due_date - timedelta(days=trigger_before) ) <= self.current_date: # start for next-due-date next_due_date, trigger_before = start_next_due_date_task( d, next_due_date, approval_person) if next_due_date is None: break self.update_assign_compliance_due_date( trigger_before, next_due_date, d["unit_id"], d["compliance_id"]) count += 1 except Exception, e: logProcessError("task_start_failed", str(e)) logProcessError("task_start_failed", d) logProcessError("task_start_failed", str(traceback.format_exc())) continue