Initial project import: Flask app, templates, init script, README

This commit is contained in:
Albert
2025-11-08 23:25:18 +01:00
commit 4b4a3fb56f
7 changed files with 240 additions and 0 deletions

16
.gitignore vendored Normal file
View File

@@ -0,0 +1,16 @@
# Virtual environment
.venv/
# SQLite DB
anmeldung.db
# Python caches
__pycache__/
*.pyc
# Editor settings
.vscode/
.idea/
# OS files
.DS_Store

25
README.md Normal file
View File

@@ -0,0 +1,25 @@
# Anmeldung_v2
Kleine Flask-Webapp, um einen papierbasierten Anmeldebogen durch ein Formular zu ersetzen und die Eingaben in einer SQLite-Datenbank zu speichern.
Installieren:
```bash
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```
DB initialisieren und Beispiel-Fragen anlegen:
```bash
python init_db.py
```
App starten:
```bash
python app.py
```
Das Formular ist dann unter http://127.0.0.1:5000/ erreichbar.

116
app.py Normal file
View File

@@ -0,0 +1,116 @@
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
import os
import re
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DB_PATH = os.path.join(BASE_DIR, 'anmeldung.db')
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_PATH}'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Adresse(db.Model):
id = db.Column(db.Integer, primary_key=True)
vorname = db.Column(db.String(100), nullable=False)
nachname = db.Column(db.String(100), nullable=False)
strasse = db.Column(db.String(200), nullable=False)
hausnummer = db.Column(db.String(50), nullable=True)
plz = db.Column(db.String(20), nullable=False)
ort = db.Column(db.String(100), nullable=False)
land = db.Column(db.String(50), default='Deutschland')
telefon_vorwahl = db.Column(db.String(20))
telefon_nummer = db.Column(db.String(50))
email = db.Column(db.String(200))
class Frage(db.Model):
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.String(500), nullable=False)
class Antwort(db.Model):
id = db.Column(db.Integer, primary_key=True)
adresse_id = db.Column(db.Integer, db.ForeignKey('adresse.id'), nullable=False)
frage_id = db.Column(db.Integer, db.ForeignKey('frage.id'), nullable=False)
text = db.Column(db.String(1000), nullable=True)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
# Adresse speichern
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()
# server-side E-Mail Validierung (einfache Prüfung)
errors = {}
email_re = re.compile(r"[^@]+@[^@]+\.[^@]+")
if email:
if not email_re.match(email):
errors['email'] = 'Ungültige E-Mail-Adresse'
# PLZ Validierung: genau 5 Ziffern
if plz:
if not re.fullmatch(r"\d{5}", plz):
errors['plz'] = 'Postleitzahl muss genau 5 Ziffern haben'
if errors:
fragen = Frage.query.all()
# pass form data back to template so fields are preserved
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()
# Antworten speichern
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()
# Nach erfolgreichem Speichern weiterleiten
return redirect(url_for('danke', id=adresse.id))
# GET: Formular anzeigen
fragen = Frage.query.all()
return render_template('index.html', fragen=fragen)
@app.route('/danke')
def danke():
id = request.args.get('id')
adresse = Adresse.query.get(id)
return render_template('danke.html', adresse=adresse)
if __name__ == '__main__':
# Ensure DB exists
if not os.path.exists(DB_PATH):
db.create_all()
app.run(debug=True)

24
init_db.py Normal file
View File

@@ -0,0 +1,24 @@
from app import app, db, Frage
def init_db():
# Ensure we run DB commands inside the Flask application context
with app.app_context():
db.drop_all()
db.create_all()
sample = [
'Haben Sie besondere Ernährungsbedürfnisse?',
'Benötigen Sie eine Übernachtung?',
'Möchten Sie unseren Newsletter erhalten?'
]
for text in sample:
f = Frage(text=text)
db.session.add(f)
db.session.commit()
print('DB initialisiert und Beispiel-Fragen angelegt.')
if __name__ == '__main__':
init_db()

2
requirements.txt Normal file
View File

@@ -0,0 +1,2 @@
Flask>=2.0
Flask-SQLAlchemy>=3.0

17
templates/danke.html Normal file
View File

@@ -0,0 +1,17 @@
<!doctype html>
<html lang="de">
<head>
<meta charset="utf-8">
<title>Danke</title>
</head>
<body>
<h1>Danke für Ihre Anmeldung</h1>
{% if adresse %}
<p>Vielen Dank, {{ adresse.vorname }} {{ adresse.nachname }}. Ihre Daten wurden gespeichert.</p>
<p>Adresse: {{ adresse.strasse }}{% if adresse.hausnummer %} {{ adresse.hausnummer }}{% endif %}, {{ adresse.plz }} {{ adresse.ort }}</p>
{% else %}
<p>Eintrag gespeichert.</p>
{% endif %}
<p><a href="/">Zurück zum Formular</a></p>
</body>
</html>

40
templates/index.html Normal file
View File

@@ -0,0 +1,40 @@
<!doctype html>
<html lang="de">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Anmeldebogen</title>
<style>label{display:block;margin-top:8px}</style>
</head>
<body>
<h1>Anmeldebogen</h1>
<form method="post">
<fieldset>
<legend>Persönliche Daten</legend>
<label>Vorname <input name="vorname" required value="{{ form.vorname if form and form.vorname }}"></label>
<label>Nachname <input name="nachname" required value="{{ form.nachname if form and form.nachname }}"></label>
<label>Straße <input name="strasse" required value="{{ form.strasse if form and form.strasse }}"></label>
<label>Hausnummer <input name="hausnummer" value="{{ form.hausnummer if form and form.hausnummer }}"></label>
<label>PLZ <input name="plz" required maxlength="5" pattern="\d{5}" title="5-stellige PLZ" value="{{ form.plz if form and form.plz }}"> {% if errors and errors.plz %}<strong style="color:red">{{ errors.plz }}</strong>{% endif %}</label>
<label>Ort <input name="ort" required value="{{ form.ort if form and form.ort }}"></label>
<label>Land <input name="land" value="{{ form.land if form and form.land else 'Deutschland' }}"></label>
<label>Telefon Vorwahl <input name="telefon_vorwahl" value="{{ form.telefon_vorwahl if form and form.telefon_vorwahl }}"></label>
<label>Telefon Nummer <input name="telefon_nummer" value="{{ form.telefon_nummer if form and form.telefon_nummer }}"></label>
<label>E-Mail <input name="email" type="email" value="{{ form.email if form and form.email }}"> {% if errors and errors.email %}<strong style="color:red">{{ errors.email }}</strong>{% endif %}</label>
</fieldset>
{% if fragen %}
<fieldset>
<legend>Fragen</legend>
{% for frage in fragen %}
<label>{{ frage.text }}
<input name="frage_{{ frage.id }}">
</label>
{% endfor %}
</fieldset>
{% endif %}
<p><button type="submit">Absenden</button></p>
</form>
</body>
</html>