diff --git a/.gitignore b/.gitignore index 1b846b5..0a2f069 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,20 @@ +# Python +app/__pycache__/ +*.pyc +*.pyo +*.pyd + # Ingnor upload files app/static/uploads/ # Ignore files venv -# Ignore Log files -logs +# Ignore Log files ss +logs/ # Ignore db folders -instance +instance/ + + +*.__pycache__ \ No newline at end of file diff --git a/app/models/__pycache__/manhole_domestic_chamber_model.cpython-313.pyc b/app/models/__pycache__/manhole_domestic_chamber_model.cpython-313.pyc index de81f18..b5bbc55 100644 Binary files a/app/models/__pycache__/manhole_domestic_chamber_model.cpython-313.pyc and b/app/models/__pycache__/manhole_domestic_chamber_model.cpython-313.pyc differ diff --git a/app/models/manhole_domestic_chamber_model.py b/app/models/manhole_domestic_chamber_model.py index e84526a..caee3a2 100644 --- a/app/models/manhole_domestic_chamber_model.py +++ b/app/models/manhole_domestic_chamber_model.py @@ -38,6 +38,9 @@ class ManholeDomesticChamber(db.Model): d_9_16_to_9_65 = db.Column(db.Float) Domestic_Chambers = db.Column(db.Float) + DWC_Pipe_Length = db.Column(db.Float) + UPVC_Pipe_Length = db.Column(db.Float) + RA_Bill_No=db.Column(db.String(500)) created_at = db.Column(db.DateTime, default=datetime.today) diff --git a/app/routes/__pycache__/file_import.cpython-313.pyc b/app/routes/__pycache__/file_import.cpython-313.pyc index d7c6bae..5186279 100644 Binary files a/app/routes/__pycache__/file_import.cpython-313.pyc and b/app/routes/__pycache__/file_import.cpython-313.pyc differ diff --git a/app/routes/file_import.py b/app/routes/file_import.py index 85ffddf..d16071f 100644 --- a/app/routes/file_import.py +++ b/app/routes/file_import.py @@ -12,11 +12,12 @@ def import_file(): if request.method == "POST": file = request.files.get("file") subcontractor_id = request.form.get("subcontractor_id") + RA_Bill_No = request.form.get("RA_Bill_No") # file_type = request.form.get("file_type") service = FileService() # success, msg = service.handle_file_upload(file, subcontractor_id, file_type) - success, msg = service.handle_file_upload(file, subcontractor_id) + success, msg = service.handle_file_upload(file, subcontractor_id, RA_Bill_No) flash(msg, "success" if success else "danger") diff --git a/app/services/__pycache__/file_service.cpython-313.pyc b/app/services/__pycache__/file_service.cpython-313.pyc index 676152a..2acc6dc 100644 Binary files a/app/services/__pycache__/file_service.cpython-313.pyc and b/app/services/__pycache__/file_service.cpython-313.pyc differ diff --git a/app/services/file_service.py b/app/services/file_service.py index 3750737..f0b0a5c 100644 --- a/app/services/file_service.py +++ b/app/services/file_service.py @@ -21,9 +21,9 @@ class FileService: return "." in filename and filename.rsplit(".", 1)[1].lower() in Config.ALLOWED_EXTENSIONS # def handle_file_upload(self, file, subcontractor_id, file_type): - def handle_file_upload(self, file, subcontractor_id): + def handle_file_upload(self, file, subcontractor_id, RA_Bill_No): - RA_Bill_No = 1 # this RA bill define temp for upload (changes also) + # RA_Bill_No = 1 # this RA bill define temp for upload (changes also) if not subcontractor_id: return False, "Please select subcontractor." @@ -49,18 +49,24 @@ class FileService: 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) - # df2 = pd.read_excel(filepath, sheet_name="MH & DC", header=0) + 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("=== Uploaded File mh ex ===") + print("\n=== Uploaded File mh ex ===") print(df_mh_ex.head()) - print("========================================") + 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_excavation(df_mh_ex, subcontractor_id, RA_Bill_No) + self.process_manhole_domestic_chamber(df_mh_dc, subcontractor_id, RA_Bill_No) @@ -94,11 +100,9 @@ class FileService: except Exception as e: return False, f"Processing failed: {e}" + - - - - # new new + # new new def process_trench_excavation(self, df, subcontractor_id,RA_Bill_No): print("RA_Bill_No of Tr Ex:",RA_Bill_No) @@ -249,6 +253,81 @@ class FileService: db.session.rollback() return False, f"Manhole Excavation save failed: {e}" + # new new + def process_manhole_domestic_chamber(self, df, subcontractor_id, RA_Bill_No): + + print("RA_Bill_No of Manhole Domestic Chamber:",RA_Bill_No) + + print("=== RAW HEADERS ===") + print(df.columns.tolist()) + print("===================") + + # Clean column names + df.columns = ( + df.columns.astype(str) + .str.strip() + .str.replace(r"[^\w]", "_", regex=True) + .str.replace("__+", "_", regex=True) + .str.strip("_") + ) + + # Remove completely empty rows + df = df.dropna(how="all") + + # Forward fill merged Location + if "Location" in df.columns: + df["Location"] = df["Location"].ffill() + + saved_count = 0 + skipped_count = 0 + + try: + for index, row in df.iterrows(): + record_data = {} + location = row.get("Location") + Node_No = row.get("Node_No") + + if (pd.isna(location) or str(location).strip() == "" or pd.isna(Node_No) or str(Node_No).strip() == ""): + skipped_count += 1 + continue + + # Map only model columns + 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"]: + value = None + + record_data[col] = value + + # If all mapped fields are None → skip + if all(v is None for v in record_data.values()): + skipped_count += 1 + continue + + record = ManholeDomesticChamber( + subcontractor_id=subcontractor_id, RA_Bill_No=RA_Bill_No, + **record_data + ) + + print("Saving Row → Location:", record.Location, " Node_No:", record.Node_No) + + db.session.add(record) + saved_count += 1 + + db.session.commit() + + return True, ( + f"Manhole Domestic Chamber saved successfully. " + f"Inserted: {saved_count}, Skipped: {skipped_count}" + ) + + except Exception as e: + db.session.rollback() + return False, f"Manhole Domestic Chamber save failed: {e}" + # olds @@ -409,50 +488,50 @@ class FileService: # 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() + # 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()}" + # # 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 + # saved_count = 0 - try: - for index, row in df.iterrows(): - record_data = {} + # 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 + # # 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_data[col] = value - record = ManholeDomesticChamber( - subcontractor_id=subcontractor_id, - **record_data - ) + # record = ManholeDomesticChamber( + # subcontractor_id=subcontractor_id, + # **record_data + # ) - db.session.add(record) - saved_count += 1 + # db.session.add(record) + # saved_count += 1 - db.session.commit() - return True, f"Manhole Domestic Chamber Construction data saved successfully. Total rows: {saved_count}" + # 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}" + # except Exception as e: + # db.session.rollback() + # return False, f"Manhole Domestic Chamber Construction Save Failed: {e}" diff --git a/app/templates/file_import.html b/app/templates/file_import.html index aaa977c..dd9d12f 100644 --- a/app/templates/file_import.html +++ b/app/templates/file_import.html @@ -28,6 +28,9 @@ --> + + +