diff --git a/config.yaml.example b/config.yaml.example index d457fe5..aacf6c7 100644 --- a/config.yaml.example +++ b/config.yaml.example @@ -78,4 +78,6 @@ pretix: event: eventslug token: randomtokenforpretixapiaccess1234567890 auth: - token: randomtokenforauthaccess1234567890 \ No newline at end of file + token: randomtokenforauthaccess1234567890 + +debug: true \ No newline at end of file diff --git a/main.py b/main.py index a539778..4bdeb78 100644 --- a/main.py +++ b/main.py @@ -21,11 +21,14 @@ MEALS = config["meals"] TYPES = config["types"] PRETIX = config["pretix"] AUTH_TOKEN = config.get("auth", {}).get("token") - +DEBUG = config.get("debug", False) with open("template.txt", "r") as f: MAIL_TEMPLATE = Template(f.read()) +def debug_print(msg): + if DEBUG: + print(msg) def require_auth(f): @wraps(f) @@ -37,7 +40,7 @@ def require_auth(f): return decorated 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["From"] = MAIL_CONFIG["sender"] 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.send_message(msg) server.quit() - print("[DEBUG] Email sent successfully") + debug_print("[DEBUG] Email sent successfully") def get_meal_times(meal_key): if meal_key.endswith("mittag"): @@ -68,7 +71,7 @@ def log_printed(email, meal_key): with open("printed.csv", "a", newline="") as csvfile: writer = csv.writer(csvfile, delimiter=";") 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): qr = qrcode.QRCode(box_size=10, border=4) @@ -84,7 +87,7 @@ def generate_qr(secret): @require_auth def order(): data = request.get_json() - print(f"[DEBUG] Incoming request JSON: {data}") + debug_print(f"[DEBUG] Incoming request JSON: {data}") email = data.get("email") typ = data.get("type") @@ -118,26 +121,26 @@ def order(): url = f"{PRETIX['host']}/api/v1/organizers/{PRETIX['organizer']}/events/{PRETIX['event']}/orders/" 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) - print(f"[DEBUG] Pretix response status: {resp.status_code}") - print(f"[DEBUG] Pretix response text: {resp.text}") + debug_print(f"[DEBUG] Pretix response status: {resp.status_code}") + debug_print(f"[DEBUG] Pretix response text: {resp.text}") try: resp_json = resp.json() - print(f"[DEBUG] Pretix response JSON: {resp_json}") + debug_print(f"[DEBUG] Pretix response JSON: {resp_json}") 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 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): - print("[DEBUG] Not enough quota available") + debug_print("[DEBUG] Not enough quota available") return "Essen ist bereits alle", 418 try: secret = resp_json["positions"][0]["secret"] - print(f"[DEBUG] Secret: {secret}") + debug_print(f"[DEBUG] Secret: {secret}") except (KeyError, IndexError): return jsonify({"error": "Secret not found in Pretix response", "resp_json": resp_json}), 500 @@ -160,4 +163,4 @@ def order(): return "Token gesendet", 201 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)