create regular exp added and change model

This commit is contained in:
2026-03-17 15:35:52 +05:30
parent c5b3e7bd60
commit 96a3a79731
9 changed files with 97 additions and 186 deletions

View File

@@ -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"
@@ -53,13 +51,13 @@ class LayingClient(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"
]
# 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
@@ -68,7 +66,7 @@ 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

View File

@@ -1,9 +1,8 @@
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,13 +48,13 @@ 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"
]
# 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
@@ -64,7 +63,7 @@ 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

View File

@@ -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,15 +53,15 @@ 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"]
# 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
@@ -73,7 +70,7 @@ 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

View File

@@ -88,7 +88,6 @@ class TrenchExcavationClient(db.Model):
# ==========================================
# AUTO CALCULATE GRAND TOTAL
# ==========================================
def calculate_trench_client_total(mapper, connection, target):
total = 0
for column in target.__table__.columns:

View File

@@ -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)

View File

@@ -15,16 +15,12 @@ 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
@@ -80,6 +76,9 @@ def build_comparison(client_rows, contractor_rows, key_field):
used_index = defaultdict(int) # 🔥 THIS FIXES YOUR ISSUE
for c in client_rows:
print("@@@@@@@@@@@@@@@@@@@@@@@")
print("c::::",c)
print("@@@@@@@@@@@@@@@@@@@@@@@")
client_location = normalize_key(c.get("Location"))
client_key = normalize_key(c.get(key_field))
@@ -104,16 +103,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 +137,18 @@ def build_comparison(client_rows, contractor_rows, key_field):
output.append(row)
df = pd.DataFrame(output)
print("-------------------")
print("df::",df)
print("++++++++++++++++++++++++s+++++")
for col in df.columns:
print("colll::",col)
df.columns = [format_header(col) for col in df.columns]
print("-------------------")
print("df.columns::",df.columns)
return df
@@ -255,105 +265,3 @@ 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

View File

@@ -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()

View File

@@ -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 {}
app.logger.info("Request Started")

10
app/utils/regex_utils.py Normal file
View File

@@ -0,0 +1,10 @@
import re
class RegularExpression:
# 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+)?$")