model as per sheet variable imort route call
This commit is contained in:
15
.gitignore
vendored
15
.gitignore
vendored
@@ -1,11 +1,20 @@
|
|||||||
|
# Python
|
||||||
|
app/__pycache__/
|
||||||
|
*.pyc
|
||||||
|
*.pyo
|
||||||
|
*.pyd
|
||||||
|
|
||||||
# Ingnor upload files
|
# Ingnor upload files
|
||||||
app/static/uploads/
|
app/static/uploads/
|
||||||
|
|
||||||
# Ignore files
|
# Ignore files
|
||||||
venv
|
venv
|
||||||
|
|
||||||
# Ignore Log files
|
# Ignore Log files ss
|
||||||
logs
|
logs/
|
||||||
|
|
||||||
# Ignore db folders
|
# Ignore db folders
|
||||||
instance
|
instance/
|
||||||
|
|
||||||
|
|
||||||
|
*.__pycache__
|
||||||
Binary file not shown.
@@ -38,6 +38,9 @@ class ManholeDomesticChamber(db.Model):
|
|||||||
d_9_16_to_9_65 = db.Column(db.Float)
|
d_9_16_to_9_65 = db.Column(db.Float)
|
||||||
|
|
||||||
Domestic_Chambers = 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)
|
created_at = db.Column(db.DateTime, default=datetime.today)
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@@ -12,11 +12,12 @@ def import_file():
|
|||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
file = request.files.get("file")
|
file = request.files.get("file")
|
||||||
subcontractor_id = request.form.get("subcontractor_id")
|
subcontractor_id = request.form.get("subcontractor_id")
|
||||||
|
RA_Bill_No = request.form.get("RA_Bill_No")
|
||||||
# file_type = request.form.get("file_type")
|
# file_type = request.form.get("file_type")
|
||||||
|
|
||||||
service = FileService()
|
service = FileService()
|
||||||
# success, msg = service.handle_file_upload(file, subcontractor_id, file_type)
|
# 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")
|
flash(msg, "success" if success else "danger")
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@@ -21,9 +21,9 @@ class FileService:
|
|||||||
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
|
||||||
|
|
||||||
# def handle_file_upload(self, file, subcontractor_id, file_type):
|
# 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:
|
if not subcontractor_id:
|
||||||
return False, "Please select subcontractor."
|
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_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_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("\n=== Uploaded File tr ex ===")
|
||||||
print(df_tr_ex.head())
|
print(df_tr_ex.head())
|
||||||
print("=============================\n")
|
print("=============================\n")
|
||||||
|
|
||||||
print("=== Uploaded File mh ex ===")
|
print("\n=== Uploaded File mh ex ===")
|
||||||
print(df_mh_ex.head())
|
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_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)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -96,8 +102,6 @@ class FileService:
|
|||||||
return False, f"Processing failed: {e}"
|
return False, f"Processing failed: {e}"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# new new
|
# new new
|
||||||
def process_trench_excavation(self, df, subcontractor_id,RA_Bill_No):
|
def process_trench_excavation(self, df, subcontractor_id,RA_Bill_No):
|
||||||
|
|
||||||
@@ -249,6 +253,81 @@ class FileService:
|
|||||||
db.session.rollback()
|
db.session.rollback()
|
||||||
return False, f"Manhole Excavation save failed: {e}"
|
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
|
# olds
|
||||||
@@ -409,50 +488,50 @@ class FileService:
|
|||||||
|
|
||||||
|
|
||||||
# Manhole and Domestic Chamber Construction save method (ManholeDomesticChamber model)
|
# Manhole and Domestic Chamber Construction save method (ManholeDomesticChamber model)
|
||||||
def process_manhole_domestic_chamber(self, df, subcontractor_id):
|
# def process_manhole_domestic_chamber(self, df, subcontractor_id):
|
||||||
# Clean column names (strip whitespace)
|
# # Clean column names (strip whitespace)
|
||||||
df.columns = [str(c).strip() for c in df.columns]
|
# df.columns = [str(c).strip() for c in df.columns]
|
||||||
# If the sheet has merged cells -> forward fill Location
|
# # If the sheet has merged cells -> forward fill Location
|
||||||
if "Location" in df.columns:
|
# if "Location" in df.columns:
|
||||||
df["Location"] = df["Location"].ffill()
|
# df["Location"] = df["Location"].ffill()
|
||||||
|
|
||||||
# REMOVE empty rows
|
# # REMOVE empty rows
|
||||||
df = df.dropna(how="all")
|
# df = df.dropna(how="all")
|
||||||
# Identify missing location rows before insert
|
# # Identify missing location rows before insert
|
||||||
missing_loc = df[df["Location"].isna() | (df["Location"].astype(str).str.strip() == "")]
|
# missing_loc = df[df["Location"].isna() | (df["Location"].astype(str).str.strip() == "")]
|
||||||
if not missing_loc.empty:
|
# if not missing_loc.empty:
|
||||||
return False, f"Error: Some rows have empty Location. Rows: {missing_loc.index.tolist()}"
|
# return False, f"Error: Some rows have empty Location. Rows: {missing_loc.index.tolist()}"
|
||||||
|
|
||||||
saved_count = 0
|
# saved_count = 0
|
||||||
|
|
||||||
try:
|
# try:
|
||||||
for index, row in df.iterrows():
|
# for index, row in df.iterrows():
|
||||||
record_data = {}
|
# record_data = {}
|
||||||
|
|
||||||
# Insert only fields that exist in model
|
# # Insert only fields that exist in model
|
||||||
for col in df.columns:
|
# for col in df.columns:
|
||||||
if hasattr(ManholeDomesticChamber, col):
|
# if hasattr(ManholeDomesticChamber, col):
|
||||||
value = row[col]
|
# value = row[col]
|
||||||
# Normalize empty values
|
# # Normalize empty values
|
||||||
if pd.isna(value) or str(value).strip() in ["", "-", "—", "nan", "NaN"]:
|
# if pd.isna(value) or str(value).strip() in ["", "-", "—", "nan", "NaN"]:
|
||||||
value = None
|
# value = None
|
||||||
|
|
||||||
record_data[col] = value
|
# record_data[col] = value
|
||||||
|
|
||||||
record = ManholeDomesticChamber(
|
# record = ManholeDomesticChamber(
|
||||||
subcontractor_id=subcontractor_id,
|
# subcontractor_id=subcontractor_id,
|
||||||
**record_data
|
# **record_data
|
||||||
)
|
# )
|
||||||
|
|
||||||
db.session.add(record)
|
# db.session.add(record)
|
||||||
saved_count += 1
|
# saved_count += 1
|
||||||
|
|
||||||
db.session.commit()
|
# db.session.commit()
|
||||||
return True, f"Manhole Domestic Chamber Construction data saved successfully. Total rows: {saved_count}"
|
# return True, f"Manhole Domestic Chamber Construction data saved successfully. Total rows: {saved_count}"
|
||||||
|
|
||||||
except Exception as e:
|
# except Exception as e:
|
||||||
db.session.rollback()
|
# db.session.rollback()
|
||||||
return False, f"Manhole Domestic Chamber Construction Save Failed: {e}"
|
# return False, f"Manhole Domestic Chamber Construction Save Failed: {e}"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,9 @@
|
|||||||
<option value="">Laying Sheet</option>
|
<option value="">Laying Sheet</option>
|
||||||
</select> -->
|
</select> -->
|
||||||
|
|
||||||
|
<label class="form-label">RA Bill No</label>
|
||||||
|
<input type="text" name="RA_Bill_No" class="form-control mb-3" required>
|
||||||
|
|
||||||
<!-- 3. FILE UPLOAD -->
|
<!-- 3. FILE UPLOAD -->
|
||||||
<label class="form-label">Choose File</label>
|
<label class="form-label">Choose File</label>
|
||||||
<input type="file" name="file" class="form-control mb-3" required>
|
<input type="file" name="file" class="form-control mb-3" required>
|
||||||
|
|||||||
Reference in New Issue
Block a user