create Mh.Ex model and MH & DC model and upload commit
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -1,11 +1,12 @@
|
||||
# app/services/file_service.py
|
||||
|
||||
import os
|
||||
import pandas as pd
|
||||
from werkzeug.utils import secure_filename
|
||||
from app.config import Config
|
||||
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.utils.file_utils import ensure_upload_folder
|
||||
|
||||
|
||||
@@ -41,8 +42,18 @@ class FileService:
|
||||
print(df.head())
|
||||
print("=============================\n")
|
||||
|
||||
# Trench Excavation save
|
||||
if file_type == "trench_excavation":
|
||||
return self.process_trench_excavation(df, subcontractor_id)
|
||||
|
||||
# Manhole Excavation save
|
||||
if file_type == "manhole_excavation":
|
||||
return self.process_manhole_excavation(df, subcontractor_id)
|
||||
|
||||
# Manhole and Domestic Chamber Construction save
|
||||
if file_type == "manhole_domestic_chamber":
|
||||
return self.process_manhole_excavation(df, subcontractor_id)
|
||||
|
||||
|
||||
return True, "File uploaded successfully."
|
||||
|
||||
@@ -51,7 +62,7 @@ class FileService:
|
||||
|
||||
|
||||
|
||||
# CLEAN & SAVE TRENCH EXCAVATION DATA
|
||||
# Trench Excavation save method (TrenchExcavation model)
|
||||
def process_trench_excavation(self, df, subcontractor_id):
|
||||
|
||||
# Clean column names (strip whitespace)
|
||||
@@ -101,3 +112,114 @@ class FileService:
|
||||
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_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(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}"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user