upload client side MH and MH DC model commit

This commit is contained in:
2025-12-14 18:04:03 +05:30
parent 1d476c4851
commit 14eac5b958
10 changed files with 266 additions and 117 deletions

View File

@@ -6,8 +6,11 @@ from app import db
from app.models.trench_excavation_model import TrenchExcavation
from app.models.manhole_excavation_model import ManholeExcavation
from app.models.manhole_domestic_chamber_model import ManholeDomesticChamber
from app.models.tr_ex_client_model import TrenchExcavationClient
from app.models.mh_ex_client_model import ManholeExcavationClient
from app.models.mh_dc_client_model import ManholeDomesticChamberClient
from app.utils.file_utils import ensure_upload_folder
@@ -56,14 +59,17 @@ class FileService:
if file_type == "manhole_domestic_chamber":
return self.process_manhole_domestic_chamber(df, subcontractor_id)
# Trench Excavation save (client)
# Tr Ex save (client)
if file_type =="tr_ex_client":
return self.client_trench_excavation(df, subcontractor_id)
# Manhole Excavation save (client)
# if file_type =="mh_ex_client":
# return self.client_manhole_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."
@@ -212,7 +218,7 @@ class FileService:
# ---------------------- client ----------------------------------
# Trench Excavation save method (TrenchExcavationClient model)
# 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
@@ -256,49 +262,96 @@ class FileService:
return False, f"Clinnt Tr Ex Save Failed: {e}"
# Manhole Excavation save method (ManholeExcavationClient model)
# def client_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()
# Mh Ex save method (ManholeExcavationClient model)
def client_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()}"
# 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 = {}
# # Insert only fields that exist in model
# for col in df.columns:
# if hasattr(ManholeExcavationClient, col):
# value = row[col]
try:
for index, row in df.iterrows():
record_data = {}
# Insert only fields that exist in model
for col in df.columns:
if hasattr(ManholeExcavationClient, col):
value = row[col]
# # Normalize empty values
# if pd.isna(value) or str(value).strip() in ["", "-", "—", "nan", "NaN"]:
# value = None
# 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 = ManholeExcavationClient(
# subcontractor_id=subcontractor_id,
# **record_data
# )
record = ManholeExcavationClient(
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" Client Mh Ex. data saved successfully. Total rows: {saved_count}"
db.session.commit()
return True, f" Client Mh Ex. data saved successfully. Total rows: {saved_count}"
# except Exception as e:
# db.session.rollback()
# return False, f"Client Mh Ex. Save Failed: {e}"
except Exception as e:
db.session.rollback()
return False, f"Client Mh Ex. Save Failed: {e}"
# Mh and Dc save method (ManholeDomesticChamberClient model)
def client_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(ManholeDomesticChamberClient, 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 = ManholeDomesticChamberClient(
subcontractor_id=subcontractor_id,
**record_data
)
db.session.add(record)
saved_count += 1
db.session.commit()
return True, f"Mh and Dc data saved successfully. Total rows: {saved_count}"
except Exception as e:
db.session.rollback()
return False, f"Mh and Dc data Save Failed: {e}"