src/dlw/admin.py aktualisiert
This commit is contained in:
parent
06fba7baad
commit
4d64d567be
134
src/dlw/admin.py
134
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 = """
|
||||
<h1>Neuen Artikel erstellen</h1>
|
||||
<form method="POST" action="/preview" enctype="multipart/form-data">
|
||||
<label>Titel:</label><br>
|
||||
<input type="text" name="title" style="width:100%" required><br><br>
|
||||
|
||||
<label>Beschreibung (Short Desc.):</label><br>
|
||||
<input type="text" name="description" style="width:100%"><br><br>
|
||||
|
||||
<label>Abstract:</label><br>
|
||||
<textarea name="abstract" style="width:100%; height:100px"></textarea><br><br>
|
||||
|
||||
<label>Artikeltext (Markdown):</label><br>
|
||||
<textarea name="article_body" style="width:100%; height:300px" required></textarea><br><br>
|
||||
|
||||
<label>PDF Download:</label>
|
||||
<input type="file" name="pdf_file" accept=".pdf"><br>
|
||||
|
||||
<label>Audio Download (MP3):</label>
|
||||
<input type="file" name="audio_file" accept=".mp3"><br><br>
|
||||
|
||||
<button type="submit" name="action" value="preview">Vorschau generieren</button>
|
||||
</form>
|
||||
"""
|
||||
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"""
|
||||
<h2>Vorschau: {data['title']}</h2>
|
||||
<hr>
|
||||
<div style="background: #f9f9f9; padding: 20px; border: 1px solid #ccc;">
|
||||
<strong>Abstract:</strong><p>{data['abstract']}</p>
|
||||
<hr>
|
||||
<p><i>[Hier würde der gerenderte Artikeltext stehen]</i></p>
|
||||
</div>
|
||||
<hr>
|
||||
<form method="POST" action="/publish" enctype="multipart/form-data">
|
||||
<input type="hidden" name="content_data" value='{markdown_content}'>
|
||||
<input type="hidden" name="article_title" value="{data['title']}">
|
||||
|
||||
<p>Möchten Sie diesen Artikel jetzt veröffentlichen? (Dies löst einen Git-Push aus)</p>
|
||||
<button type="submit">Artikel Bestätigen und Veröffentlichen</button>
|
||||
<a href="/">Abbrechen und zurück</a>
|
||||
</form>
|
||||
"""
|
||||
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}"
|
||||
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
|
||||
Loading…
Reference in New Issue