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 }}
-