diff --git a/schadenprotokoll/api/app.py b/schadenprotokoll/api/app.py index b1bbf84..54ef662 100644 --- a/schadenprotokoll/api/app.py +++ b/schadenprotokoll/api/app.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 """ Schadenprotokoll API - Flask Backend -Endpunkte: /generate, /analyze, /vorbericht +Endpunkte: /generate, /analyze, /vorbericht, /templates """ from flask import Flask, request, jsonify, send_file from flask_cors import CORS @@ -12,8 +12,9 @@ from processors import parse_laufzettel, fill_pdf, analyze_pdf, generate_vorberi app = Flask(__name__) CORS(app) -TEMPLATE_PDF = "/opt/stacks/spa-hosting/html/schadenprotokoll/templates/protokoll.pdf" -TEMPLATE_DOCX = "/opt/stacks/spa-hosting/html/schadenprotokoll/templates/vorbericht.docx" +TEMPLATES_DIR = "/opt/stacks/spa-hosting/html/schadenprotokoll/templates" +TEMPLATE_PDF = os.path.join(TEMPLATES_DIR, "protokoll.pdf") +TEMPLATE_DOCX = os.path.join(TEMPLATES_DIR, "vorbericht.docx") @app.route("/health", methods=["GET"]) @@ -21,6 +22,47 @@ def health(): return jsonify({"status": "ok"}) +@app.route("/templates", methods=["GET"]) +def list_templates(): + """Listet alle verfuegbaren Templates""" + templates = [] + for f in os.listdir(TEMPLATES_DIR): + path = os.path.join(TEMPLATES_DIR, f) + if os.path.isfile(path) and not f.endswith(".bak"): + templates.append({ + "name": f, + "size": os.path.getsize(path), + "type": "pdf" if f.endswith(".pdf") else "docx" + }) + return jsonify({"templates": templates}) + + +@app.route("/templates/upload", methods=["POST"]) +def upload_template(): + """Laedt ein neues Template hoch""" + if "file" not in request.files: + return jsonify({"error": "Keine Datei hochgeladen"}), 400 + + file = request.files["file"] + ttype = request.form.get("type", "pdf") + + if ttype == "pdf" and not file.filename.endswith(".pdf"): + return jsonify({"error": "PDF-Template muss .pdf sein"}), 400 + if ttype == "docx" and not file.filename.endswith(".docx"): + return jsonify({"error": "Word-Template muss .docx sein"}), 400 + + target = TEMPLATE_PDF if ttype == "pdf" else TEMPLATE_DOCX + backup = target + ".bak" + + if os.path.exists(target): + if os.path.exists(backup): + os.remove(backup) + os.rename(target, backup) + + file.save(target) + return jsonify({"success": True, "message": f"Template erfolgreich hochgeladen"}) + + @app.route("/generate", methods=["POST"]) def generate(): """Laufzettel.docx -> vorausgefuelltes Protokoll.pdf""" @@ -33,20 +75,13 @@ def generate(): with tempfile.NamedTemporaryFile(suffix=".docx", delete=False) as tmp_in: file.save(tmp_in.name) - try: data = parse_laufzettel(tmp_in.name) - with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as tmp_out: fill_pdf(TEMPLATE_PDF, tmp_out.name, data) - os.unlink(tmp_in.name) - return send_file( - tmp_out.name, - mimetype="application/pdf", - as_attachment=True, - download_name="Schadenprotokoll_vorbefuellt.pdf" - ) + return send_file(tmp_out.name, mimetype="application/pdf", + as_attachment=True, download_name="Schadenprotokoll_vorbefuellt.pdf") except Exception as e: os.unlink(tmp_in.name) return jsonify({"error": str(e)}), 500 @@ -64,7 +99,6 @@ def analyze(): with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as tmp: file.save(tmp.name) - try: result = analyze_pdf(tmp.name) os.unlink(tmp.name) @@ -86,20 +120,14 @@ def vorbericht(): with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as tmp_in: file.save(tmp_in.name) - try: pdf_data = analyze_pdf(tmp_in.name) - with tempfile.NamedTemporaryFile(suffix=".docx", delete=False) as tmp_out: generate_vorbericht(pdf_data, TEMPLATE_DOCX, tmp_out.name) - os.unlink(tmp_in.name) - return send_file( - tmp_out.name, + return send_file(tmp_out.name, mimetype="application/vnd.openxmlformats-officedocument.wordprocessingml.document", - as_attachment=True, - download_name="Vorbericht.docx" - ) + as_attachment=True, download_name="Vorbericht.docx") except Exception as e: os.unlink(tmp_in.name) return jsonify({"error": str(e)}), 500 diff --git a/schadenprotokoll/app.js b/schadenprotokoll/app.js index 6a99b30..f38ebfd 100644 --- a/schadenprotokoll/app.js +++ b/schadenprotokoll/app.js @@ -172,3 +172,79 @@ document.getElementById("vorbericht-btn").addEventListener("click", async () => showStatus("vorbericht-status", "error", `❌ Fehler: ${err.message}`); } }); + +// Admin Tab - Templates laden +async function loadTemplates() { + try { + const response = await fetch(`${API_BASE}/templates`); + const data = await response.json(); + const container = document.getElementById("templates-container"); + + if (data.templates.length === 0) { + container.innerHTML = "
Keine Templates vorhanden.
"; + return; + } + + container.innerHTML = data.templates.map(t => ` +Fehler: ${err.message}
`; + } +} + +// Template Upload +function setupTemplateUpload(zoneId, inputId, templateType) { + const zone = document.getElementById(zoneId); + const input = document.getElementById(inputId); + + zone.addEventListener("click", () => input.click()); + zone.addEventListener("dragover", (e) => { e.preventDefault(); zone.classList.add("dragover"); }); + zone.addEventListener("dragleave", () => zone.classList.remove("dragover")); + zone.addEventListener("drop", (e) => { + e.preventDefault(); + zone.classList.remove("dragover"); + if (e.dataTransfer.files.length) { + input.files = e.dataTransfer.files; + uploadTemplate(input.files[0], templateType); + } + }); + input.addEventListener("change", () => { + if (input.files.length) uploadTemplate(input.files[0], templateType); + }); +} + +async function uploadTemplate(file, type) { + showStatus("admin-status", "loading", `⏳ Lade ${type.toUpperCase()} Template hoch...`); + + const formData = new FormData(); + formData.append("file", file); + formData.append("type", type); + + try { + const response = await fetch(`${API_BASE}/templates/upload`, { + method: "POST", + body: formData + }); + + if (!response.ok) { + const err = await response.json(); + throw new Error(err.error); + } + + showStatus("admin-status", "success", "✓ Template erfolgreich hochgeladen"); + loadTemplates(); + } catch (err) { + showStatus("admin-status", "error", `❌ Fehler: ${err.message}`); + } +} + +setupTemplateUpload("pdf-template-zone", "pdf-template-input", "pdf"); +setupTemplateUpload("docx-template-zone", "docx-template-input", "docx"); + +// Templates beim Tab-Wechsel laden +document.querySelector([data-tab=admin]).addEventListener("click", loadTemplates); diff --git a/schadenprotokoll/index.html b/schadenprotokoll/index.html index 45e98a8..e6df99d 100644 --- a/schadenprotokoll/index.html +++ b/schadenprotokoll/index.html @@ -17,26 +17,23 @@ +Lädt Daten aus dem Schadenlaufzettel.docx und füllt das Schadenprotokoll-PDF vor.
+Laedt Daten aus dem Schadenlaufzettel.docx und fuellt das Schadenprotokoll-PDF vor.
Datei hierher ziehen oder klicken zum Auswählen
+Datei hierher ziehen oder klicken
Liest alle Formularfelder und Dropdown-Auswahlen aus dem Schadenprotokoll.
Das ausgefüllte PDF-Formular
+Das ausgefuellte PDF-Formular
Generiert einen strukturierten Vorbericht aus dem ausgefüllten Schadenprotokoll.
+Generiert einen strukturierten Vorbericht aus dem ausgefuellten Schadenprotokoll.
Das vollständig ausgefüllte PDF-Formular
+Das vollstaendig ausgefuellte PDF-Formular
Hier kannst du die PDF- und Word-Templates hochladen, die fuer die Generierung verwendet werden.
+Schadenprotokoll-Vorlage
+Vorbericht-Vorlage
+