Create user , user login register routes and pages
This commit is contained in:
@@ -18,14 +18,21 @@ from app.utils.file_utils import ensure_upload_folder
|
||||
|
||||
class FileService:
|
||||
|
||||
# ---------------- COMMON HELPERS ----------------
|
||||
def allowed_file(self, filename):
|
||||
return (
|
||||
"." in filename
|
||||
and filename.rsplit(".", 1)[1].lower() in Config.ALLOWED_EXTENSIONS
|
||||
)
|
||||
return ("." in filename and filename.rsplit(".", 1)[1].lower() in Config.ALLOWED_EXTENSIONS)
|
||||
|
||||
# -------------------------- SUBCONTRACTORFILE UPLOAD -------------------------
|
||||
# SUBCONTRACTOR FILE UPLOAD
|
||||
def normalize(self, val):
|
||||
if val is None or pd.isna(val):
|
||||
return None
|
||||
|
||||
val = str(val).strip()
|
||||
if val.lower() in ["", "nan", "none", "-", "—"]:
|
||||
return None
|
||||
|
||||
return val.upper()
|
||||
|
||||
# ---------------- SUBCONTRACTOR FILE UPLOAD ----------------
|
||||
def handle_file_upload(self, file, subcontractor_id, RA_Bill_No):
|
||||
|
||||
if not subcontractor_id:
|
||||
@@ -50,9 +57,9 @@ class FileService:
|
||||
file.save(filepath)
|
||||
|
||||
try:
|
||||
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)
|
||||
df_tr_ex = pd.read_excel(filepath, sheet_name="Tr.Ex.", header=12, dtype={"MH No": str})
|
||||
df_mh_ex = pd.read_excel(filepath, sheet_name="MH Ex.", header=12, dtype={"MH No": str})
|
||||
df_mh_dc = pd.read_excel(filepath, sheet_name="MH & DC", header=11, dtype={"MH No": str})
|
||||
|
||||
self.process_trench_excavation(df_tr_ex, subcontractor_id, RA_Bill_No)
|
||||
self.process_manhole_excavation(df_mh_ex, subcontractor_id, RA_Bill_No)
|
||||
@@ -62,9 +69,9 @@ class FileService:
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return False, f"Processing failed(SUBCONTRACTOR): {e}"
|
||||
return False, f"Import failed: {e}"
|
||||
|
||||
# Trench Excavation (Subcontractor)
|
||||
# ---------------- Trench Excavation (Subcontractor) ----------------
|
||||
def process_trench_excavation(self, df, subcontractor_id, RA_Bill_No):
|
||||
|
||||
df.columns = (
|
||||
@@ -80,42 +87,49 @@ class FileService:
|
||||
if "Location" in df.columns:
|
||||
df["Location"] = df["Location"].ffill()
|
||||
|
||||
try:
|
||||
for _, row in df.iterrows():
|
||||
location = row.get("Location")
|
||||
mh_no = row.get("MH_NO")
|
||||
errors = []
|
||||
|
||||
if (
|
||||
pd.isna(location)
|
||||
or str(location).strip() == ""
|
||||
or pd.isna(mh_no)
|
||||
or str(mh_no).strip() == ""
|
||||
):
|
||||
continue
|
||||
for idx, row in df.iterrows():
|
||||
location = self.normalize(row.get("Location"))
|
||||
mh_no = self.normalize(row.get("MH_NO"))
|
||||
|
||||
record_data = {}
|
||||
for col in df.columns:
|
||||
if hasattr(TrenchExcavation, col):
|
||||
val = row[col]
|
||||
if pd.isna(val) or str(val).strip() in ["", "-", "—", "nan"]:
|
||||
val = None
|
||||
record_data[col] = val
|
||||
if not location or not mh_no:
|
||||
continue
|
||||
|
||||
record = TrenchExcavation(
|
||||
subcontractor_id=subcontractor_id,
|
||||
RA_Bill_No=RA_Bill_No,
|
||||
**record_data,
|
||||
exists = TrenchExcavation.query.filter_by(
|
||||
subcontractor_id=subcontractor_id,
|
||||
RA_Bill_No=RA_Bill_No,
|
||||
Location=location,
|
||||
MH_NO=mh_no,
|
||||
).first()
|
||||
|
||||
if exists:
|
||||
errors.append(
|
||||
f"Model-Tr.Ex. (Row {idx+1}): Duplicate → Location={location}, MH_NO={mh_no}"
|
||||
)
|
||||
db.session.add(record)
|
||||
continue
|
||||
|
||||
db.session.commit()
|
||||
return True, "Trench Excavation saved successfully."
|
||||
record_data = {}
|
||||
for col in df.columns:
|
||||
if hasattr(TrenchExcavation, col):
|
||||
val = row[col]
|
||||
if pd.isna(val) or str(val).strip() in ["", "-", "—", "nan"]:
|
||||
val = None
|
||||
record_data[col] = val
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
raise e
|
||||
record = TrenchExcavation(
|
||||
subcontractor_id=subcontractor_id,
|
||||
RA_Bill_No=RA_Bill_No,
|
||||
**record_data,
|
||||
)
|
||||
db.session.add(record)
|
||||
|
||||
# Manhole Excavation (Subcontractor)
|
||||
if errors:
|
||||
raise Exception(" | ".join(errors))
|
||||
|
||||
db.session.commit()
|
||||
|
||||
# ---------------- Manhole Excavation (Subcontractor) ----------------
|
||||
def process_manhole_excavation(self, df, subcontractor_id, RA_Bill_No):
|
||||
|
||||
df.columns = (
|
||||
@@ -131,43 +145,49 @@ class FileService:
|
||||
if "Location" in df.columns:
|
||||
df["Location"] = df["Location"].ffill()
|
||||
|
||||
try:
|
||||
for _, row in df.iterrows():
|
||||
location = row.get("Location")
|
||||
mh_no = row.get("MH_NO")
|
||||
errors = []
|
||||
|
||||
if (
|
||||
pd.isna(location)
|
||||
or str(location).strip() == ""
|
||||
or pd.isna(mh_no)
|
||||
or str(mh_no).strip() == ""
|
||||
):
|
||||
continue
|
||||
for idx, row in df.iterrows():
|
||||
location = self.normalize(row.get("Location"))
|
||||
mh_no = self.normalize(row.get("MH_NO"))
|
||||
|
||||
record_data = {}
|
||||
for col in df.columns:
|
||||
if hasattr(ManholeExcavation, col):
|
||||
val = row[col]
|
||||
if pd.isna(val) or str(val).strip() in ["", "-", "—", "nan"]:
|
||||
val = None
|
||||
record_data[col] = val
|
||||
if not location or not mh_no:
|
||||
continue
|
||||
|
||||
record = ManholeExcavation(
|
||||
subcontractor_id=subcontractor_id,
|
||||
RA_Bill_No=RA_Bill_No,
|
||||
**record_data,
|
||||
exists = ManholeExcavation.query.filter_by(
|
||||
subcontractor_id=subcontractor_id,
|
||||
RA_Bill_No=RA_Bill_No,
|
||||
Location=location,
|
||||
MH_NO=mh_no,
|
||||
).first()
|
||||
|
||||
if exists:
|
||||
errors.append(
|
||||
f"Model-MH Ex. (Row {idx+1}): Duplicate → Location={location}, MH_NO={mh_no}"
|
||||
)
|
||||
db.session.add(record)
|
||||
continue
|
||||
|
||||
db.session.commit()
|
||||
return True, "Manhole Excavation saved successfully."
|
||||
record_data = {}
|
||||
for col in df.columns:
|
||||
if hasattr(ManholeExcavation, col):
|
||||
val = row[col]
|
||||
if pd.isna(val) or str(val).strip() in ["", "-", "—", "nan"]:
|
||||
val = None
|
||||
record_data[col] = val
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
raise e
|
||||
record = ManholeExcavation(
|
||||
subcontractor_id=subcontractor_id,
|
||||
RA_Bill_No=RA_Bill_No,
|
||||
**record_data,
|
||||
)
|
||||
db.session.add(record)
|
||||
|
||||
|
||||
# Manhole & Domestic Chamber (Subcontractor)
|
||||
if errors:
|
||||
raise Exception(" | ".join(errors))
|
||||
|
||||
db.session.commit()
|
||||
|
||||
# ---------------- Manhole & Domestic Chamber (Subcontractor) ----------------
|
||||
def process_manhole_domestic_chamber(self, df, subcontractor_id, RA_Bill_No):
|
||||
|
||||
df.columns = (
|
||||
@@ -183,43 +203,49 @@ class FileService:
|
||||
if "Location" in df.columns:
|
||||
df["Location"] = df["Location"].ffill()
|
||||
|
||||
try:
|
||||
for _, row in df.iterrows():
|
||||
location = row.get("Location")
|
||||
node_no = row.get("MH_NO")
|
||||
errors = []
|
||||
|
||||
if (
|
||||
pd.isna(location)
|
||||
or str(location).strip() == ""
|
||||
or pd.isna(node_no)
|
||||
or str(node_no).strip() == ""
|
||||
):
|
||||
continue
|
||||
for idx, row in df.iterrows():
|
||||
location = self.normalize(row.get("Location"))
|
||||
mh_no = self.normalize(row.get("MH_NO"))
|
||||
|
||||
record_data = {}
|
||||
for col in df.columns:
|
||||
if hasattr(ManholeDomesticChamber, col):
|
||||
val = row[col]
|
||||
if pd.isna(val) or str(val).strip() in ["", "-", "—", "nan"]:
|
||||
val = None
|
||||
record_data[col] = val
|
||||
if not location or not mh_no:
|
||||
continue
|
||||
|
||||
record = ManholeDomesticChamber(
|
||||
subcontractor_id=subcontractor_id,
|
||||
RA_Bill_No=RA_Bill_No,
|
||||
**record_data,
|
||||
exists = ManholeDomesticChamber.query.filter_by(
|
||||
subcontractor_id=subcontractor_id,
|
||||
RA_Bill_No=RA_Bill_No,
|
||||
Location=location,
|
||||
MH_NO=mh_no,
|
||||
).first()
|
||||
|
||||
if exists:
|
||||
errors.append(
|
||||
f"Model-MH & DC (Row {idx+1}): Duplicate → Location={location}, MH_NO={mh_no}"
|
||||
)
|
||||
db.session.add(record)
|
||||
continue
|
||||
|
||||
db.session.commit()
|
||||
return True, "Manhole Domestic Chamber saved successfully."
|
||||
record_data = {}
|
||||
for col in df.columns:
|
||||
if hasattr(ManholeDomesticChamber, col):
|
||||
val = row[col]
|
||||
if pd.isna(val) or str(val).strip() in ["", "-", "—", "nan"]:
|
||||
val = None
|
||||
record_data[col] = val
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
raise e
|
||||
record = ManholeDomesticChamber(
|
||||
subcontractor_id=subcontractor_id,
|
||||
RA_Bill_No=RA_Bill_No,
|
||||
**record_data,
|
||||
)
|
||||
db.session.add(record)
|
||||
|
||||
|
||||
# ------------------- CLIENT FILE UPLOAD --------------------------
|
||||
if errors:
|
||||
raise Exception(" | ".join(errors))
|
||||
|
||||
db.session.commit()
|
||||
|
||||
# ---------------- CLIENT FILE UPLOAD ----------------
|
||||
def handle_client_file_upload(self, file, RA_Bill_No):
|
||||
|
||||
if not RA_Bill_No:
|
||||
@@ -245,26 +271,18 @@ class FileService:
|
||||
df_mh_ex = pd.read_excel(filepath, sheet_name="MH Ex.", header=4)
|
||||
df_mh_dc = pd.read_excel(filepath, sheet_name="MH & DC", header=3)
|
||||
|
||||
print("------------------")
|
||||
print("df_tr_ex :",df_tr_ex)
|
||||
print("------------------")
|
||||
print("df_mh_ex :",df_mh_ex)
|
||||
print("------------------")
|
||||
print("df_mh_dc :",df_mh_dc)
|
||||
print("------------------")
|
||||
|
||||
self.save_client_data(df_tr_ex, TrenchExcavationClient, RA_Bill_No)
|
||||
self.save_client_data(df_mh_ex, ManholeExcavationClient, RA_Bill_No)
|
||||
self.save_client_data(df_mh_dc, ManholeDomesticChamberClient, RA_Bill_No)
|
||||
|
||||
db.session.commit()
|
||||
return True, "Client file uploaded and all data saved successfully."
|
||||
return True, "Client file uploaded successfully."
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return False, f"Client data save failed: {e}"
|
||||
return False, f"Client import failed: {e}"
|
||||
|
||||
# Client all model Save Method
|
||||
# ---------------- CLIENT SAVE METHOD ----------------
|
||||
def save_client_data(self, df, model, RA_Bill_No):
|
||||
|
||||
df.columns = [str(c).strip() for c in df.columns]
|
||||
@@ -274,23 +292,13 @@ class FileService:
|
||||
|
||||
df = df.dropna(how="all")
|
||||
|
||||
if "Location" in df.columns:
|
||||
missing_loc = df[
|
||||
df["Location"].isna()
|
||||
| (df["Location"].astype(str).str.strip() == "")
|
||||
]
|
||||
if not missing_loc.empty:
|
||||
raise Exception(
|
||||
f"{model.__name__}: Empty Location at rows {missing_loc.index.tolist()}"
|
||||
)
|
||||
|
||||
for _, row in df.iterrows():
|
||||
for idx, row in df.iterrows():
|
||||
record_data = {}
|
||||
|
||||
for col in df.columns:
|
||||
if hasattr(model, col):
|
||||
val = row[col]
|
||||
if pd.isna(val) or str(val).strip() in ["", "-", "—", "nan", "NaN"]:
|
||||
if pd.isna(val) or str(val).strip() in ["", "-", "—", "nan"]:
|
||||
val = None
|
||||
record_data[col] = val
|
||||
|
||||
|
||||
Reference in New Issue
Block a user