src/dlw/admin.py aktualisiert
This commit is contained in:
parent
7726ca595f
commit
06fba7baad
153
src/dlw/admin.py
153
src/dlw/admin.py
|
|
@ -1,129 +1,24 @@
|
|||
from flask import Flask, render_template_string, request, redirect, url_for
|
||||
from werkzeug.utils import secure_filename
|
||||
import os
|
||||
import time
|
||||
from . import git_ops
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['UPLOAD_FOLDER'] = os.path.join(os.getcwd(), 'downloads')
|
||||
app.secret_key = 'sehr_geheimer_schluessel' # Muss für Flash-Messages gesetzt werden
|
||||
|
||||
# --- Hilfsfunktionen für die Content-Erzeugung ---
|
||||
|
||||
def generate_markdown_content(data, article_slug):
|
||||
"""Erzeugt den Markdown-Inhalt inklusive YAML Frontmatter."""
|
||||
|
||||
# Pfade, wie sie im fertigen SSG-Blog verwendet werden
|
||||
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():
|
||||
# Einfache HTML-Formular-Definition (in einem echten Template besser)
|
||||
form_html = """
|
||||
<h1>Neuen Artikel erstellen</h1>
|
||||
<form method="POST" action="/preview" enctype="multipart/form-data">
|
||||
<label>Titel:</label><input type="text" name="title" required><br>
|
||||
<label>Beschreibung (Short Desc.):</label><input type="text" name="description"><br>
|
||||
<label>Abstract:</label><textarea name="abstract"></textarea><br>
|
||||
<label>Artikeltext (Markdown):</label><textarea name="article_body" required></textarea><br>
|
||||
|
||||
<label>PDF Download:</label><input type="file" name="pdf_file"><br>
|
||||
<label>Audio Download (MP3):</label><input type="file" name="audio_file"><br>
|
||||
|
||||
<button type="submit" name="action" value="preview">Vorschau</button>
|
||||
</form>
|
||||
"""
|
||||
return form_html
|
||||
|
||||
@app.route('/preview', methods=['POST'])
|
||||
def preview_article():
|
||||
data = request.form
|
||||
|
||||
# Slug vereinfachen
|
||||
slug = secure_filename(data['title']).lower().replace('-', '_')
|
||||
|
||||
markdown_content = generate_markdown_content(data, slug)
|
||||
|
||||
# --- Vorschau-Rendering ---
|
||||
# In einer echten Implementierung müsste hier Pelican's Render-Engine
|
||||
# oder zumindest Pelican's Jinja2-Kontext genutzt werden,
|
||||
# um die Vorschau exakt wie das Endergebnis darzustellen.
|
||||
|
||||
# Für diese Demo simulieren wir die Vorschau nur
|
||||
preview_html = f"""
|
||||
<h2>Vorschau: {data['title']}</h2>
|
||||
<hr>
|
||||
<h3>Abstract</h3>
|
||||
<p>{data['abstract']}</p>
|
||||
|
||||
<p>... Gerenderter Artikeltext hier (simuliert) ...</p>
|
||||
|
||||
<form method="POST" action="/publish">
|
||||
<!-- Alle Daten müssen versteckt an die Publish-Route übergeben werden (z.B. als JSON in einem hidden field) -->
|
||||
<input type="hidden" name="content_data" value='{markdown_content}'>
|
||||
<button type="submit" name="action" value="publish">Artikel Bestätigen und Veröffentlichen (Push zu Git)</button>
|
||||
<a href="/">Zurück zur Eingabe</a>
|
||||
</form>
|
||||
"""
|
||||
return preview_html
|
||||
|
||||
@app.route('/publish', methods=['POST'])
|
||||
def publish_article():
|
||||
# ACHTUNG: Daten aus der Vorschau sind möglicherweise im POST-Body
|
||||
markdown_content = request.form.get('content_data')
|
||||
|
||||
if not markdown_content:
|
||||
return "Fehler: Keine Inhaltsdaten zur Veröffentlichung.", 400
|
||||
|
||||
# Daten extrahieren (simplifiziert)
|
||||
lines = markdown_content.split('\n')
|
||||
title_line = [l for l in lines if l.startswith('Title:')][0]
|
||||
article_title = title_line.split(':', 1)[1].strip()
|
||||
|
||||
# Slug aus dem Titel ableiten
|
||||
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 der Datei: {e}", 500
|
||||
|
||||
# 2. Speichern der Uploads (Muss hier noch integriert werden, da Uploads in /preview gehandhabt wurden.
|
||||
# In einem Produktivsystem müsste Flask die Uploads in einer Session zwischenspeichern.)
|
||||
|
||||
# WICHTIG: Die Logic zum Verschieben der tatsächlichen PDF/MP3 Dateien
|
||||
# muss hier implementiert werden, um sie in das 'downloads' Verzeichnis
|
||||
# zu verschieben, bevor der Git-Commit erfolgt.
|
||||
|
||||
# 3. Git-Operation: Commit und Push
|
||||
success, message = git_ops.commit_and_push_article(article_path, download_paths, article_title)
|
||||
|
||||
if success:
|
||||
return f"<h1>Erfolg!</h1><p>{message}</p><p>Forgejo Action läuft jetzt.</p>"
|
||||
else:
|
||||
return f"<h1>FEHLER</h1><p>Veröffentlichung fehlgeschlagen: {message}</p>", 500
|
||||
|
||||
from flask import Flask, request, flash
|
||||
from .marvin_ops import MarvinClient
|
||||
import os
|
||||
|
||||
app = Flask(__name__)
|
||||
# ... (Konfiguration wie zuvor) ...
|
||||
|
||||
@app.route('/publish', methods=['POST'])
|
||||
def publish_article():
|
||||
# ... (Markdown Generierung wie zuvor) ...
|
||||
|
||||
# Datei-Upload Handling
|
||||
download_paths = []
|
||||
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}"
|
||||
Loading…
Reference in New Issue