diff --git a/app/models/laying_client_model.py b/app/models/laying_client_model.py index 0260433..1cfa15b 100644 --- a/app/models/laying_client_model.py +++ b/app/models/laying_client_model.py @@ -1,9 +1,7 @@ from app import db from datetime import datetime from sqlalchemy import event -import re -# REGEX PATTERN -PIPE_MM_PATTERN = re.compile(r"^pipe_\d+_mm$") +from app.utils.regex_utils import RegularExpression class LayingClient(db.Model): __tablename__ = "laying_client" @@ -52,25 +50,13 @@ class LayingClient(db.Model): def serialize(self): return {c.name: getattr(self, c.name) for c in self.__table__.columns} - - def sum_laying_fields(): - return [ - "pipe_150_mm", "pipe_200_mm", "pipe_250_mm", - "pipe_300_mm", "pipe_350_mm", "pipe_400_mm", - "pipe_450_mm", "pipe_500_mm", "pipe_600_mm", - "pipe_700_mm", "pipe_900_mm", "pipe_1200_mm" - ] - -# =============================== + # AUTO TOTAL USING REGEX -# =============================== def calculate_laying_total(mapper, connection, target): total = 0 - for column in target.__table__.columns: - if PIPE_MM_PATTERN.match(column.name): + if RegularExpression.PIPE_MM_PATTERN.match(column.name): total += getattr(target, column.name) or 0 - target.Total = total event.listen(LayingClient, "before_insert", calculate_laying_total) diff --git a/app/models/laying_model.py b/app/models/laying_model.py index fc1d3c7..e159c43 100644 --- a/app/models/laying_model.py +++ b/app/models/laying_model.py @@ -1,9 +1,7 @@ from app import db from datetime import datetime from sqlalchemy import event -import re -# REGEX PATTERN -PIPE_MM_PATTERN = re.compile(r"^pipe_\d+_mm$") +from app.utils.regex_utils import RegularExpression class Laying(db.Model): __tablename__ = "laying" @@ -49,24 +47,12 @@ class Laying(db.Model): return {c.name: getattr(self, c.name) for c in self.__table__.columns} - def sum_laying_fields(): - return [ - "pipe_150_mm", "pipe_200_mm", "pipe_250_mm", - "pipe_300_mm", "pipe_350_mm", "pipe_400_mm", - "pipe_450_mm", "pipe_500_mm", "pipe_600_mm", - "pipe_700_mm", "pipe_900_mm", "pipe_1200_mm" - ] - -# =============================== # AUTO TOTAL USING REGEX -# =============================== def calculate_laying_total(mapper, connection, target): total = 0 - for column in target.__table__.columns: - if PIPE_MM_PATTERN.match(column.name): + if RegularExpression.PIPE_MM_PATTERN.match(column.name): total += getattr(target, column.name) or 0 - target.Total = total event.listen(Laying, "before_insert", calculate_laying_total) diff --git a/app/models/manhole_domestic_chamber_model.py b/app/models/manhole_domestic_chamber_model.py index a5ae5ef..5303420 100644 --- a/app/models/manhole_domestic_chamber_model.py +++ b/app/models/manhole_domestic_chamber_model.py @@ -1,10 +1,7 @@ from app import db from datetime import datetime from sqlalchemy import event -import re - -# REGEX PATTERN -D_RANGE_PATTERN = re.compile(r"^d_\d+(?:_\d+)?_to_\d+(?:_\d+)?$") +from app.utils.regex_utils import RegularExpression class ManholeDomesticChamber(db.Model): __tablename__ = "manhole_domestic_chamber" @@ -56,28 +53,13 @@ class ManholeDomesticChamber(db.Model): return {c.name: getattr(self, c.name) for c in self.__table__.columns} - def sum_mh_dc_fields(): - return [ - "d_0_to_0_75", "d_0_76_to_1_05", "d_1_06_to_1_65", - "d_1_66_to_2_15", "d_2_16_to_2_65", "d_2_66_to_3_15", - "d_3_16_to_3_65", "d_3_66_to_4_15", "d_4_16_to_4_65", - "d_4_66_to_5_15", "d_5_16_to_5_65", "d_5_66_to_6_15", - "d_6_16_to_6_65", "d_6_66_to_7_15", "d_7_16_to_7_65", - "d_7_66_to_8_15", "d_8_16_to_8_65", "d_8_66_to_9_15", - "d_9_16_to_9_65"] - -# =============================== # AUTO TOTAL USING REGEX -# =============================== def calculate_mh_dc_total(mapper, connection, target): total = 0 - for column in target.__table__.columns: - if D_RANGE_PATTERN.match(column.name): + if RegularExpression.D_RANGE_PATTERN.match(column.name): total += getattr(target, column.name) or 0 - target.Total = total - event.listen(ManholeDomesticChamber, "before_insert", calculate_mh_dc_total) event.listen(ManholeDomesticChamber, "before_update", calculate_mh_dc_total) \ No newline at end of file diff --git a/app/models/manhole_excavation_model.py b/app/models/manhole_excavation_model.py index d0b008f..15839a2 100644 --- a/app/models/manhole_excavation_model.py +++ b/app/models/manhole_excavation_model.py @@ -1,6 +1,7 @@ from app import db from datetime import datetime from sqlalchemy import event +from app.utils.regex_utils import RegularExpression class ManholeExcavation(db.Model): __tablename__ = "manhole_excavation" @@ -67,13 +68,11 @@ class ManholeExcavation(db.Model): def serialize(self): return {c.name: getattr(self, c.name) for c in self.__table__.columns} -# ========================================== -# AUTO CALCULATE GRAND TOTAL -# ========================================== +# AUTO TOTAL USING REGEX def calculate_Manhole_total(mapper, connection, target): total = 0 for column in target.__table__.columns: - if column.name.endswith("_total"): + if RegularExpression.STR_TOTAL_PATTERN.match(column.name): total += getattr(target, column.name) or 0 target.Total = total diff --git a/app/models/mh_dc_client_model.py b/app/models/mh_dc_client_model.py index 6acaaa6..0fb90ee 100644 --- a/app/models/mh_dc_client_model.py +++ b/app/models/mh_dc_client_model.py @@ -1,19 +1,12 @@ from app import db from datetime import datetime from sqlalchemy import event -import re - -# REGEX PATTERN -D_RANGE_PATTERN = re.compile(r"^d_\d+(?:_\d+)?_to_\d+(?:_\d+)?$") +from app.utils.regex_utils import RegularExpression class ManholeDomesticChamberClient(db.Model): __tablename__ = "mh_dc_client" id = db.Column(db.Integer, primary_key=True) - # Foreign Key to Subcontractor table - # subcontractor_id = db.Column(db.Integer, db.ForeignKey("subcontractors.id"), nullable=False) - # Relationship for easy access (subcontractor.subcontractor_name) - # subcontractor = db.relationship("Subcontractor", backref="mh_dc_records") # Basic Fields RA_Bill_No=db.Column(db.String(500)) @@ -23,7 +16,6 @@ class ManholeDomesticChamberClient(db.Model): MH_IL_LEVEL = db.Column(db.Float, default=0) Depth_of_MH = db.Column(db.Float, default=0) - # Excavation categories d_0_to_1_5 = db.Column(db.Float, default=0) d_1_5_to_2_0 = db.Column(db.Float, default=0) @@ -49,27 +41,13 @@ class ManholeDomesticChamberClient(db.Model): def serialize(self): return {c.name: getattr(self, c.name) for c in self.__table__.columns} - - def sum_mh_dc_fields(): - return [ - "d_0_to_0_75", "d_0_76_to_1_05", "d_1_06_to_1_65", - "d_1_66_to_2_15", "d_2_16_to_2_65", "d_2_66_to_3_15", - "d_3_16_to_3_65", "d_3_66_to_4_15", "d_4_16_to_4_65", - "d_4_66_to_5_15", "d_5_16_to_5_65", "d_5_66_to_6_15", - "d_6_16_to_6_65", "d_6_66_to_7_15", "d_7_16_to_7_65", - "d_7_66_to_8_15", "d_8_16_to_8_65", "d_8_66_to_9_15", - "d_9_16_to_9_65" ] - -# =============================== + # AUTO TOTAL USING REGEX -# =============================== def calculate_mh_dc_total(mapper, connection, target): total = 0 - for column in target.__table__.columns: - if D_RANGE_PATTERN.match(column.name): + if RegularExpression.D_RANGE_PATTERN.match(column.name): total += getattr(target, column.name) or 0 - target.Total = total diff --git a/app/models/mh_ex_client_model.py b/app/models/mh_ex_client_model.py index 7ce2657..d7707c7 100644 --- a/app/models/mh_ex_client_model.py +++ b/app/models/mh_ex_client_model.py @@ -1,15 +1,12 @@ from app import db from datetime import datetime from sqlalchemy import event +from app.utils.regex_utils import RegularExpression class ManholeExcavationClient(db.Model): __tablename__ = "mh_ex_client" id = db.Column(db.Integer, primary_key=True) - # Foreign Key to Subcontractor table - # subcontractor_id = db.Column(db.Integer, db.ForeignKey("subcontractors.id"), nullable=False) - # Relationship for easy access (subcontractor.subcontractor_name) - # subcontractor = db.relationship("Subcontractor", backref="mh_ex_records") # Basic Fields RA_Bill_No=db.Column(db.String(500)) @@ -84,13 +81,11 @@ class ManholeExcavationClient(db.Model): return {c.name: getattr(self, c.name) for c in self.__table__.columns} -# ========================================== -# AUTO CALCULATE GRAND TOTAL -# ========================================== +# AUTO TOTAL USING REGEX def calculate_Manhole_total(mapper, connection, target): total = 0 for column in target.__table__.columns: - if column.name.endswith("_total"): + if RegularExpression.STR_TOTAL_PATTERN.match(column.name): total += getattr(target, column.name) or 0 target.Total = total diff --git a/app/models/tr_ex_client_model.py b/app/models/tr_ex_client_model.py index 6c18c36..4f943d0 100644 --- a/app/models/tr_ex_client_model.py +++ b/app/models/tr_ex_client_model.py @@ -1,6 +1,7 @@ from app import db from datetime import datetime from sqlalchemy import event +from app.utils.regex_utils import RegularExpression class TrenchExcavationClient(db.Model): __tablename__ = "tr_ex_client" @@ -85,17 +86,13 @@ class TrenchExcavationClient(db.Model): return {c.name: getattr(self, c.name) for c in self.__table__.columns} -# ========================================== -# AUTO CALCULATE GRAND TOTAL -# ========================================== - +# AUTO TOTAL USING REGEX def calculate_trench_client_total(mapper, connection, target): total = 0 for column in target.__table__.columns: - if column.name.endswith("_total"): + if RegularExpression.STR_TOTAL_PATTERN.match(column.name): total += getattr(target, column.name) or 0 target.Total = total - event.listen(TrenchExcavationClient, "before_insert", calculate_trench_client_total) event.listen(TrenchExcavationClient, "before_update", calculate_trench_client_total) \ No newline at end of file diff --git a/app/models/trench_excavation_model.py b/app/models/trench_excavation_model.py index c7a19c0..b3959ef 100644 --- a/app/models/trench_excavation_model.py +++ b/app/models/trench_excavation_model.py @@ -1,6 +1,7 @@ from app import db from datetime import datetime from sqlalchemy import event +from app.utils.regex_utils import RegularExpression class TrenchExcavation(db.Model): __tablename__ = "trench_excavation" @@ -79,44 +80,12 @@ class TrenchExcavation(db.Model): def serialize(self): return {c.name: getattr(self, c.name) for c in self.__table__.columns} - def excavation_category_sums(self): - def safe(val): - return val or 0 - - return { - "Soft_Murum_Total": ( - safe(self.Soft_Murum_0_to_1_5) - + safe(self.Soft_Murum_1_5_to_3_0) - + safe(self.Soft_Murum_3_0_to_4_5) - ), - - "Hard_Murum_Total": ( - safe(self.Hard_Murum_0_to_1_5) - + safe(self.Hard_Murum_1_5_to_3_0) - ), - - "Soft_Rock_Total": ( - safe(self.Soft_Rock_0_to_1_5) - + safe(self.Soft_Rock_1_5_to_3_0) - ), - - "Hard_Rock_Total": ( - safe(self.Hard_Rock_0_to_1_5) - + safe(self.Hard_Rock_1_5_to_3_0) - + safe(self.Hard_Rock_3_0_to_4_5) - + safe(self.Hard_Rock_4_5_to_6_0) - + safe(self.Hard_Rock_6_0_to_7_5) - ), - } - -# ========================================== -# AUTO CALCULATE GRAND TOTAL -# ========================================== +# AUTO TOTAL USING REGEX def calculate_trench_total(mapper, connection, target): total = 0 for column in target.__table__.columns: - if column.name.endswith("_total"): + if RegularExpression.STR_TOTAL_PATTERN.match(column.name): total += getattr(target, column.name) or 0 target.Total = total diff --git a/app/routes/dashboard.py b/app/routes/dashboard.py index 219a41d..af6f6d7 100644 --- a/app/routes/dashboard.py +++ b/app/routes/dashboard.py @@ -1,7 +1,7 @@ import matplotlib matplotlib.use("Agg") -from flask import Blueprint, render_template, session, redirect, url_for, jsonify +from flask import Blueprint, render_template, session, redirect, url_for, jsonify, request import matplotlib.pyplot as plt import io import base64 @@ -93,6 +93,9 @@ def subcontractor_chart(): if subcontractor_id: query = query.filter(TrenchExcavation.subcontractor_id == subcontractor_id) + if category: + query = query.filter(TrenchExcavation.subcontractor_id == subcontractor_id) + if ra_bill: query = query.filter(TrenchExcavation.RA_Bill_No == ra_bill) diff --git a/app/routes/generate_comparison_report.py b/app/routes/generate_comparison_report.py index 47ec5c8..c48c986 100644 --- a/app/routes/generate_comparison_report.py +++ b/app/routes/generate_comparison_report.py @@ -2,7 +2,7 @@ from flask import Blueprint, render_template, request, send_file, flash from collections import defaultdict import pandas as pd import io - + from app.models.subcontractor_model import Subcontractor from app.models.trench_excavation_model import TrenchExcavation from app.models.manhole_excavation_model import ManholeExcavation @@ -15,17 +15,13 @@ from app.models.mh_dc_client_model import ManholeDomesticChamberClient from app.models.laying_client_model import LayingClient from app.utils.helpers import login_required -import re +from app.utils.regex_utils import RegularExpression generate_report_bp = Blueprint("generate_report", __name__, url_prefix="/report") -# sum field of pipe laying (pipe_150_mm) -PIPE_MM_PATTERN = re.compile(r"^pipe_\d+_mm$") -# sum fields of MH dc (d_0_to_0_75) -D_RANGE_PATTERN = re.compile( r"^d_\d+(?:_\d+)?_to_\d+(?:_\d+)?$") - + # NORMALIZER def normalize_key(value): @@ -80,6 +76,7 @@ def build_comparison(client_rows, contractor_rows, key_field): used_index = defaultdict(int) # 🔥 THIS FIXES YOUR ISSUE for c in client_rows: + client_location = normalize_key(c.get("Location")) client_key = normalize_key(c.get(key_field)) @@ -104,16 +101,16 @@ def build_comparison(client_rows, contractor_rows, key_field): float(v or 0) for k, v in c.items() if k.endswith("_total") - or D_RANGE_PATTERN.match(k) - or PIPE_MM_PATTERN.match(k) + or RegularExpression.D_RANGE_PATTERN.match(k) + or RegularExpression.PIPE_MM_PATTERN.match(k) ) sub_total = sum( float(v or 0) for k, v in s.items() if k.endswith("_total") - or D_RANGE_PATTERN.match(k) - or PIPE_MM_PATTERN.match(k) + or RegularExpression.D_RANGE_PATTERN.match(k) + or RegularExpression.PIPE_MM_PATTERN.match(k) ) row = { @@ -138,7 +135,9 @@ def build_comparison(client_rows, contractor_rows, key_field): output.append(row) df = pd.DataFrame(output) + # formatting headers df.columns = [format_header(col) for col in df.columns] + return df @@ -254,106 +253,4 @@ def comparison_report(): ) return render_template("generate_comparison_report.html",subcontractors=subcontractors) - - -# def build_comparison_mh_dc(client_rows, contractor_rows, key_field): -# contractor_lookup = make_lookup(contractor_rows, key_field) -# mh_dc_fields = ManholeDomesticChamberClient.sum_mh_dc_fields() - -# output = [] - -# for c in client_rows: -# loc = normalize_key(c.get("Location")) -# key = normalize_key(c.get(key_field)) -# if not loc or not key: -# continue - -# s = contractor_lookup.get((loc, key)) -# if not s: -# continue - -# client_total = sum(float(c.get(f, 0) or 0) for f in mh_dc_fields) -# sub_total = sum(float(s.get(f, 0) or 0) for f in mh_dc_fields) - -# row = { -# "Location": loc, -# key_field.replace("_", " "): key -# } - -# # CLIENT – ALL FIELDS -# for k, v in c.items(): -# if k in ["id", "created_at"]: -# continue -# row[f"Client-{k}"] = v - -# row["Client-Total"] = round(client_total, 2) -# row[" "] = "" - -# # SUBCONTRACTOR – ALL FIELDS -# for k, v in s.items(): -# if k in ["id", "created_at", "subcontractor_id"]: -# continue -# row[f"Subcontractor-{k}"] = v - -# row["Subcontractor-Total"] = round(sub_total, 2) -# row["Diff"] = round(client_total - sub_total, 2) - -# output.append(row) - -# df = pd.DataFrame(output) -# df.columns = [format_header(col) for col in df.columns] -# return df - - -# def build_comparison_laying(client_rows, contractor_rows, key_field): -# contractor_lookup = make_lookup(contractor_rows, key_field) -# laying_fields = Laying.sum_laying_fields() - -# output = [] - -# for c in client_rows: -# loc = normalize_key(c.get("Location")) -# key = normalize_key(c.get(key_field)) -# if not loc or not key: -# continue - -# s = contractor_lookup.get((loc, key)) -# if not s: -# continue - -# client_total = sum(float(c.get(f, 0) or 0) for f in laying_fields) -# sub_total = sum(float(s.get(f, 0) or 0) for f in laying_fields) - -# print("--------------",key,"----------") -# print("sum -client_total ",client_total) -# print("sum -sub_total ",sub_total) -# print("Diff ---- ",client_total - sub_total) -# print("------------------------") -# row = { -# "Location": loc, -# key_field.replace("_", " "): key -# } - -# # CLIENT – ALL FIELDS -# for k, v in c.items(): -# if k in ["id", "created_at"]: -# continue -# row[f"Client-{k}"] = v - -# row["Client-Total"] = round(client_total, 2) -# row[" "] = "" - -# # SUBCONTRACTOR – ALL FIELDS -# for k, v in s.items(): -# if k in ["id", "created_at", "subcontractor_id"]: -# continue -# row[f"Subcontractor-{k}"] = v - -# row["Subcontractor-Total"] = round(sub_total, 2) -# row["Diff"] = round(client_total - sub_total, 2) - -# output.append(row) - -# df = pd.DataFrame(output) -# df.columns = [format_header(col) for col in df.columns] -# return df \ No newline at end of file + \ No newline at end of file diff --git a/app/routes/subcontractor_routes.py b/app/routes/subcontractor_routes.py index cdf7e2b..9a97efa 100644 --- a/app/routes/subcontractor_routes.py +++ b/app/routes/subcontractor_routes.py @@ -121,7 +121,7 @@ def update_subcontractor(id): subcontractor.mobile_no = request.form.get("mobile_no") subcontractor.email_id = request.form.get("email_id") subcontractor.gst_no = request.form.get("gst_no") - subcontractor.gst_no = request.form.get("pan_no") + subcontractor.pan_no = request.form.get("pan_no") subcontractor.status = request.form.get("status") db.session.commit() diff --git a/app/services/logger_service.py b/app/services/logger_service.py index 347f396..5b7da1b 100644 --- a/app/services/logger_service.py +++ b/app/services/logger_service.py @@ -1,20 +1,25 @@ import os import logging from logging.handlers import RotatingFileHandler -from flask import request, session +from flask import request, session, has_request_context -class RequestFormatter(logging.Formatter): - """ - Custom formatter to safely inject request data - """ +class RequestContextFilter(logging.Filter): - def format(self, record): - record.user = getattr(record, "user", "Anonymous") - record.ip = getattr(record, "ip", "N/A") - record.method = getattr(record, "method", "N/A") - record.url = getattr(record, "url", "N/A") - return super().format(record) + def filter(self, record): + + if has_request_context(): + record.user = session.get("user_email", "Anonymous") + record.ip = request.remote_addr + record.method = request.method + record.url = request.url + else: + record.user = "System" + record.ip = "N/A" + record.method = "N/A" + record.url = "N/A" + + return True class LoggerService: @@ -22,18 +27,17 @@ class LoggerService: @staticmethod def init_app(app): - # Create logs folder if not exists if not os.path.exists("logs"): os.makedirs("logs") - formatter = RequestFormatter( + formatter = logging.Formatter( "%(asctime)s | %(levelname)s | " "User:%(user)s | IP:%(ip)s | " "Method:%(method)s | URL:%(url)s | " "%(message)s" ) - # 🔹 INFO LOG + # INFO LOG info_handler = RotatingFileHandler( "logs/app.log", maxBytes=5 * 1024 * 1024, @@ -42,7 +46,7 @@ class LoggerService: info_handler.setLevel(logging.INFO) info_handler.setFormatter(formatter) - # 🔹 ERROR LOG + # ERROR LOG error_handler = RotatingFileHandler( "logs/error.log", maxBytes=5 * 1024 * 1024, @@ -51,32 +55,25 @@ class LoggerService: error_handler.setLevel(logging.ERROR) error_handler.setFormatter(formatter) - # 🔹 CONSOLE LOG + # CONSOLE console_handler = logging.StreamHandler() console_handler.setLevel(logging.DEBUG) console_handler.setFormatter(formatter) + # 🔹 ADD FILTER (important) + context_filter = RequestContextFilter() + + info_handler.addFilter(context_filter) + error_handler.addFilter(context_filter) + console_handler.addFilter(context_filter) + app.logger.setLevel(logging.DEBUG) + app.logger.addHandler(info_handler) app.logger.addHandler(error_handler) app.logger.addHandler(console_handler) - # Auto request logging + # Log every request automatically @app.before_request def log_request(): - app.logger.info( - "Request Started", - extra=LoggerService.get_request_data() - ) - - @staticmethod - def get_request_data(): - try: - return { - "user": session.get("user_email", "Anonymous"), - "ip": request.remote_addr, - "method": request.method, - "url": request.url - } - except: - return {} \ No newline at end of file + app.logger.info("Request Started") \ No newline at end of file diff --git a/app/templates/users.htm b/app/templates/users.html similarity index 100% rename from app/templates/users.htm rename to app/templates/users.html diff --git a/app/utils/regex_utils.py b/app/utils/regex_utils.py new file mode 100644 index 0000000..f6eb7b1 --- /dev/null +++ b/app/utils/regex_utils.py @@ -0,0 +1,12 @@ +import re + +class RegularExpression: + + # sum fields of TrEx, MhEx (_total) + STR_TOTAL_PATTERN = re.compile(r".*_total$") + + # sum fields of pipe laying (pipe_150_mm) + PIPE_MM_PATTERN = re.compile(r"^pipe_\d+_mm$") + + # sum fields of MH dc (d_0_to_0_75) + D_RANGE_PATTERN = re.compile( r"^d_\d+(?:_\d+)?_to_\d+(?:_\d+)?$") \ No newline at end of file