feat(app): refactor to app-factory; add application package and routes
This commit is contained in:
77
application/routes.py
Normal file
77
application/routes.py
Normal file
@@ -0,0 +1,77 @@
|
||||
from flask import Blueprint, render_template, request, redirect, url_for, current_app
|
||||
from .extensions import db
|
||||
from .models import Adresse, Frage, Antwort
|
||||
from utils import generate_vcard
|
||||
import re
|
||||
|
||||
bp = Blueprint('public', __name__)
|
||||
|
||||
|
||||
@bp.route('/', methods=['GET', 'POST'])
|
||||
def index():
|
||||
if request.method == 'POST':
|
||||
vorname = request.form.get('vorname', '').strip()
|
||||
nachname = request.form.get('nachname', '').strip()
|
||||
strasse = request.form.get('strasse', '').strip()
|
||||
hausnummer = request.form.get('hausnummer', '').strip()
|
||||
plz = request.form.get('plz', '').strip()
|
||||
ort = request.form.get('ort', '').strip()
|
||||
land = request.form.get('land', 'Deutschland').strip()
|
||||
telefon_vorwahl = request.form.get('telefon_vorwahl', '').strip()
|
||||
telefon_nummer = request.form.get('telefon_nummer', '').strip()
|
||||
email = request.form.get('email', '').strip()
|
||||
|
||||
errors = {}
|
||||
email_re = re.compile(r"[^@]+@[^@]+\.[^@]+")
|
||||
if email:
|
||||
if not email_re.match(email):
|
||||
errors['email'] = 'Ungültige E-Mail-Adresse'
|
||||
if plz:
|
||||
if not re.fullmatch(r"\d{5}", plz):
|
||||
errors['plz'] = 'Postleitzahl muss genau 5 Ziffern haben'
|
||||
|
||||
if errors:
|
||||
fragen = Frage.query.all()
|
||||
form = request.form.to_dict()
|
||||
return render_template('index.html', fragen=fragen, errors=errors, form=form)
|
||||
|
||||
adresse = Adresse(
|
||||
vorname=vorname,
|
||||
nachname=nachname,
|
||||
strasse=strasse,
|
||||
hausnummer=hausnummer,
|
||||
plz=plz,
|
||||
ort=ort,
|
||||
land=land,
|
||||
telefon_vorwahl=telefon_vorwahl,
|
||||
telefon_nummer=telefon_nummer,
|
||||
email=email,
|
||||
)
|
||||
db.session.add(adresse)
|
||||
db.session.commit()
|
||||
|
||||
fragen = Frage.query.all()
|
||||
for frage in fragen:
|
||||
key = f'frage_{frage.id}'
|
||||
antwort_text = request.form.get(key, '').strip()
|
||||
antwort = Antwort(adresse_id=adresse.id, frage_id=frage.id, text=antwort_text)
|
||||
db.session.add(antwort)
|
||||
db.session.commit()
|
||||
|
||||
try:
|
||||
base_dir = current_app.config.get('BASE_DIR') if current_app.config.get('BASE_DIR') else getattr(current_app, 'BASE_DIR', '.')
|
||||
generate_vcard(adresse, base_dir)
|
||||
except Exception as e:
|
||||
current_app.logger.exception('Fehler beim Erzeugen der vCard for adresse id=%s: %s', adresse.id if hasattr(adresse, 'id') else 'unknown', e)
|
||||
|
||||
return redirect(url_for('public.danke', id=adresse.id))
|
||||
|
||||
fragen = Frage.query.all()
|
||||
return render_template('index.html', fragen=fragen)
|
||||
|
||||
|
||||
@bp.route('/danke')
|
||||
def danke():
|
||||
id = request.args.get('id')
|
||||
adresse = db.session.get(Adresse, int(id)) if id else None
|
||||
return render_template('danke.html', adresse=adresse)
|
||||
Reference in New Issue
Block a user