-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
97 lines (79 loc) · 3.35 KB
/
Copy pathapp.py
File metadata and controls
97 lines (79 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
import traceback
import tempfile
import shutil
from datetime import datetime
from flask import Flask, render_template, request, send_file, jsonify
from compare import run_comparison, build_excel, _load_item_prices
app = Flask(__name__)
app.config["MAX_CONTENT_LENGTH"] = 100 * 1024 * 1024 # 100 MB
# ── Use a temp dir OUTSIDE the project so Werkzeug reloader doesn't
# detect uploaded files as source changes and restart mid-upload ──────────
_UPLOAD_TEMP = os.path.join(tempfile.gettempdir(), "pending_order_uploads")
os.makedirs(_UPLOAD_TEMP, exist_ok=True)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/compare", methods=["POST"])
def compare():
ov_file = request.files.get("ov_file")
fa_file = request.files.get("fa_file")
ageing_file = request.files.get("ageing_file")
prices_file = request.files.get("prices_file")
inventory_file = request.files.get("inventory_file")
if not ov_file or not fa_file or not ageing_file or not prices_file:
return jsonify({"error": "OV, FA, Ageing and Item Prices files are all required."}), 400
# Use a unique subdirectory per request so concurrent uploads don't collide
tmp_dir = tempfile.mkdtemp(dir=_UPLOAD_TEMP)
try:
ov_path = os.path.join(tmp_dir, "ov.xlsx")
fa_path = os.path.join(tmp_dir, "fa.xlsx")
ageing_path = os.path.join(tmp_dir, "ageing.xlsx")
prices_path = os.path.join(tmp_dir, "item_prices.xlsx")
inventory_path = os.path.join(tmp_dir, "inventory.xlsx") if inventory_file else None
ov_file.save(ov_path)
fa_file.save(fa_path)
ageing_file.save(ageing_path)
prices_file.save(prices_path)
if inventory_file:
inventory_file.save(inventory_path)
findings_df, ov_df, fa_df, \
ref_customers_df, ref_overdue_df, ref_inventory_df = run_comparison(
ov_path, fa_path,
customers_path=None,
ageing_path=ageing_path,
inventory_path=inventory_path,
)
price_map = _load_item_prices(prices_path)
buf = build_excel(
findings_df, ov_df, fa_df, price_map,
ref_customers_df=ref_customers_df,
ref_overdue_df=ref_overdue_df,
ref_inventory_df=ref_inventory_df,
)
except Exception:
tb = traceback.format_exc()
print(tb)
return jsonify({"error": tb}), 500
finally:
shutil.rmtree(tmp_dir, ignore_errors=True)
now = datetime.now()
filename = f"{now.strftime('%d.%m.%Y')} - Pending Order Report.xlsx"
return send_file(
buf,
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
as_attachment=True,
download_name=filename,
)
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(
host="0.0.0.0",
port=port,
debug=True,
use_reloader=False, # ← KEY FIX: prevents Werkzeug from restarting
# the server when uploaded files are saved to
# disk, which was killing the connection and
# causing ERR_FAILED with no Flask logs.
threaded=True, # handle concurrent requests properly
)