-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
106 lines (81 loc) · 3.47 KB
/
app.py
File metadata and controls
106 lines (81 loc) · 3.47 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
98
99
100
101
102
103
104
105
106
"""Module to handle the incoming http requests and generate reports."""
from datetime import datetime
import os
import re
from flask import Flask, request, send_file, jsonify, redirect
from report import get_product_customisations
app = Flask("css-reports")
@app.route("/")
def hello():
"""Redirect to the main website if no specific route is requested."""
return redirect("https://cssbham.com", code=302)
@app.errorhandler(404)
def page_not_found(exception: Exception | int):
"""Handle 404 errors by redirecting to the main website."""
print(exception)
return redirect("https://cssbham.com", code=302)
@app.route("/customisation_report", methods=["GET"])
async def fetch_customisation_report():
"""Fetch the report based on query parameters."""
# Retrieve query parameters
auth_cookie: str | None = request.args.get("auth_cookie")
organisation_id: str | None = request.args.get("organisation_id")
report_type: str | None = request.args.get("report_type")
product_name: str | None = request.args.get("product_name")
product_names: str | None = request.args.get("product_names")
start_date: str | None = request.args.get("start_date")
end_date: str | None = request.args.get("end_date")
print(f"Organisation ID: {organisation_id}")
print(f"Product Name: {product_name}")
if not auth_cookie or not organisation_id:
return jsonify(
{"error": "An auth token and organisation id are required."}
), 400
if (not product_name) and (not product_names):
return jsonify(
{"error": "Either product_name or product_names is required."}
), 400
if product_name and product_names:
return jsonify(
{"error": "Both product_name and product_names cannot be provided."}
), 400
if not report_type:
report_type = "Customisations"
if report_type not in ["Customisations", "Purchasers", "Sales"]:
return jsonify({"error": "Invalid report type specified."}), 400
start_date_dt: datetime
end_date_dt: datetime
try:
start_date_dt = datetime.strptime(start_date or "2000-01-01", "%Y-%m-%d")
end_date_dt = datetime.strptime(end_date or "2100-01-01", "%Y-%m-%d")
except ValueError:
return jsonify({"error": "Invalid date format. Use YYYY-MM-DD."}), 400
name_or_id: str = product_name or product_names # type: ignore[assignment]
name_or_id = re.sub(r"\W\s", "", name_or_id)
csv_file_path: str | None = None
try:
# Generate the CSV file
csv_file_path = await get_product_customisations(
product_id_or_name=name_or_id,
auth_cookie=auth_cookie,
org_id=organisation_id,
from_date_input=start_date_dt,
to_date_input=end_date_dt,
report_type=report_type,
)
if not csv_file_path:
print("Failed to generate the customisation report")
return jsonify(
{"error": "Failed to generate the customisation report"}
), 500
# Return the file as a response
return send_file(csv_file_path, as_attachment=True)
except Exception as unknown_error:
return jsonify({"error": str(unknown_error)}), 500
finally:
# Clean up the generated file
if csv_file_path and os.path.exists(csv_file_path):
os.remove(csv_file_path)
if __name__ == "__main__":
# from waitress import serve
app.run(host="0.0.0.0", port=8000, debug=True)