add optional debug state

This commit is contained in:
Thies Mueller
2025-09-24 20:23:25 +02:00
parent 6e4079485f
commit 305172950b
2 changed files with 19 additions and 14 deletions

View File

@@ -78,4 +78,6 @@ pretix:
event: eventslug event: eventslug
token: randomtokenforpretixapiaccess1234567890 token: randomtokenforpretixapiaccess1234567890
auth: auth:
token: randomtokenforauthaccess1234567890 token: randomtokenforauthaccess1234567890
debug: true

29
main.py
View File

@@ -21,11 +21,14 @@ MEALS = config["meals"]
TYPES = config["types"] TYPES = config["types"]
PRETIX = config["pretix"] PRETIX = config["pretix"]
AUTH_TOKEN = config.get("auth", {}).get("token") AUTH_TOKEN = config.get("auth", {}).get("token")
DEBUG = config.get("debug", False)
with open("template.txt", "r") as f: with open("template.txt", "r") as f:
MAIL_TEMPLATE = Template(f.read()) MAIL_TEMPLATE = Template(f.read())
def debug_print(msg):
if DEBUG:
print(msg)
def require_auth(f): def require_auth(f):
@wraps(f) @wraps(f)
@@ -37,7 +40,7 @@ def require_auth(f):
return decorated return decorated
def send_email(to_email, subject, body, attachment_bytes, filename): def send_email(to_email, subject, body, attachment_bytes, filename):
print(f"[DEBUG] Sending email to {to_email} with attachment {filename}") debug_print(f"[DEBUG] Sending email to {to_email} with attachment {filename}")
msg = EmailMessage() msg = EmailMessage()
msg["From"] = MAIL_CONFIG["sender"] msg["From"] = MAIL_CONFIG["sender"]
msg["To"] = to_email msg["To"] = to_email
@@ -51,7 +54,7 @@ def send_email(to_email, subject, body, attachment_bytes, filename):
server.login(MAIL_CONFIG["user"], MAIL_CONFIG["password"]) server.login(MAIL_CONFIG["user"], MAIL_CONFIG["password"])
server.send_message(msg) server.send_message(msg)
server.quit() server.quit()
print("[DEBUG] Email sent successfully") debug_print("[DEBUG] Email sent successfully")
def get_meal_times(meal_key): def get_meal_times(meal_key):
if meal_key.endswith("mittag"): if meal_key.endswith("mittag"):
@@ -68,7 +71,7 @@ def log_printed(email, meal_key):
with open("printed.csv", "a", newline="") as csvfile: with open("printed.csv", "a", newline="") as csvfile:
writer = csv.writer(csvfile, delimiter=";") writer = csv.writer(csvfile, delimiter=";")
writer.writerow([date_str, time_str, meal_key, location]) writer.writerow([date_str, time_str, meal_key, location])
print(f"[DEBUG] Logged printed PDF: {location} - {meal_key}") debug_print(f"[DEBUG] Logged printed PDF: {location} - {meal_key}")
def generate_qr(secret): def generate_qr(secret):
qr = qrcode.QRCode(box_size=10, border=4) qr = qrcode.QRCode(box_size=10, border=4)
@@ -84,7 +87,7 @@ def generate_qr(secret):
@require_auth @require_auth
def order(): def order():
data = request.get_json() data = request.get_json()
print(f"[DEBUG] Incoming request JSON: {data}") debug_print(f"[DEBUG] Incoming request JSON: {data}")
email = data.get("email") email = data.get("email")
typ = data.get("type") typ = data.get("type")
@@ -118,26 +121,26 @@ def order():
url = f"{PRETIX['host']}/api/v1/organizers/{PRETIX['organizer']}/events/{PRETIX['event']}/orders/" url = f"{PRETIX['host']}/api/v1/organizers/{PRETIX['organizer']}/events/{PRETIX['event']}/orders/"
headers = {"Authorization": f"Token {PRETIX['token']}"} headers = {"Authorization": f"Token {PRETIX['token']}"}
print(f"[DEBUG] Sending POST to Pretix: {url} with body {pretix_body}") debug_print(f"[DEBUG] Sending POST to Pretix: {url} with body {pretix_body}")
resp = requests.post(url, json=pretix_body, headers=headers) resp = requests.post(url, json=pretix_body, headers=headers)
print(f"[DEBUG] Pretix response status: {resp.status_code}") debug_print(f"[DEBUG] Pretix response status: {resp.status_code}")
print(f"[DEBUG] Pretix response text: {resp.text}") debug_print(f"[DEBUG] Pretix response text: {resp.text}")
try: try:
resp_json = resp.json() resp_json = resp.json()
print(f"[DEBUG] Pretix response JSON: {resp_json}") debug_print(f"[DEBUG] Pretix response JSON: {resp_json}")
except ValueError: except ValueError:
print("[DEBUG] Pretix returned no JSON") debug_print("[DEBUG] Pretix returned no JSON")
return jsonify({"error": "Pretix returned no JSON", "status_code": resp.status_code, "text": resp.text}), 502 return jsonify({"error": "Pretix returned no JSON", "status_code": resp.status_code, "text": resp.text}), 502
if "positions" in resp_json and "item" in resp_json["positions"][0] and isinstance(resp_json["positions"][0]["item"], list): if "positions" in resp_json and "item" in resp_json["positions"][0] and isinstance(resp_json["positions"][0]["item"], list):
if isinstance(resp_json["positions"][0]["item"][0], str): if isinstance(resp_json["positions"][0]["item"][0], str):
print("[DEBUG] Not enough quota available") debug_print("[DEBUG] Not enough quota available")
return "Essen ist bereits alle", 418 return "Essen ist bereits alle", 418
try: try:
secret = resp_json["positions"][0]["secret"] secret = resp_json["positions"][0]["secret"]
print(f"[DEBUG] Secret: {secret}") debug_print(f"[DEBUG] Secret: {secret}")
except (KeyError, IndexError): except (KeyError, IndexError):
return jsonify({"error": "Secret not found in Pretix response", "resp_json": resp_json}), 500 return jsonify({"error": "Secret not found in Pretix response", "resp_json": resp_json}), 500
@@ -160,4 +163,4 @@ def order():
return "Token gesendet", 201 return "Token gesendet", 201
if __name__ == "__main__": if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, debug=True) app.run(host="0.0.0.0", port=8000, debug=DEBUG)