From 47df8a26689f17cba8c5464aefd1b2beb3733168 Mon Sep 17 00:00:00 2001 From: pjpatil12 Date: Sat, 20 Dec 2025 12:39:16 +0530 Subject: [PATCH] remove comment code --- app/services/file_service.py | 280 ++--------------------------------- 1 file changed, 15 insertions(+), 265 deletions(-) diff --git a/app/services/file_service.py b/app/services/file_service.py index f0b0a5c..53ca25b 100644 --- a/app/services/file_service.py +++ b/app/services/file_service.py @@ -23,12 +23,10 @@ class FileService: # def handle_file_upload(self, file, subcontractor_id, file_type): def handle_file_upload(self, file, subcontractor_id, RA_Bill_No): - # RA_Bill_No = 1 # this RA bill define temp for upload (changes also) - if not subcontractor_id: return False, "Please select subcontractor." - # if not file_type: - # return False, "Please select file type." + if not RA_Bill_No: + return False, "Please Enter RA Bill No)." if not file or file.filename == "": return False, "No file selected." if not self.allowed_file(file.filename): @@ -45,69 +43,26 @@ class FileService: try: - # df = pd.read_csv(filepath) if filename.endswith(".csv") else pd.read_excel(filepath) - df_tr_ex = pd.read_excel(filepath, sheet_name ="Tr.Ex.", header=12) df_mh_ex = pd.read_excel(filepath, sheet_name="MH Ex.", header=12) df_mh_dc = pd.read_excel(filepath, sheet_name="MH & DC", header=11) - print("\n=== Uploaded File tr ex ===") - print(df_tr_ex.head()) - print("=============================\n") - - print("\n=== Uploaded File mh ex ===") - print(df_mh_ex.head()) - print("========================================\n") - - print("\n=== Uploaded File MH DC ===") - print(df_mh_dc.head()) - print("========================================\n") - - self.process_trench_excavation(df_tr_ex, subcontractor_id, RA_Bill_No) self.process_manhole_excavation(df_mh_ex, subcontractor_id, RA_Bill_No) self.process_manhole_domestic_chamber(df_mh_dc, subcontractor_id, RA_Bill_No) - - - # df = pd.read_csv(filepath) if filename.endswith(".csv") else pd.read_excel(filepath) - - # # Trench Excavation save (subcontractor) - # if file_type == "trench_excavation": - # return self.process_trench_excavation(df, subcontractor_id) - - # # Manhole Excavation save (subcontractor) - # if file_type == "manhole_excavation": - # return self.process_manhole_excavation(df, subcontractor_id) - - # # Manhole and Domestic Chamber Construction save (subcontractor) - # if file_type == "manhole_domestic_chamber": - # return self.process_manhole_domestic_chamber(df, subcontractor_id) - - # # Tr Ex save (client) - # if file_type =="tr_ex_client": - # return self.client_trench_excavation(df, subcontractor_id) - - # # Mh Ex save (client) - # if file_type =="mh_ex_client": - # return self.client_manhole_excavation(df, subcontractor_id) - - # # Mh and Dc save (client) - # if file_type == "mh_dc_client": - # return self.client_manhole_domestic_chamber(df, subcontractor_id) - return True, "File uploaded successfully." except Exception as e: return False, f"Processing failed: {e}" - # new new + # Trench Excavation save method (TrenchExcavation model) def process_trench_excavation(self, df, subcontractor_id,RA_Bill_No): - print("RA_Bill_No of Tr Ex:",RA_Bill_No) + print("trench_excavation RA_Bill_No :",RA_Bill_No) - print("=== RAW HEADERS ===") + print("=== trench_excavation ===") print(df.columns.tolist()) print("===================") @@ -161,7 +116,7 @@ class FileService: **record_data ) - print("Saving Row → Location:", record.Location, " MH_NO:", record.MH_NO) + print("trench_excavation Saving Row → Location:", record.Location, " MH_NO:", record.MH_NO) db.session.add(record) saved_count += 1 @@ -178,12 +133,12 @@ class FileService: return False, f"Trench Excavation save failed: {e}" - # new new + # Manhole Excavation save method (ManholeExcavation model) def process_manhole_excavation(self, df, subcontractor_id, RA_Bill_No): - print("RA_Bill_No of MH EX:",RA_Bill_No) + print("manhole_excavation RA_Bill_No:",RA_Bill_No) - print("=== RAW HEADERS ===") + print("=== manhole_excavation ===") print(df.columns.tolist()) print("===================") @@ -237,7 +192,7 @@ class FileService: **record_data ) - print("Saving Row → Location:", record.Location, " MH_NO:", record.MH_NO) + print("manhole_excavation Saving Row → Location:", record.Location, " MH_NO:", record.MH_NO) db.session.add(record) saved_count += 1 @@ -253,12 +208,12 @@ class FileService: db.session.rollback() return False, f"Manhole Excavation save failed: {e}" - # new new + # Manhole and Domestic Chamber Construction save method (ManholeDomesticChamber model) def process_manhole_domestic_chamber(self, df, subcontractor_id, RA_Bill_No): - print("RA_Bill_No of Manhole Domestic Chamber:",RA_Bill_No) + print("manhole_domestic_chamber RA_Bill_No :",RA_Bill_No) - print("=== RAW HEADERS ===") + print("=== manhole_domestic_chamber ===") print(df.columns.tolist()) print("===================") @@ -312,7 +267,7 @@ class FileService: **record_data ) - print("Saving Row → Location:", record.Location, " Node_No:", record.Node_No) + print("manhole_domestic_chamber Saving Row → Location:", record.Location, " Node_No:", record.Node_No) db.session.add(record) saved_count += 1 @@ -330,213 +285,8 @@ class FileService: -# olds - # def handle_file_upload(self, file, subcontractor_id, file_type): - # # def handle_file_upload(self, file, subcontractor_id): - - # if not subcontractor_id: - # return False, "Please select subcontractor." - # # if not file_type: - # # return False, "Please select file type." - # if not file or file.filename == "": - # return False, "No file selected." - # if not self.allowed_file(file.filename): - # return False, "Invalid file type! Allowed: CSV, XLSX, XLS" - - # ensure_upload_folder() - - # folder = os.path.join(Config.UPLOAD_FOLDER, f"sub_{subcontractor_id}") - # os.makedirs(folder, exist_ok=True) - - # filename = secure_filename(file.filename) - # filepath = os.path.join(folder, filename) - # file.save(filepath) - - # try: - - # # df = pd.read_csv(filepath) if filename.endswith(".csv") else pd.read_excel(filepath) - # df = pd.read_excel(filepath, sheet_name ="Tr.Ex.", header=0) - # df1 = pd.read_excel(filepath, sheet_name="MH Ex.", header=0) - - # print("\n=== Uploaded File Preview ===") - # print(" file name h:",df) - # print("=============================\n") - # print(" file name h1:",df1) - # # print(df.head()) - # print("=============================\n") - - # # Trench Excavation save (subcontractor) - # if file_type == "trench_excavation": - # return self.process_trench_excavation(df, subcontractor_id) - - # # Manhole Excavation save (subcontractor) - # if file_type == "manhole_excavation": - # return self.process_manhole_excavation(df, subcontractor_id) - - # # Manhole and Domestic Chamber Construction save (subcontractor) - # if file_type == "manhole_domestic_chamber": - # return self.process_manhole_domestic_chamber(df, subcontractor_id) - - # # Tr Ex save (client) - # if file_type =="tr_ex_client": - # return self.client_trench_excavation(df, subcontractor_id) - - # # Mh Ex save (client) - # if file_type =="mh_ex_client": - # return self.client_manhole_excavation(df, subcontractor_id) - - # # Mh and Dc save (client) - # if file_type == "mh_dc_client": - # return self.client_manhole_domestic_chamber(df, subcontractor_id) - - # return True, "File uploaded successfully." - - # except Exception as e: - # return False, f"Processing failed: {e}" - - - # ---------------------- Sub contractor -------------------------- - # Trench Excavation save method (TrenchExcavation model) - # def process_trench_excavation(self, df, subcontractor_id): - # df.columns = [str(c).strip() for c in df.columns] - # # If the sheet has merged cells -> forward fill Location - # if "Location" in df.columns: - # df["Location"] = df["Location"].ffill() - - # df = df.dropna(how="all") # REMOVE empty rows - # # Identify missing location rows before insert - # missing_loc = df[df["Location"].isna() | (df["Location"].astype(str).str.strip() == "")] - # if not missing_loc.empty: - # return False, f"Error: Some rows have empty Location. Rows: {missing_loc.index.tolist()}" - - # saved_count = 0 - - # try: - # for index, row in df.iterrows(): - # record_data = {} - # # Insert only fields that exist in model - # for col in df.columns: - # if hasattr(TrenchExcavation, col): - # value = row[col] - # # Normalize empty values - # if pd.isna(value) or str(value).strip() in ["", "-", "—", "nan", "NaN"]: - # value = None - - # record_data[col] = value - - # record = TrenchExcavation( - # subcontractor_id=subcontractor_id, - # **record_data - # ) - - # db.session.add(record) - # saved_count += 1 - - # db.session.commit() - # return True, f"Trench Excavation data saved successfully. Total rows: {saved_count}" - - # except Exception as e: - # db.session.rollback() - # return False, f"Trench Excavation Save Failed: {e}" - - - # Manhole Excavation save method (ManholeExcavation model) - # def process_manhole_excavation(self, df, subcontractor_id): - # # Clean column names (strip whitespace) - # df.columns = [str(c).strip() for c in df.columns] - # # If the sheet has merged cells -> forward fill Location - # if "Location" in df.columns: - # df["Location"] = df["Location"].ffill() - - # # REMOVE empty rows - # df = df.dropna(how="all") - # # Identify missing location rows before insert - # missing_loc = df[df["Location"].isna() | (df["Location"].astype(str).str.strip() == "")] - # if not missing_loc.empty: - # return False, f"Error: Some rows have empty Location. Rows: {missing_loc.index.tolist()}" - - # saved_count = 0 - - # try: - # for index, row in df.iterrows(): - # record_data = {} - # # Insert only fields that exist in model - # for col in df.columns: - # if hasattr(ManholeExcavation, col): - # value = row[col] - - # # Normalize empty values - # if pd.isna(value) or str(value).strip() in ["", "-", "—", "nan", "NaN"]: - # value = None - - # record_data[col] = value - - # record = ManholeExcavation( - # subcontractor_id=subcontractor_id, - # **record_data - # ) - - # db.session.add(record) - # saved_count += 1 - - # db.session.commit() - # return True, f"Manhole Excavation data saved successfully. Total rows: {saved_count}" - - # except Exception as e: - # db.session.rollback() - # return False, f"Manhole Excavation Save Failed: {e}" - - - # Manhole and Domestic Chamber Construction save method (ManholeDomesticChamber model) - # def process_manhole_domestic_chamber(self, df, subcontractor_id): - # # Clean column names (strip whitespace) - # df.columns = [str(c).strip() for c in df.columns] - # # If the sheet has merged cells -> forward fill Location - # if "Location" in df.columns: - # df["Location"] = df["Location"].ffill() - - # # REMOVE empty rows - # df = df.dropna(how="all") - # # Identify missing location rows before insert - # missing_loc = df[df["Location"].isna() | (df["Location"].astype(str).str.strip() == "")] - # if not missing_loc.empty: - # return False, f"Error: Some rows have empty Location. Rows: {missing_loc.index.tolist()}" - - # saved_count = 0 - - # try: - # for index, row in df.iterrows(): - # record_data = {} - - # # Insert only fields that exist in model - # for col in df.columns: - # if hasattr(ManholeDomesticChamber, col): - # value = row[col] - # # Normalize empty values - # if pd.isna(value) or str(value).strip() in ["", "-", "—", "nan", "NaN"]: - # value = None - - # record_data[col] = value - - # record = ManholeDomesticChamber( - # subcontractor_id=subcontractor_id, - # **record_data - # ) - - # db.session.add(record) - # saved_count += 1 - - # db.session.commit() - # return True, f"Manhole Domestic Chamber Construction data saved successfully. Total rows: {saved_count}" - - # except Exception as e: - # db.session.rollback() - # return False, f"Manhole Domestic Chamber Construction Save Failed: {e}" - - - # ---------------------- client ---------------------------------- - # Tr Ex save method (TrenchExcavationClient model) + def client_trench_excavation(self, df, subcontractor_id): df.columns = [str(c).strip() for c in df.columns] # If the sheet has merged cells -> forward fill Location