diff --git a/france/france_rsvp/templates/rsvp.html b/france/france_rsvp/templates/rsvp.html index caf1c1c..74f0e3d 100644 --- a/france/france_rsvp/templates/rsvp.html +++ b/france/france_rsvp/templates/rsvp.html @@ -4,19 +4,18 @@ RSVP - Vicky & Remi + {% if error %} +

{{ error }}

+ {% endif %}
- {% for person in adults %}

- - + +

- {% endfor %} - {% for person in kids %}

- - + +

- {% endfor %}

diff --git a/france/france_rsvp/web.py b/france/france_rsvp/web.py index 0f42230..99d69a1 100644 --- a/france/france_rsvp/web.py +++ b/france/france_rsvp/web.py @@ -1,6 +1,11 @@ from flask import Flask, render_template, request, url_for +import logging from werkzeug.utils import redirect +from .database import get_name_and_replies, record_reply + + +logger = logging.getLogger(__name__) app = Flask('france_rsvp') @@ -26,16 +31,29 @@ def thanks(): @app.route('/france/', methods=['GET', 'POST']) def rsvp(code): + error = None if request.method == 'POST': - # TODO: Store response - return redirect(url_for('thanks')) + 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')) - # TODO: Lookup guest from code - adults = ['Remi', 'Vicky'] - kids = ['LB'] + # Lookup guests from code + adults = None + kids = None + try: + name, adults, kids = get_name_and_replies(code) + except ValueError: + error = "Code invalide" return render_template( 'rsvp.html', adults=adults, kids=kids, + error=error, )