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

67 lines
1.7 KiB
Python
Raw Normal View History

2022-05-10 19:11:49 +00:00
from flask import Flask, render_template, request, url_for
2022-05-11 00:58:27 +00:00
import logging
2022-05-10 19:11:49 +00:00
from werkzeug.utils import redirect
2022-05-11 03:16:14 +00:00
from .codes import correct_code
2022-05-11 00:58:27 +00:00
from .database import get_name_and_replies, record_reply
logger = logging.getLogger(__name__)
2022-05-10 19:11:49 +00:00
app = Flask('france_rsvp')
@app.route('/france/', methods=['GET', 'POST'])
def form():
error = None
if request.method == 'POST':
if (
'code' in request.form
and len(request.form['code']) == 4
):
2022-05-11 03:16:14 +00:00
code = correct_code(request.form['code'])
2022-05-11 03:16:02 +00:00
try:
get_name_and_replies(code)
except ValueError:
error = "Code invalide"
else:
return redirect(url_for('rsvp', code=code))
2022-05-10 19:11:49 +00:00
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):
2022-05-11 00:58:27 +00:00
error = None
2022-05-10 19:11:49 +00:00
if request.method == 'POST':
2022-05-11 00:58:27 +00:00
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'))
2022-05-10 19:11:49 +00:00
2022-05-11 00:58:27 +00:00
# Lookup guests from code
adults = None
kids = None
try:
name, adults, kids = get_name_and_replies(code)
except ValueError:
error = "Code invalide"
2022-05-10 19:11:49 +00:00
return render_template(
'rsvp.html',
adults=adults,
kids=kids,
2022-05-11 00:58:27 +00:00
error=error,
2022-05-10 19:11:49 +00:00
)