From 5382ff7d043d09fb499d43dce17e7dbd70ecd9e4 Mon Sep 17 00:00:00 2001 From: Michael Tenetko Date: Sat, 7 Dec 2024 20:40:40 +0300 Subject: [PATCH] (feat) created a q5011_2t adder specifically for waves 1 and 2 --- add_q5011_2t_w1.py | 102 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 add_q5011_2t_w1.py diff --git a/add_q5011_2t_w1.py b/add_q5011_2t_w1.py new file mode 100644 index 0000000..68a1d15 --- /dev/null +++ b/add_q5011_2t_w1.py @@ -0,0 +1,102 @@ +import json +import pandas as pd +import psycopg2 +import zipfile +from datetime import datetime + +from glob import glob + + +class IVDate1Updater: + IVDATE1_DATETIME_FORMAT = "%d.%m.%Y %H:%M:%S" # 02.05.2022 15:16:18 + ISO_DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S" # 2022-05-02 15:04:09 + + def __init__(self): + self.config = self.get_config() + + def get_config(self): + with open("config.json", "r", encoding="utf-8") as input_file: + return json.load(input_file) + + def get_dataframe(self, file_name): + df = pd.read_excel(file_name) + df = df.astype({"IVDate1": "str"}) + + return df + + def is_month_incorrect(self, dataframe): + first_row = dataframe.iloc[0] + ivdate = first_row["IVDate1"] + ivdate = datetime.strptime(ivdate, self.IVDATE1_DATETIME_FORMAT) + + for _, row in dataframe.iterrows(): + recruiting_date = row["IVDate1"] + + + def update_table(self, dataframe, month_is_incorrect): + + with psycopg2.connect( + host=self.config["db_host"], + dbname=self.config["db_name"], + user=self.config["db_user"], + password=self.config["db_password"], + ) as conn: + with conn.cursor() as cur: + for _, row in dataframe.iterrows(): + ivdate1 = row["IVDate1"] + ivdate1 = datetime.strptime(ivdate1, self.IVDATE1_DATETIME_FORMAT) + ivdate1 = datetime.strftime(ivdate1, self.ISO_DATETIME_FORMAT) + + query_parameters = { + "id": row["ID"], + "q5011_2t": ivdate1, + } + cur.execute( + """ + UPDATE recruits_log + SET q5011_2t = %(q5011_2t)s + WHERE id = %(id)s; + """, + query_parameters, + ) + conn.commit() + + def make_fixed_recruiting_date(self, recruiting_date): + month = recruiting_date[5:7] + new_month = self.get_new_month(month) + + rd_year = recruiting_date[:4] + rd_day_and_time = recruiting_date[8:] + + new_recruiting_date = f"{rd_year}-{new_month}-{rd_day_and_time}" + + return new_recruiting_date + + def get_new_month(self, month): + month = int(month) + 1 + month = f"{month:02d}" + + return month + + def run(self): + for file_name in glob("./xlsx/*.zip"): + print(file_name[7:]) + with zipfile.ZipFile(file_name, "r") as zip: + with zip.open(zip.namelist()[0]) as excel_file: + dataframe = self.get_dataframe(excel_file) + month_is_incorrect = self.is_month_incorrect(dataframe) + self.update_table(dataframe, month_is_incorrect) + + def run_excel(self): + for file_name in glob("./xlsx/*.xlsx"): + print(file_name[7:]) + dataframe = self.get_dataframe(file_name) + month_is_incorrect = self.is_month_incorrect(dataframe) + self.update_table(dataframe, False) + + +if __name__ == "__main__": + u = IVDate1Updater() + u.run_excel() + + # u.run() \ No newline at end of file