added checks for new pit stop form
This commit is contained in:
parent
00c6f6d71d
commit
47921e1941
@ -1,3 +0,0 @@
|
||||
from main import init_db
|
||||
|
||||
init_db()
|
43
app/main.py
43
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)
|
||||
|
@ -16,4 +16,8 @@ td {
|
||||
text-align: right;
|
||||
padding-left: 10px;
|
||||
padding-right: 10px;
|
||||
}
|
||||
}
|
||||
|
||||
.error {
|
||||
color: red;
|
||||
}]
|
@ -7,11 +7,6 @@
|
||||
{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
{% if data.error %}
|
||||
<div>
|
||||
<p class='error'><strong>Error:</strong> {{ data.error }}</p>
|
||||
</div>
|
||||
{% endif %}
|
||||
<form class="form-horizontal" id='createPitStop' action="{{ url_for('createPitStop') }}" method='post'>
|
||||
<fieldset>
|
||||
|
||||
@ -23,6 +18,9 @@
|
||||
<label class="control-label" for="date">Date of Pitstop</label>
|
||||
<div class="controls">
|
||||
<input id="date" name="date" placeholder="" class="input-large" required="" type="date" value='{{ data.last.date }}' />
|
||||
{% if data.error['date'] %}
|
||||
<p class='error'>{{ data.error['date'] }}</p>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -31,6 +29,9 @@
|
||||
<label class="control-label" for="odometer">Odometer</label>
|
||||
<div class="controls">
|
||||
<input id="odometer" name="odometer" placeholder="" class="input-large" required="" type="text" value='{{ data.last.odometer }}' />
|
||||
{% if data.error['odometer'] %}
|
||||
<p class='error'>{{ data.error['odometer'] }}</p>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -39,6 +40,9 @@
|
||||
<label class="control-label" for="litres">Litres</label>
|
||||
<div class="controls">
|
||||
<input id="litres" name="litres" placeholder="" class="input-large" required="" type="text" value='{{ data.last.litres }}' />
|
||||
{% if data.error['litres'] %}
|
||||
<p class='error'>{{ data.error['litres'] }}</p>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user