From 8c8803874e0ef6539277fbef1308b583d766c20f Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Sun, 17 Apr 2016 12:50:31 +0200 Subject: [PATCH] First step for WTForms --- app/main.py | 85 +++++++++---------------------- app/requirements.txt | 1 + app/templates/layout.html | 1 - app/templates/newPitStopForm.html | 59 +++++++++------------ 4 files changed, 50 insertions(+), 96 deletions(-) diff --git a/app/main.py b/app/main.py index de85eb0..c4aa102 100644 --- a/app/main.py +++ b/app/main.py @@ -10,6 +10,10 @@ import hashlib import time from functools import wraps +from flask_wtf import Form +from wtforms import DateField, IntegerField, DecimalField +from wtforms.validators import DataRequired + app = Flask(__name__) DATABASE = '/data/rollerverbrauch.db' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+DATABASE @@ -106,71 +110,32 @@ def teardown_request(exception): def index(): return redirect(url_for('get_pit_stops')) +class CreatePitstopForm(Form): + date = DateField('Date of Pitstop') + odometer = IntegerField('Odometer (km)') + litres = DecimalField('Litres (l)', places=1) -@app.route('/pitstops', methods=['POST']) + +@app.route('/pitstops/createForm', methods=['GET', 'POST']) @requires_auth -def create_pit_stop(): +def create_pit_stop_form(): + form = CreatePitstopForm() + if form.validate_on_submit(): + new_stop = Pitstop(form.odometer.data, form.litres.data, form.date.data) + sqldb.session.add(new_stop) + sqldb.session.commit() + return redirect(url_for('get_pit_stops')) + last_pitstop = Pitstop.query.order_by(Pitstop.date.desc()).first() if last_pitstop is None: last_pitstop = Pitstop(0, 0, None) - error_msg = {} - - date_of_pitstop = request.form['date'] - try: - date_of_pitstop = datetime.strptime(date_of_pitstop, '%Y-%m-%d').strftime('%Y-%m-%d') - except ValueError: - error_msg['date'] = 'invalid date, only YYYY-MM-DD is allowed' - date_of_pitstop = request.form['date'] - - odometer = request.form['odometer'] - try: - odometer = int(odometer) - except ValueError: - error_msg['odometer'] = 'Illegal Value, only Integers allowed' - odometer = None - if odometer is not None and odometer <= last_pitstop.odometer: - error_msg['odometer'] = 'Illegal Value, new Value must be bigger as given value' - odometer = request.form['odometer'] - if odometer is None: - odometer = request.form['odometer'] - - litres = request.form['litres'] - try: - litres = float(litres) - except ValueError: - error_msg['litres'] = 'Illegal Value, only floating point allowed' - litres = None - if litres is not None and litres <= 0: - error_msg['litres'] = 'Litres must not be 0' - litres = request.form['litres'] - if litres is None: - litres = request.form['litres'] - - # error checking here - if len(error_msg) > 0: - data = {'last': {'date': date_of_pitstop, 'odometer': odometer, 'litres': litres}, 'error': error_msg} - return render_template('newPitStopForm.html', data=data) - new_stop = Pitstop(odometer, litres, datetime.strptime(date_of_pitstop, '%Y-%m-%d')) - sqldb.session.add(new_stop) - sqldb.session.commit() - - return redirect(url_for('get_pit_stops')) - - -@app.route('/pitstops/createForm', methods=['GET']) -@requires_auth -def create_pit_stop_form(): - last_stop = Pitstop.query.order_by(Pitstop.date.desc()).first() - if last_stop is None: - last_stop = Pitstop(0, 0, date(1970, 1, 1)) - values = dict() - values['odometer'] = last_stop.odometer - values['litres'] = last_stop.litres - values['date'] = time.strftime("%Y-%m-%d") - g.data['last'] = values - g.data['error'] = None - return render_template('newPitStopForm.html', data=g.data) + # dynamically set values + form.odometer.default = last_pitstop.odometer + form.litres.default = last_pitstop.litres + form.date.default = datetime.now() + form.process() + return render_template('newPitStopForm.html', form=form) @app.route('/pitstops', methods=['GET']) @@ -197,7 +162,7 @@ def get_statistics(): average_distance = 0 average_litres_fuelled = 0 average_litres_used = 0 - + if count > 0: sum_litres = 0 for pitstop in pitstops: diff --git a/app/requirements.txt b/app/requirements.txt index fb675a9..ce9fbfc 100644 --- a/app/requirements.txt +++ b/app/requirements.txt @@ -1,2 +1,3 @@ Flask Flask-SQLAlchemy +Flask-WTF diff --git a/app/templates/layout.html b/app/templates/layout.html index 2688f76..f1ae071 100644 --- a/app/templates/layout.html +++ b/app/templates/layout.html @@ -69,4 +69,3 @@ - diff --git a/app/templates/newPitStopForm.html b/app/templates/newPitStopForm.html index 190e0eb..47ff31d 100644 --- a/app/templates/newPitStopForm.html +++ b/app/templates/newPitStopForm.html @@ -1,43 +1,32 @@ {% extends "layout.html" %} {% block body %} +
+ {{ form.hidden_tag() }} - - - -
- -
- -

{{ data.error['date'] }}

-
-
- - -
- -
- -

{{ data.error['odometer'] }}

-
-
- - -
- -
- -

{{ data.error['litres'] }}

-
-
- -
-
- - + +
+ {{ form.date(size=20) }}
+ +
+ +
+ {{ form.odometer(size=20) }} +
+
+ +
+ +
+ {{ form.litres(size=20) }} +
+
+ + - -{% endblock %} \ No newline at end of file + + +{% endblock %}