Files
3d.lehrstun.de/app.py

96 lines
2.6 KiB
Python
Raw Normal View History

2026-05-24 18:56:59 +02:00
"""3D-Druck-AG — Flask-Bootstrap.
Nach V8 (siehe docs/decisions.md#E-020): Blueprint-Struktur.
Diese Datei laedt die Konfiguration, registriert Blueprints, definiert
Context-Processor + Error-Handler.
Routen leben in routes/. Externe Services in services/.
Quickstart (lokal):
python -c "from database import init_db; init_db()"
python seed.py
python app.py # http://localhost:5057
"""
import os
from pathlib import Path
from dotenv import load_dotenv
from flask import Flask, render_template, request
from tiere import tier_emoji, tier_name
# Routes-Blueprints
from routes.oeffentlich import bp as bp_oeffentlich
from routes.profil import bp as bp_profil
from routes.admin import bp as bp_admin
from routes.api import bp as bp_api
load_dotenv()
app = Flask(__name__)
app.secret_key = os.environ.get("FLASK_SECRET", "dev-key-bitte-aendern")
# Blueprints registrieren
app.register_blueprint(bp_oeffentlich)
app.register_blueprint(bp_profil)
app.register_blueprint(bp_admin)
app.register_blueprint(bp_api)
# ===========================================================================
# Markdown-Filter fuer Templates (Pattern wie arduino.lehrstun.de)
# ===========================================================================
import markdown as _markdown
from markupsafe import Markup
2026-05-24 18:56:59 +02:00
@app.template_filter("markdown")
def markdown_filter(text):
"""Rendert Markdown zu HTML und markiert das Ergebnis als 'safe',
damit Jinja2 die HTML-Tags nicht escaped (sonst sieht man <strong>
statt fettem Text)."""
2026-05-24 18:56:59 +02:00
if not text:
return ""
html = _markdown.markdown(
2026-05-24 18:56:59 +02:00
text,
extensions=["fenced_code", "tables", "nl2br"],
output_format="html5",
)
return Markup(html)
2026-05-24 18:56:59 +02:00
# ===========================================================================
# Context-Processor: in jedem Template verfuegbar
# ===========================================================================
@app.context_processor
def inject_globals():
return {
"tier_emoji": tier_emoji,
"tier_name": tier_name,
}
# ===========================================================================
# Error-Handler
# ===========================================================================
@app.errorhandler(404)
def err_404(e):
return render_template("404.html"), 404
@app.errorhandler(500)
def err_500(e):
return render_template("500.html"), 500
@app.errorhandler(501)
def err_501(e):
return render_template("501.html"), 501
if __name__ == "__main__":
# Port 5057 (5055 = quiz-collector, 5056 = arduino-db)
app.run(host="0.0.0.0", port=5057, debug=True)