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

69 lines
1.8 KiB
Python
Raw Normal View History

2022-05-12 21:26:32 +00:00
from flask import Flask, render_template, redirect, request, url_for
2022-05-11 00:58:27 +00:00
import logging
2022-05-10 19:11:49 +00:00
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/')
def index():
return render_template('index.html')
2022-05-10 19:11:49 +00:00
@app.route('/france/rsvp', methods=['GET', 'POST'])
2022-05-10 19:11:49 +00:00
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):
# 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")
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
return render_template(
'rsvp.html',
adults=adults,
2022-05-22 04:57:14 +00:00
children=children,
2022-05-11 00:58:27 +00:00
error=error,
2022-05-10 19:11:49 +00:00
)