From 47921e1941eab0819ec6d8495c3f3efec5941332 Mon Sep 17 00:00:00 2001 From: joachim lusiardi Date: Mon, 9 Mar 2015 20:06:17 +0100 Subject: [PATCH] added checks for new pit stop form --- app/init.py | 3 --- app/main.py | 43 +++++++++++++++++++++++++++---- app/static/main.css | 6 ++++- app/templates/newPitStopForm.html | 14 ++++++---- 4 files changed, 52 insertions(+), 14 deletions(-) delete mode 100644 app/init.py diff --git a/app/init.py b/app/init.py deleted file mode 100644 index 85a8291..0000000 --- a/app/init.py +++ /dev/null @@ -1,3 +0,0 @@ -from main import init_db - -init_db() \ No newline at end of file diff --git a/app/main.py b/app/main.py index 75742fa..5f0922a 100644 --- a/app/main.py +++ b/app/main.py @@ -8,7 +8,7 @@ from contextlib import closing import os, os.path app = Flask(__name__) -DATABASE = '/data/rollerverbrauch.db' +DATABASE = '../rollerverbrauch.db' DEBUG = True SECRET_KEY = 'development key' USERNAME = 'admin' @@ -45,11 +45,44 @@ def index(): @app.route('/pitstops', methods=['POST']) def createPitStop(): + last_pitstop = getLastPitStop() + errorMsg = {} + date = request.form['date'] + try: + date = datetime.strptime(date, '%Y-%m-%d').strftime('%Y-%m-%d') + except ValueError: + errorMsg['date'] = 'invalid date, only YYYY-MM-DD is allowed' + date = request.form['date'] + odometer = request.form['odometer'] + try: + odometer = int(odometer) + except ValueError: + errorMsg['odometer'] = 'Illegal Value, only Integers allowed' + odometer = None + if odometer is not None and odometer <= last_pitstop['odometer']: + errorMsg['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: + errorMsg['litres'] = 'Illegal Value, only floating point allowed' + litres = None + if litres is not None and litres <= 0: + errorMsg['litres'] = 'Litres must not be 0' + litres = request.form['litres'] + if litres is None: + litres = request.form['litres'] + # error checking here + if len(errorMsg) > 0: + data = {'last': {'date': date, 'odometer': odometer, 'litres': litres}, 'error': errorMsg} + return render_template('newPitStopForm.html', data=data) addPitStop(date, odometer, litres) @@ -74,13 +107,13 @@ def getStatistics(): averageDistance = 0 averageLitresFuelled = 0 averageLitresUsed = 0 - count = len(pitstops) - if len(pitstops) > 0: + + if count > 0: sumLitres = 0 for pitstop in pitstops: sumLitres += pitstop['litres'] averageLitresFuelled = round(sumLitres/count, 2) - if len(pitstops) > 1: + if count > 1: distance = pitstops[-1]['odometer'] - pitstops[0]['odometer'] averageDistance = round(distance/(count - 1), 2) averageLitresUsed = round(100 * (sumLitres-pitstops[0]['litres'])/distance, 2) diff --git a/app/static/main.css b/app/static/main.css index 9b5533b..b3f581e 100644 --- a/app/static/main.css +++ b/app/static/main.css @@ -16,4 +16,8 @@ td { text-align: right; padding-left: 10px; padding-right: 10px; -} \ No newline at end of file +} + +.error { + color: red; +}] \ No newline at end of file diff --git a/app/templates/newPitStopForm.html b/app/templates/newPitStopForm.html index c81e58a..75a3b44 100644 --- a/app/templates/newPitStopForm.html +++ b/app/templates/newPitStopForm.html @@ -7,11 +7,6 @@ {% endblock %} {% block body %} - {% if data.error %} -
-

Error: {{ data.error }}

-
- {% endif %}
@@ -23,6 +18,9 @@
+ {% if data.error['date'] %} +

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

+ {% endif %}
@@ -31,6 +29,9 @@
+ {% if data.error['odometer'] %} +

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

+ {% endif %}
@@ -39,6 +40,9 @@
+ {% if data.error['litres'] %} +

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

+ {% endif %}