diff --git a/.gitignore b/.gitignore index f349a89..6500874 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,6 @@ # Ignore folders instance/ -static/uploads/ - +app/static/uploads/ # Ignore files .env diff --git a/app/__init__.py b/app/__init__.py index fe63ee8..b589d00 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -29,23 +29,3 @@ def create_app(): return app - - - -# from flask import Flask -# from app.config import Config - -# def create_app(): -# app = Flask(__name__) -# app.config.from_object(Config) - -# # Register Blueprints -# from app.routes.dashboard import dashboard_bp -# from app.routes.file_import import file_import_bp -# from app.routes.user import user_bp - -# app.register_blueprint(dashboard_bp) -# app.register_blueprint(file_import_bp) -# app.register_blueprint(user_bp) - -# return app diff --git a/app/__pycache__/__init__.cpython-313.pyc b/app/__pycache__/__init__.cpython-313.pyc index 911bc21..e29c10b 100644 Binary files a/app/__pycache__/__init__.cpython-313.pyc and b/app/__pycache__/__init__.cpython-313.pyc differ diff --git a/app/__pycache__/config.cpython-313.pyc b/app/__pycache__/config.cpython-313.pyc index a5e209a..c811851 100644 Binary files a/app/__pycache__/config.cpython-313.pyc and b/app/__pycache__/config.cpython-313.pyc differ diff --git a/app/models/__pycache__/manhole_domestic_chamber_model.cpython-313.pyc b/app/models/__pycache__/manhole_domestic_chamber_model.cpython-313.pyc index 6ffca9a..cb81deb 100644 Binary files a/app/models/__pycache__/manhole_domestic_chamber_model.cpython-313.pyc and b/app/models/__pycache__/manhole_domestic_chamber_model.cpython-313.pyc differ diff --git a/app/models/manhole_domestic_chamber_model.py b/app/models/manhole_domestic_chamber_model.py index 201b34e..319c54c 100644 --- a/app/models/manhole_domestic_chamber_model.py +++ b/app/models/manhole_domestic_chamber_model.py @@ -15,7 +15,7 @@ class ManholeDomesticChamber(db.Model): Node_No = db.Column(db.String(100)) Depth_of_MH = db.Column(db.Float) - # Excavation categories + # Excavation categories d_0_to_0_75 = db.Column(db.Float) d_1_06_to_1_65 = db.Column(db.Float) d_1_06_to_1_65 = db.Column(db.Float) diff --git a/app/routes/__pycache__/file_report.cpython-313.pyc b/app/routes/__pycache__/file_report.cpython-313.pyc index e83b2f1..9ff4879 100644 Binary files a/app/routes/__pycache__/file_report.cpython-313.pyc and b/app/routes/__pycache__/file_report.cpython-313.pyc differ diff --git a/app/routes/file_report.py b/app/routes/file_report.py index 2a6cd1d..2e8555c 100644 --- a/app/routes/file_report.py +++ b/app/routes/file_report.py @@ -1,10 +1,16 @@ -# app/routes/file_report.py - -from flask import Blueprint, render_template, request, flash +from flask import Blueprint, render_template, request, send_file, flash from app.models.subcontractor_model import Subcontractor +from app.models.manhole_excavation_model import ManholeExcavation +from app.models.trench_excavation_model import TrenchExcavation +from app.models.manhole_domestic_chamber_model import ManholeDomesticChamber +from app import db + +import pandas as pd +import io file_report_bp = Blueprint("file_report", __name__, url_prefix="/file") + @file_report_bp.route("/report", methods=["GET", "POST"]) def report_file(): subcontractors = Subcontractor.query.all() @@ -14,9 +20,110 @@ def report_file(): if not subcontractor_id: flash("Please select a subcontractor.", "danger") - else: - flash(f"Report generated for Subcontractor ID: {subcontractor_id}", "success") + return render_template("report.html", subcontractors=subcontractors) - return render_template("report.html", title="Report Download", subcontractors=subcontractors) + # Fetch subcontractor for file name + subcontractor = Subcontractor.query.get(subcontractor_id) + + manhole_excavation = ManholeExcavation.query.filter_by(subcontractor_id=subcontractor_id).all() + trench_excavation = TrenchExcavation.query.filter_by(subcontractor_id=subcontractor_id).all() + domestic_chamber = ManholeDomesticChamber.query.filter_by(subcontractor_id=subcontractor_id).all() + + + # Convert to DataFrame + df_mh_exc = pd.DataFrame([m.__dict__ for m in manhole_excavation]) + df_trench = pd.DataFrame([t.__dict__ for t in trench_excavation]) + df_domestic = pd.DataFrame([d.__dict__ for d in domestic_chamber]) + + # Drop unnecessary SQLAlchemy fields + drop_cols = ["id", "subcontractor_id", "created_at", "_sa_instance_state","Remarks"] + + df_mh_exc.drop(columns=drop_cols, errors="ignore", inplace=True) + df_trench.drop(columns=drop_cols, errors="ignore", inplace=True) + df_domestic.drop(columns=drop_cols, errors="ignore", inplace=True) + + + mh_exc_columns = [ + "Location", "MH_NO", "Upto_IL_Depth", "Cutting_Depth", "ID_of_MH_m", + "Ex_Dia_of_Manhole", "Area_of_Manhole", + + "Soft_Murum_0_to_1_5", "Soft_Murum_1_5_to_3_0", "Soft_Murum_3_0_to_4_5", + "Hard_Murum_0_to_1_5", "Hard_Murum_1_5_to_3_0", + "Soft_Rock_0_to_1_5", "Soft_Rock_1_5_to_3_0", + "Hard_Rock_0_to_1_5", "Hard_Rock_1_5_to_3_0", + "Hard_Rock_3_0_to_4_5", "Hard_Rock_4_5_to_6_0", "Hard_Rock_6_0_to_7_5", + + "Soft_Murum_0_to_1_5_total", "Soft_Murum_1_5_to_3_0_total", + "Soft_Murum_3_0_to_4_5_total", + "Hard_Murum_0_to_1_5_total", "Hard_Murum_1_5_and_above_total", + "Soft_Rock_0_to_1_5_total", "Soft_Rock_1_5_and_above_total", + "Hard_Rock_0_to_1_5_total", "Hard_Rock_1_5_and_above_total", + "Hard_Rock_3_0_to_4_5_total", "Hard_Rock_4_5_to_6_0_total", + "Hard_Rock_6_0_to_7_5_total", + + "Remarks", "Total" + ] + + trench_columns = [ + "Location", "MH_NO", "CC_length", "Invert_Level", "MH_Top_Level", + "Ground_Level", "ID_of_MH_m", "Actual_Trench_Length", "Pipe_Dia_mm", + + "Width_0_to_2_5", "Width_2_5_to_3_0", "Width_3_0_to_4_5", "Width_4_5_to_6_0", + + "Upto_IL_Depth", "Cutting_Depth", "Avg_Depth", + + "Soft_Murum_0_to_1_5", "Soft_Murum_1_5_to_3_0", "Soft_Murum_3_0_to_4_5", + "Hard_Murum_0_to_1_5", "Hard_Murum_1_5_to_3_0", + + "Soft_Rock_0_to_1_5", "Soft_Rock_1_5_to_3_0", + + "Hard_Rock_0_to_1_5", "Hard_Rock_1_5_to_3_0", + "Hard_Rock_3_0_to_4_5", "Hard_Rock_4_5_to_6_0", "Hard_Rock_6_0_to_7_5", + + "Soft_Murum_0_to_1_5_total", "Soft_Murum_1_5_to_3_0_total", + "Soft_Murum_3_0_to_4_5_total", + "Hard_Murum_0_to_1_5_total", "Hard_Murum_1_5_and_above_total", + "Soft_Rock_0_to_1_5_total", "Soft_Rock_1_5_and_above_total", + "Hard_Rock_0_to_1_5_total", "Hard_Rock_1_5_and_above_total", + "Hard_Rock_3_0_to_4_5_total", "Hard_Rock_4_5_to_6_0_total", + "Hard_Rock_6_0_to_7_5_total", + + "Remarks", "Total" + ] + + domestic_columns = [ + "Location", "Node_No", "Depth_of_MH", + "d_0_to_0_75", "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", + "Domestic_Chambers" + ] + + # Reorder columns serial wise + df_mh_exc = df_mh_exc.reindex(columns=mh_exc_columns, fill_value="") + df_trench = df_trench.reindex(columns=trench_columns, fill_value="") + df_domestic = df_domestic.reindex(columns=domestic_columns, fill_value="") + + # WRITE EXCEL FILE + output = io.BytesIO() + file_name = f"{subcontractor.subcontractor_name}_Report.xlsx" + + with pd.ExcelWriter(output, engine="xlsxwriter") as writer: + df_trench.to_excel(writer, index=False, sheet_name="Tr.Ex.") + df_mh_exc.to_excel(writer, index=False, sheet_name="MH.Ex.") + df_domestic.to_excel(writer, index=False, sheet_name="MH & DC") + + output.seek(0) + + return send_file( + output, + download_name=file_name, + as_attachment=True, + mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + ) + + return render_template("report.html", subcontractors=subcontractors) - return render_template("report.html", title="Report Download", subcontractors=subcontractors) diff --git a/app/templates/base.html b/app/templates/base.html index 2f5941b..f77028f 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -37,7 +37,6 @@ - @@ -62,4 +61,5 @@ - \ No newline at end of file + + diff --git a/app/templates/report.html b/app/templates/report.html index a2a6d11..e6dd6c4 100644 --- a/app/templates/report.html +++ b/app/templates/report.html @@ -1,24 +1,39 @@ {% extends "base.html" %} {% block content %} -

File Report

+
-
+

File Report

-
+ + {% with messages = get_flashed_messages(with_categories=true) %} + {% if messages %} + {% for category, message in messages %} + + {% endfor %} + {% endif %} + {% endwith %} - - - + - -
+ + + + + + + +
{% endblock %} \ No newline at end of file