model class update total cal fields all model

This commit is contained in:
2026-02-25 15:27:59 +05:30
parent 163c7814ed
commit cf7d1636f9
8 changed files with 380 additions and 195 deletions

View File

@@ -1,5 +1,6 @@
from app import db
from datetime import datetime
from sqlalchemy import event
class TrenchExcavation(db.Model):
__tablename__ = "trench_excavation"
@@ -108,3 +109,17 @@ class TrenchExcavation(db.Model):
+ safe(self.Hard_Rock_6_0_to_7_5)
),
}
# ==========================================
# AUTO CALCULATE GRAND TOTAL
# ==========================================
def calculate_trench_total(mapper, connection, target):
total = 0
for column in target.__table__.columns:
if column.name.endswith("_total"):
total += getattr(target, column.name) or 0
target.Total = total
event.listen(TrenchExcavation, "before_insert", calculate_trench_total)
event.listen(TrenchExcavation, "before_update", calculate_trench_total)