modification of code and loggger apply and changes of update from.
This commit is contained in:
111
main.py
111
main.py
@@ -1,7 +1,6 @@
|
||||
from flask import Flask, render_template, request, redirect, url_for, flash,send_file ,jsonify
|
||||
from flask import Flask, render_template, request, redirect, url_for, flash,send_file ,jsonify, session
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
from werkzeug.utils import secure_filename
|
||||
from datetime import date
|
||||
from AppCode.Config import DBConfig
|
||||
@@ -14,7 +13,12 @@ from AppCode.AOHandler import AOHandler
|
||||
from AppCode.CITHandler import CITHandler
|
||||
from AppCode.ITATHandler import ITATHandler
|
||||
from AppCode.MatCreditHandler import MatCreditHandler
|
||||
import logging
|
||||
import sys
|
||||
from AppCode.Log import LogHelper
|
||||
|
||||
# Loading env file
|
||||
load_dotenv()
|
||||
|
||||
# Server
|
||||
app = Flask(__name__)
|
||||
@@ -24,6 +28,13 @@ app.secret_key=os.getenv("SECRET_KEY")
|
||||
auth = LoginAuth()
|
||||
app.register_blueprint(auth.bp)
|
||||
|
||||
# LOGGING SETUP
|
||||
LogHelper.setup_logger(app)
|
||||
|
||||
@app.before_request
|
||||
def log_all_requests():
|
||||
LogHelper.log_request()
|
||||
|
||||
|
||||
# welcome page
|
||||
@app.route('/')
|
||||
@@ -46,6 +57,7 @@ def upload_file():
|
||||
FileHandler.CHeckExistingOrCreateNewUploadFolder()
|
||||
docHandler = DocumentHandler()
|
||||
docHandler.Upload(request=request)
|
||||
LogHelper.log_action("UPLOAD", "Document uploaded")
|
||||
return redirect(url_for('view_documents'))
|
||||
return render_template('upload.html')
|
||||
|
||||
@@ -68,7 +80,7 @@ def uploaded_file(filename):
|
||||
if not os.path.exists(filepath):
|
||||
flash("Unsupported file type for viewing", "warning")
|
||||
return redirect(url_for('view_documents'))
|
||||
|
||||
LogHelper.log_action("VIEW FILE", filename)
|
||||
file_ext = filename.rsplit('.', 1)[-1].lower()
|
||||
# --- View Mode ---
|
||||
if mode == 'view':
|
||||
@@ -124,7 +136,7 @@ def add_itr():
|
||||
mat_utilized=float(request.form.get("mat_credit_utilized", 0)),
|
||||
remarks="Created via ITR"
|
||||
)
|
||||
|
||||
LogHelper.log_action("ADD ITR Record", f"Year: {request.form['year']}")
|
||||
# flash("ITR record added successfully!", "success")
|
||||
flash("ITR record and documents uploaded successfully!", "success")
|
||||
return redirect(url_for('display_itr'))
|
||||
@@ -138,6 +150,7 @@ def delete_itr(id):
|
||||
itr = ITRHandler()
|
||||
itr.delete_itr_by_id(id=id)
|
||||
itr.close()
|
||||
LogHelper.log_action("ITR record deleted successfully!", id)
|
||||
return redirect(url_for('display_itr'))
|
||||
|
||||
## 3. UPDATE an existing ITR record
|
||||
@@ -150,8 +163,19 @@ def update_itr(id):
|
||||
data = {k: request.form.get(k, 0) for k in request.form}
|
||||
itr.update(id, data)
|
||||
itr.close()
|
||||
return redirect(url_for('display_itr'))
|
||||
mat = MatCreditHandler()
|
||||
|
||||
# AUTO SAVE MAT FROM ITR
|
||||
mat.save_from_itr(
|
||||
year=request.form["year"],
|
||||
mat_created=float(request.form.get("mat_credit_created", 0)),
|
||||
opening_balance=float(request.form.get("opening_balance", 0)),
|
||||
mat_utilized=float(request.form.get("mat_credit_utilized", 0)),
|
||||
remarks="Updated via ITR"
|
||||
)
|
||||
LogHelper.log_action("ITR record updated successfully!", data)
|
||||
return redirect(url_for('display_itr'))
|
||||
|
||||
record = itr.get_itr_by_id(id)
|
||||
itr.close()
|
||||
return render_template('update_itr.html', record=record, current_date=date.today().isoformat())
|
||||
@@ -193,7 +217,7 @@ def add_ao():
|
||||
mat_utilized=float(request.form.get("mat_credit_utilized", 0)),
|
||||
remarks="Created via ao"
|
||||
)
|
||||
|
||||
LogHelper.log_action("AO record added successfully!", "")
|
||||
flash("AO record added successfully!", "success")
|
||||
return redirect(url_for('display_ao'))
|
||||
return render_template('add_ao.html',current_date=date.today().isoformat())
|
||||
@@ -212,6 +236,15 @@ def update_ao(id):
|
||||
data = request.form.to_dict()
|
||||
ao.update_ao(id, data)
|
||||
ao.close()
|
||||
mat = MatCreditHandler()
|
||||
mat.save_from_itr(
|
||||
year=request.form["year"],
|
||||
mat_created=float(request.form.get("mat_credit_created", 0)),
|
||||
opening_balance=float(request.form.get("opening_balance", 0)),
|
||||
mat_utilized=float(request.form.get("mat_credit_utilized", 0)),
|
||||
remarks="Created via ao"
|
||||
)
|
||||
LogHelper.log_action("AO record updated successfully!", data)
|
||||
flash("AO record updated successfully!", "success")
|
||||
return redirect(url_for('display_ao'))
|
||||
|
||||
@@ -226,6 +259,7 @@ def delete_ao(id):
|
||||
ao = AOHandler()
|
||||
ao.delete_ao_by_id(id=id)
|
||||
ao.close()
|
||||
LogHelper.log_action("AO deleted successfully!", id)
|
||||
flash("AO deleted successfully!", "success")
|
||||
return redirect(url_for('display_ao'))
|
||||
|
||||
@@ -266,6 +300,7 @@ def add_cit():
|
||||
mat_utilized=float(request.form.get("mat_credit_utilized", 0)),
|
||||
remarks="Created via cit"
|
||||
)
|
||||
LogHelper.log_action("CIT record added successfully!", "")
|
||||
flash("CIT record added successfully!", "success")
|
||||
return redirect(url_for('display_cit'))
|
||||
|
||||
@@ -278,6 +313,7 @@ def delete_cit(id):
|
||||
cit = CITHandler()
|
||||
cit.delete_cit(id)
|
||||
cit.close()
|
||||
LogHelper.log_action("CIT record deleted successfully!", id)
|
||||
flash("CIT record deleted successfully!", "success")
|
||||
return redirect(url_for('display_cit'))
|
||||
|
||||
@@ -296,6 +332,16 @@ def update_cit(id):
|
||||
data = {k: request.form.get(k, 0) for k in request.form}
|
||||
cit.update_cit(id, data)
|
||||
cit.close()
|
||||
mat = MatCreditHandler()
|
||||
# AUTO SAVE MAT FROM ITR
|
||||
mat.save_from_itr(
|
||||
year=request.form["year"],
|
||||
mat_created=float(request.form.get("mat_credit_created", 0)),
|
||||
opening_balance=float(request.form.get("opening_balance", 0)),
|
||||
mat_utilized=float(request.form.get("mat_credit_utilized", 0)),
|
||||
remarks="Updated via cit"
|
||||
)
|
||||
LogHelper.log_action("CIT record updated successfully!", data)
|
||||
return redirect(url_for('display_cit'))
|
||||
|
||||
cit.close()
|
||||
@@ -336,9 +382,9 @@ def add_itat():
|
||||
mat_created=float(request.form.get("mat_credit_created", 0)),
|
||||
opening_balance=float(request.form.get("opening_balance", 0)),
|
||||
mat_utilized=float(request.form.get("mat_credit_utilized", 0)),
|
||||
remarks="Created via ITR"
|
||||
remarks="Created via ITAT"
|
||||
)
|
||||
|
||||
LogHelper.log_action("ITAT record added successfully!", data)
|
||||
flash("ITAT record added successfully!", "success")
|
||||
return redirect(url_for('display_itat'))
|
||||
|
||||
@@ -358,6 +404,16 @@ def update_itat(id):
|
||||
if request.method == 'POST':
|
||||
itat.update_itat(id, request.form)
|
||||
itat.close()
|
||||
mat = MatCreditHandler()
|
||||
mat.save_from_itr(
|
||||
year=request.form["year"],
|
||||
mat_created=float(request.form.get("mat_credit_created", 0)),
|
||||
opening_balance=float(request.form.get("opening_balance", 0)),
|
||||
mat_utilized=float(request.form.get("mat_credit_utilized", 0)),
|
||||
remarks="Updated via ITAT"
|
||||
)
|
||||
|
||||
LogHelper.log_action("ITAT Record Updated!", id)
|
||||
flash("ITAT Record Updated!", "success")
|
||||
return redirect(url_for('display_itat'))
|
||||
|
||||
@@ -370,6 +426,7 @@ def update_itat(id):
|
||||
def delete_itat(id):
|
||||
itat = ITATHandler()
|
||||
itat.delete_itat_by_id(id)
|
||||
LogHelper.log_action("itat record of by id:", id)
|
||||
itat.close()
|
||||
flash("ITAT Record Deleted!", "success")
|
||||
return redirect(url_for('display_itat'))
|
||||
@@ -398,7 +455,7 @@ def itr_report():
|
||||
|
||||
if output is None:
|
||||
return "No records found for the selected year."
|
||||
|
||||
LogHelper.log_action("itr report download", selected_year)
|
||||
return send_file(
|
||||
output,
|
||||
mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
@@ -425,7 +482,7 @@ def ao_report():
|
||||
|
||||
if output is None:
|
||||
return "No records found for the selected year."
|
||||
|
||||
LogHelper.log_action("ao report download", selected_year)
|
||||
return send_file(
|
||||
output,
|
||||
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
@@ -453,7 +510,7 @@ def cit_report():
|
||||
|
||||
if output is None:
|
||||
return "No records found for the selected year."
|
||||
|
||||
LogHelper.log_action("cit report download", selected_year)
|
||||
return send_file(
|
||||
output,
|
||||
mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
@@ -482,7 +539,7 @@ def itat_report():
|
||||
|
||||
if output is None:
|
||||
return "No records found for the selected year."
|
||||
|
||||
LogHelper.log_action("itat report download", selected_year)
|
||||
return send_file(
|
||||
output,
|
||||
mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
@@ -512,7 +569,7 @@ def download_summary():
|
||||
return "Year parameter is required", 400
|
||||
|
||||
docHandler = DocumentHandler()
|
||||
# reuse your existing Summary_report method
|
||||
LogHelper.log_action("/summary/download | download summary sheet !",year_raw)
|
||||
return docHandler.Summary_report(request=request)
|
||||
|
||||
|
||||
@@ -562,8 +619,9 @@ def mat_credit():
|
||||
for u in utilization_rows:
|
||||
all_years.add(u["utilized_year"])
|
||||
utilization_map.setdefault(
|
||||
u["mat_credit_id"], {}
|
||||
u["mat_credit_id"], {}
|
||||
)[u["utilized_year"]] = u["utilized_amount"]
|
||||
LogHelper.log_action("/mat_credit| Save mat credit !",all_years)
|
||||
|
||||
return render_template(
|
||||
"mat_credit.html",
|
||||
@@ -579,6 +637,7 @@ def save_mat_row():
|
||||
mat = MatCreditHandler()
|
||||
try:
|
||||
mat.save_single(request.json)
|
||||
LogHelper.log_action("/save_mat_row", "Save Mat row!")
|
||||
return jsonify({"message": "Row saved successfully"})
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
@@ -602,6 +661,30 @@ def summary_preview_route():
|
||||
# except Exception as e:
|
||||
# return jsonify({"error": str(e)}), 500
|
||||
|
||||
|
||||
@app.route("/view_logs", methods=["GET", "POST"])
|
||||
@auth.login_required
|
||||
def view_logs():
|
||||
secret = os.getenv("LOG_VIEW_SECRET")
|
||||
|
||||
if request.method == "POST":
|
||||
entered = request.form.get("secret")
|
||||
|
||||
if entered != secret:
|
||||
flash("Invalid secret!", "danger")
|
||||
return render_template("view_logs_auth.html")
|
||||
try:
|
||||
with open("logs/app.log", "r") as f:
|
||||
logs = f.readlines()
|
||||
except FileNotFoundError:
|
||||
logs = ["Log file not found"]
|
||||
|
||||
return render_template("view_logs.html", logs=logs)
|
||||
|
||||
return render_template("view_logs_auth.html")
|
||||
|
||||
|
||||
|
||||
# run server
|
||||
if __name__ == '__main__':
|
||||
app.run(
|
||||
|
||||
Reference in New Issue
Block a user