src/dlw/admin.py aktualisiert
This commit is contained in:
parent
4d64d567be
commit
b5aa41a16b
|
|
@ -5,17 +5,27 @@ import time
|
||||||
from . import git_ops
|
from . import git_ops
|
||||||
from .marvin_ops import MarvinClient # Für Uberspace 8 Marvin API
|
from .marvin_ops import MarvinClient # Für Uberspace 8 Marvin API
|
||||||
|
|
||||||
|
# --- Konfiguration aus Umgebungsvariablen ---
|
||||||
|
# Diese Variablen werden auf dem Uberspace (z.B. via .bash_profile oder Service-Config) gesetzt.
|
||||||
|
UBERSPACE_ASTEROID = os.getenv('UBERSPACE_ASTEROID')
|
||||||
|
UBERSPACE_DOMAIN = os.getenv('UBERSPACE_DOMAIN')
|
||||||
|
MARVIN_API_KEY = os.getenv('MARVIN_API_KEY')
|
||||||
|
FLASK_SECRET_KEY = os.getenv('FLASK_SECRET_KEY', 'fallback-fuer-lokale-entwicklung')
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
# Konfiguration: Nutzt das 'downloads' Verzeichnis im aktuellen Arbeitsverzeichnis
|
# Konfiguration: Nutzt das 'downloads' Verzeichnis im aktuellen Arbeitsverzeichnis
|
||||||
app.config['UPLOAD_FOLDER'] = os.path.join(os.getcwd(), 'downloads')
|
app.config['UPLOAD_FOLDER'] = os.path.join(os.getcwd(), 'downloads')
|
||||||
app.secret_key = 'sehr_geheimer_schluessel' # Erforderlich für Session/Flash-Messages
|
app.secret_key = FLASK_SECRET_KEY
|
||||||
|
|
||||||
|
# Sicherstellen, dass der Upload-Ordner existiert
|
||||||
|
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
||||||
|
|
||||||
# --- Hilfsfunktionen für die Content-Erzeugung ---
|
# --- Hilfsfunktionen für die Content-Erzeugung ---
|
||||||
|
|
||||||
def generate_markdown_content(data, article_slug):
|
def generate_markdown_content(data, article_slug):
|
||||||
"""Erzeugt den Markdown-Inhalt inklusive YAML Frontmatter für Pelican."""
|
"""Erzeugt den Markdown-Inhalt inklusive YAML Frontmatter für Pelican."""
|
||||||
|
|
||||||
# Pfade für die Links im statischen Blog
|
# Pfade für die Links im statischen Blog (relativ zur Domain)
|
||||||
pdf_path = f"/downloads/{article_slug}.pdf"
|
pdf_path = f"/downloads/{article_slug}.pdf"
|
||||||
audio_path = f"/downloads/{article_slug}.mp3"
|
audio_path = f"/downloads/{article_slug}.mp3"
|
||||||
|
|
||||||
|
|
@ -40,6 +50,7 @@ def new_article_form():
|
||||||
"""Zeigt das Eingabeformular für die Autorin."""
|
"""Zeigt das Eingabeformular für die Autorin."""
|
||||||
form_html = """
|
form_html = """
|
||||||
<h1>Neuen Artikel erstellen</h1>
|
<h1>Neuen Artikel erstellen</h1>
|
||||||
|
<p>Angemeldet auf Asteroid: <strong>{{ asteroid }}</strong></p>
|
||||||
<form method="POST" action="/preview" enctype="multipart/form-data">
|
<form method="POST" action="/preview" enctype="multipart/form-data">
|
||||||
<label>Titel:</label><br>
|
<label>Titel:</label><br>
|
||||||
<input type="text" name="title" style="width:100%" required><br><br>
|
<input type="text" name="title" style="width:100%" required><br><br>
|
||||||
|
|
@ -62,7 +73,7 @@ def new_article_form():
|
||||||
<button type="submit" name="action" value="preview">Vorschau generieren</button>
|
<button type="submit" name="action" value="preview">Vorschau generieren</button>
|
||||||
</form>
|
</form>
|
||||||
"""
|
"""
|
||||||
return form_html #
|
return render_template_string(form_html, asteroid=UBERSPACE_ASTEROID)
|
||||||
|
|
||||||
@app.route('/preview', methods=['POST'])
|
@app.route('/preview', methods=['POST'])
|
||||||
def preview_article():
|
def preview_article():
|
||||||
|
|
@ -73,7 +84,9 @@ def preview_article():
|
||||||
slug = secure_filename(data['title']).lower().replace('-', '_')
|
slug = secure_filename(data['title']).lower().replace('-', '_')
|
||||||
markdown_content = generate_markdown_content(data, slug)
|
markdown_content = generate_markdown_content(data, slug)
|
||||||
|
|
||||||
# Simulation der Vorschau
|
# In der Praxis müssten die Dateien hier temporär zwischengespeichert werden,
|
||||||
|
# um sie im /publish Schritt final zu übernehmen.
|
||||||
|
|
||||||
preview_html = f"""
|
preview_html = f"""
|
||||||
<h2>Vorschau: {data['title']}</h2>
|
<h2>Vorschau: {data['title']}</h2>
|
||||||
<hr>
|
<hr>
|
||||||
|
|
@ -83,16 +96,16 @@ def preview_article():
|
||||||
<p><i>[Hier würde der gerenderte Artikeltext stehen]</i></p>
|
<p><i>[Hier würde der gerenderte Artikeltext stehen]</i></p>
|
||||||
</div>
|
</div>
|
||||||
<hr>
|
<hr>
|
||||||
<form method="POST" action="/publish" enctype="multipart/form-data">
|
<form method="POST" action="/publish">
|
||||||
<input type="hidden" name="content_data" value='{markdown_content}'>
|
<input type="hidden" name="content_data" value='{markdown_content}'>
|
||||||
<input type="hidden" name="article_title" value="{data['title']}">
|
<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>
|
<p>Möchten Sie diesen Artikel jetzt auf <strong>{UBERSPACE_DOMAIN}</strong> veröffentlichen?</p>
|
||||||
<button type="submit">Artikel Bestätigen und Veröffentlichen</button>
|
<button type="submit">Artikel Bestätigen und Veröffentlichen (Git Push)</button>
|
||||||
<a href="/">Abbrechen und zurück</a>
|
<a href="/">Abbrechen und zurück</a>
|
||||||
</form>
|
</form>
|
||||||
"""
|
"""
|
||||||
return preview_html #
|
return preview_html
|
||||||
|
|
||||||
@app.route('/publish', methods=['POST'])
|
@app.route('/publish', methods=['POST'])
|
||||||
def publish_article():
|
def publish_article():
|
||||||
|
|
@ -101,15 +114,14 @@ def publish_article():
|
||||||
article_title = request.form.get('article_title')
|
article_title = request.form.get('article_title')
|
||||||
|
|
||||||
if not markdown_content:
|
if not markdown_content:
|
||||||
return "Fehler: Keine Inhaltsdaten vorhanden.", 400
|
flash("Fehler: Keine Inhaltsdaten vorhanden.")
|
||||||
|
return redirect(url_for('new_article_form'))
|
||||||
|
|
||||||
# Dateinamen und Pfad festlegen
|
# Dateinamen und Pfad festlegen
|
||||||
article_slug = secure_filename(article_title).lower().replace('-', '_')
|
article_slug = secure_filename(article_title).lower().replace('-', '_')
|
||||||
filename = f"{article_slug}.md"
|
filename = f"{article_slug}.md"
|
||||||
article_path = os.path.join(os.getcwd(), 'content', filename)
|
article_path = os.path.join(os.getcwd(), 'content', filename)
|
||||||
|
|
||||||
download_paths = []
|
|
||||||
|
|
||||||
# 1. Speichern des Markdown-Artikels
|
# 1. Speichern des Markdown-Artikels
|
||||||
try:
|
try:
|
||||||
with open(article_path, 'w', encoding='utf-8') as f:
|
with open(article_path, 'w', encoding='utf-8') as f:
|
||||||
|
|
@ -117,12 +129,18 @@ def publish_article():
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
return f"Fehler beim Speichern des Artikels: {e}", 500
|
return f"Fehler beim Speichern des Artikels: {e}", 500
|
||||||
|
|
||||||
# 2. Speichern der hochgeladenen Dateien (PDF/MP3)
|
# 2. Git Operationen ausführen
|
||||||
# Hinweis: In der Praxis müssen Dateien ggf. zwischen Preview und Publish gepuffert werden.
|
try:
|
||||||
for key in ['pdf_file', 'audio_file']:
|
# Wir übergeben die Umgebungsvariablen an die git_ops, falls dort
|
||||||
file = request.files.get(key)
|
# spezifische Commit-Messages oder Remote-Targets benötigt werden.
|
||||||
if file and file.filename != '':
|
git_ops.add_and_commit(filename, f"Neuer Artikel: {article_title}")
|
||||||
ext = ".pdf" if key == "pdf_file" else ".mp3"
|
git_ops.push_to_remote()
|
||||||
target_name = secure_filename(f"{article_slug}{ext}")
|
flash(f"Erfolgreich veröffentlicht auf {UBERSPACE_DOMAIN}!")
|
||||||
save_path = os.path.join(app.config['UPLOAD_FOLDER'], target_name)
|
except Exception as e:
|
||||||
file
|
return f"Git-Fehler: {e}", 500
|
||||||
|
|
||||||
|
return redirect(url_for('new_article_form'))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
# Lokal zum Testen, auf Uberspace wird dies via Gunicorn/Passenger gestartet
|
||||||
|
app.run(debug=True)
|
||||||
Loading…
Reference in New Issue