From 4d64d567be955263ba8e772b99fb447a23cc39f3 Mon Sep 17 00:00:00 2001 From: Kim Diallo Date: Sat, 3 Jan 2026 01:08:00 +0100 Subject: [PATCH] src/dlw/admin.py aktualisiert --- src/dlw/admin.py | 134 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 119 insertions(+), 15 deletions(-) diff --git a/src/dlw/admin.py b/src/dlw/admin.py index 6bb6c8e..2d7ac80 100644 --- a/src/dlw/admin.py +++ b/src/dlw/admin.py @@ -1,24 +1,128 @@ -from flask import Flask, request, flash -from .marvin_ops import MarvinClient +from flask import Flask, render_template_string, request, redirect, url_for, flash +from werkzeug.utils import secure_filename import os +import time +from . import git_ops +from .marvin_ops import MarvinClient # Für Uberspace 8 Marvin API app = Flask(__name__) -# ... (Konfiguration wie zuvor) ... +# Konfiguration: Nutzt das 'downloads' Verzeichnis im aktuellen Arbeitsverzeichnis +app.config['UPLOAD_FOLDER'] = os.path.join(os.getcwd(), 'downloads') +app.secret_key = 'sehr_geheimer_schluessel' # Erforderlich für Session/Flash-Messages + +# --- Hilfsfunktionen für die Content-Erzeugung --- + +def generate_markdown_content(data, article_slug): + """Erzeugt den Markdown-Inhalt inklusive YAML Frontmatter für Pelican.""" + + # Pfade für die Links im statischen Blog + pdf_path = f"/downloads/{article_slug}.pdf" + audio_path = f"/downloads/{article_slug}.mp3" + + markdown_template = f"""--- +Title: {data['title']} +Date: {time.strftime("%Y-%m-%d %H:%M")} +Slug: {article_slug} +Description: {data['description']} +Abstract: {data['abstract']} +PDF_Download: {pdf_path} +Audio_Download: {audio_path} +--- + +{data['article_body']} +""" + return markdown_template.strip() + +# --- Flask Routen --- + +@app.route('/', methods=['GET']) +def new_article_form(): + """Zeigt das Eingabeformular für die Autorin.""" + form_html = """ +

Neuen Artikel erstellen

+
+
+

+ +
+

+ +
+

+ +
+

+ + +
+ + +

+ + +
+ """ + return form_html # + +@app.route('/preview', methods=['POST']) +def preview_article(): + """Erzeugt eine Vorschau des Artikels.""" + data = request.form + + # Slug für Dateinamen generieren + slug = secure_filename(data['title']).lower().replace('-', '_') + markdown_content = generate_markdown_content(data, slug) + + # Simulation der Vorschau + preview_html = f""" +

Vorschau: {data['title']}

+
+
+ Abstract:

{data['abstract']}

+
+

[Hier würde der gerenderte Artikeltext stehen]

+
+
+
+ + + +

Möchten Sie diesen Artikel jetzt veröffentlichen? (Dies löst einen Git-Push aus)

+ + Abbrechen und zurück +
+ """ + return preview_html # @app.route('/publish', methods=['POST']) def publish_article(): - # ... (Markdown Generierung wie zuvor) ... - - # Datei-Upload Handling + """Speichert Dateien und führt den Git-Push aus.""" + markdown_content = request.form.get('content_data') + article_title = request.form.get('article_title') + + if not markdown_content: + return "Fehler: Keine Inhaltsdaten vorhanden.", 400 + + # Dateinamen und Pfad festlegen + article_slug = secure_filename(article_title).lower().replace('-', '_') + filename = f"{article_slug}.md" + article_path = os.path.join(os.getcwd(), 'content', filename) + download_paths = [] + + # 1. Speichern des Markdown-Artikels + try: + with open(article_path, 'w', encoding='utf-8') as f: + f.write(markdown_content) + except IOError as e: + return f"Fehler beim Speichern des Artikels: {e}", 500 + + # 2. Speichern der hochgeladenen Dateien (PDF/MP3) + # Hinweis: In der Praxis müssen Dateien ggf. zwischen Preview und Publish gepuffert werden. for key in ['pdf_file', 'audio_file']: file = request.files.get(key) - if file and file.filename: - path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename) - file.save(path) - download_paths.append(path) - - # Git Push - from .import git_ops - success, msg = git_ops.commit_and_push_article(article_path, download_paths, "Titel") - return f"Status: {msg}" \ No newline at end of file + if file and file.filename != '': + ext = ".pdf" if key == "pdf_file" else ".mp3" + target_name = secure_filename(f"{article_slug}{ext}") + save_path = os.path.join(app.config['UPLOAD_FOLDER'], target_name) + file \ No newline at end of file