dlw-frontend/src/dlw/admin.py

85 lines
3.1 KiB
Python

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
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = os.path.join(os.getcwd(), 'downloads')
app.config['CONTENT_FOLDER'] = os.path.join(os.getcwd(), 'content')
app.secret_key = os.getenv('FLASK_SECRET_KEY', 'dev-key-123')
# Verzeichnisse sicherstellen
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
os.makedirs(app.config['CONTENT_FOLDER'], exist_ok=True)
def generate_markdown_content(data, slug):
return f"""---
Title: {data['title']}
Date: {time.strftime("%Y-%m-%d %H:%M")}
Slug: {slug}
Description: {data['description']}
Abstract: {data['abstract']}
PDF_Download: /downloads/{slug}.pdf
Audio_Download: /downloads/{slug}.mp3
---
{data['article_body']}
"""
@app.route('/', methods=['GET'])
def index():
form_html = """
<h1>Blog Admin</h1>
<form method="POST" action="/preview" enctype="multipart/form-data">
<input type="text" name="title" placeholder="Titel" required><br>
<input type="text" name="description" placeholder="Kurzbeschreibung"><br>
<textarea name="abstract" placeholder="Abstract"></textarea><br>
<textarea name="article_body" placeholder="Inhalt (Markdown)" required></textarea><br>
PDF: <input type="file" name="pdf_file"><br>
MP3: <input type="file" name="audio_file"><br>
<button type="submit">Vorschau</button>
</form>
"""
return render_template_string(form_html)
@app.route('/preview', methods=['POST'])
def preview():
data = request.form
slug = secure_filename(data['title']).lower().replace('-', '_')
# Dateien temporär speichern (vereinfacht für dieses Beispiel)
if 'pdf_file' in request.files:
request.files['pdf_file'].save(os.path.join(app.config['UPLOAD_FOLDER'], f"{slug}.pdf"))
if 'audio_file' in request.files:
request.files['audio_file'].save(os.path.join(app.config['UPLOAD_FOLDER'], f"{slug}.mp3"))
markdown_content = generate_markdown_content(data, slug)
return render_template_string("""
<h2>Vorschau: {{ title }}</h2>
<pre>{{ content }}</pre>
<form method="POST" action="/publish">
<input type="hidden" name="slug" value="{{ slug }}">
<input type="hidden" name="title" value="{{ title }}">
<input type="hidden" name="content" value='{{ content }}'>
<button type="submit">Veröffentlichen</button>
</form>
""", title=data['title'], content=markdown_content, slug=slug)
@app.route('/publish', methods=['POST'])
def publish():
slug = request.form.get('slug')
content = request.form.get('content')
title = request.form.get('title')
file_path = os.path.join(app.config['CONTENT_FOLDER'], f"{slug}.md")
with open(file_path, 'w') as f:
f.write(content)
# Git Workflow
success, msg = git_ops.commit_and_push_article(file_path, [], title)
flash(msg)
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)