vicky-remi-wedding-website/france/france_rsvp/web.py

69 lines
1.8 KiB
Python

from flask import Flask, render_template, redirect, request, url_for
import logging
from .codes import correct_code
from .database import get_name_and_replies, record_reply
logger = logging.getLogger(__name__)
app = Flask('france_rsvp')
@app.route('/france/')
def index():
return render_template('index.html')
@app.route('/france/rsvp', methods=['GET', 'POST'])
def form():
error = None
if request.method == 'POST':
if (
'code' in request.form
and len(request.form['code']) == 4
):
code = correct_code(request.form['code'])
try:
get_name_and_replies(code)
except ValueError:
error = "Code invalide"
else:
return redirect(url_for('rsvp', code=code))
else:
error = "Code invalide"
return render_template('form.html', error=error)
@app.route('/france/thanks')
def thanks():
return render_template('thanks.html')
@app.route('/france/<code>', methods=['GET', 'POST'])
def rsvp(code):
# Lookup guests from code
adults = None
children = None
try:
name, adults, children = get_name_and_replies(code)
except ValueError:
return render_template('form.html', error="Code invalide")
error = None
if request.method == 'POST':
try:
adults = int(request.form['adults'], 10)
children = int(request.form['children'], 10)
except (KeyError, ValueError, OverflowError):
logger.warning("Invalid reply", exc_info=True)
error = "Nombres invalides"
else:
record_reply(code, adults, children)
return redirect(url_for('thanks'))
return render_template(
'rsvp.html',
adults=adults,
children=children,
error=error,
)