commit
8057c59329
109
app/main.py
109
app/main.py
|
@ -7,8 +7,10 @@ from flask import url_for
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
import uuid
|
import uuid
|
||||||
import hashlib
|
import hashlib
|
||||||
import time
|
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
from flask_wtf import Form
|
||||||
|
from wtforms import DateField, IntegerField, DecimalField
|
||||||
|
from wtforms.validators import DataRequired, ValidationError
|
||||||
import os
|
import os
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
@ -107,70 +109,53 @@ def index():
|
||||||
return redirect(url_for('get_pit_stops'))
|
return redirect(url_for('get_pit_stops'))
|
||||||
|
|
||||||
|
|
||||||
@app.route('/pitstops', methods=['POST'])
|
def date_check(form, field):
|
||||||
@requires_auth
|
if field.data < form.pitstop.date:
|
||||||
def create_pit_stop():
|
raise ValidationError('The new date must after %s' % form.pitstop.date)
|
||||||
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'])
|
def odometer_check(form, field):
|
||||||
|
if field.data <= form.pitstop.odometer:
|
||||||
|
raise ValidationError('The new odometer value must be higher than %i km' % form.pitstop.odometer)
|
||||||
|
|
||||||
|
|
||||||
|
def litres_check(form, field):
|
||||||
|
if field.data is not None and field.data <= 0:
|
||||||
|
raise ValidationError('You must fuel at least 0.1 l')
|
||||||
|
|
||||||
|
|
||||||
|
class CreatePitstopForm(Form):
|
||||||
|
date = DateField('Date of Pitstop', validators=[date_check])
|
||||||
|
odometer = IntegerField('Odometer (km)', validators=[odometer_check])
|
||||||
|
litres = DecimalField('Litres (l)', places=1, validators=[litres_check])
|
||||||
|
pitstop = None
|
||||||
|
|
||||||
|
def set_pitstop(self, pitstop):
|
||||||
|
self.pitstop = pitstop
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/pitstops/createForm', methods=['GET', 'POST'])
|
||||||
@requires_auth
|
@requires_auth
|
||||||
def create_pit_stop_form():
|
def create_pit_stop_form():
|
||||||
last_stop = Pitstop.query.order_by(Pitstop.date.desc()).first()
|
last_pitstop = Pitstop.query.order_by(Pitstop.id.desc()).first()
|
||||||
if last_stop is None:
|
if last_pitstop is None:
|
||||||
last_stop = Pitstop(0, 0, date(1970, 1, 1))
|
last_pitstop = Pitstop(0, 0, datetime.now())
|
||||||
values = dict()
|
|
||||||
values['odometer'] = last_stop.odometer
|
form = CreatePitstopForm()
|
||||||
values['litres'] = last_stop.litres
|
form.set_pitstop(last_pitstop)
|
||||||
values['date'] = time.strftime("%Y-%m-%d")
|
if form.validate_on_submit():
|
||||||
g.data['last'] = values
|
|
||||||
g.data['error'] = None
|
new_stop = Pitstop(form.odometer.data, form.litres.data, form.date.data)
|
||||||
return render_template('newPitStopForm.html', data=g.data)
|
sqldb.session.add(new_stop)
|
||||||
|
sqldb.session.commit()
|
||||||
|
return redirect(url_for('get_pit_stops'))
|
||||||
|
|
||||||
|
# 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'])
|
@app.route('/pitstops', methods=['GET'])
|
||||||
|
@ -197,7 +182,7 @@ def get_statistics():
|
||||||
average_distance = 0
|
average_distance = 0
|
||||||
average_litres_fuelled = 0
|
average_litres_fuelled = 0
|
||||||
average_litres_used = 0
|
average_litres_used = 0
|
||||||
|
|
||||||
if count > 0:
|
if count > 0:
|
||||||
sum_litres = 0
|
sum_litres = 0
|
||||||
for pitstop in pitstops:
|
for pitstop in pitstops:
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
Flask
|
Flask
|
||||||
Flask-SQLAlchemy
|
Flask-SQLAlchemy
|
||||||
|
Flask-WTF
|
||||||
|
|
|
@ -4,6 +4,25 @@
|
||||||
<li><a href='{{ url_for('get_manual') }}'>Manual</a></li>
|
<li><a href='{{ url_for('get_manual') }}'>Manual</a></li>
|
||||||
{%- endmacro %}
|
{%- endmacro %}
|
||||||
|
|
||||||
|
{% macro render_field(field) %}
|
||||||
|
<div class="form-group">
|
||||||
|
<label class="col-sm-2 control-label">
|
||||||
|
{{ field.label }}
|
||||||
|
</label>
|
||||||
|
<div class="col-sm-10">
|
||||||
|
{{ field(**kwargs)|safe }}
|
||||||
|
{% if field.errors %}
|
||||||
|
<p class='error'>
|
||||||
|
{% for error in field.errors %}
|
||||||
|
{{ error }}
|
||||||
|
{% endfor %}
|
||||||
|
</p>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endmacro %}
|
||||||
|
|
||||||
|
|
||||||
<!doctype html>
|
<!doctype html>
|
||||||
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang=""> <![endif]-->
|
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang=""> <![endif]-->
|
||||||
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8" lang=""> <![endif]-->
|
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8" lang=""> <![endif]-->
|
||||||
|
@ -69,4 +88,3 @@
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
|
|
|
@ -1,43 +1,13 @@
|
||||||
{% extends "layout.html" %}
|
{% extends "layout.html" %}
|
||||||
|
|
||||||
{% block body %}
|
{% block body %}
|
||||||
|
<form class='form-horizontal' method="POST">
|
||||||
<form class='form-horizontal' id='createPitStop' action="{{ url_for('create_pit_stop') }}" method='post'>
|
{{ form.hidden_tag() }}
|
||||||
|
{{ render_field(form.date) }}
|
||||||
<!-- Text input-->
|
{{ render_field(form.odometer) }}
|
||||||
<div class="form-group {% if data.error['date'] %}has-error{% endif %}">
|
{{ render_field(form.litres) }}
|
||||||
<label class="col-sm-2 control-label" for="date">Date of Pitstop</label>
|
<input type="submit" value="Go">
|
||||||
<div class="col-sm-10">
|
|
||||||
<input class="form-control" id="date" name="date" placeholder="" required="" type="date" value='{{ data.last.date }}' />
|
|
||||||
<p class='error'>{{ data.error['date'] }}</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Text input-->
|
|
||||||
<div class="form-group {% if data.error['odometer'] %}has-error{% endif %}">
|
|
||||||
<label class="col-sm-2 control-label" for="odometer">Odometer (km)</label>
|
|
||||||
<div class="col-sm-10">
|
|
||||||
<input class="form-control" id="odometer" name="odometer" placeholder="" type="number" value='{{ data.last.odometer }}' />
|
|
||||||
<p class='error'>{{ data.error['odometer'] }}</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Text input-->
|
|
||||||
<div class="form-group {% if data.error['litres'] %}has-error{% endif %}">
|
|
||||||
<label class="col-sm-2 control-label" for="litres">Litres (l)</label>
|
|
||||||
<div class="col-sm-10">
|
|
||||||
<input class="form-control" id="litres" name="litres" placeholder="" type="number" step='0.1' value='{{ data.last.litres }}' />
|
|
||||||
<p class='error'>{{ data.error['litres'] }}</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Button (Double) -->
|
|
||||||
<div class="form-group">
|
|
||||||
<div class="controls">
|
|
||||||
<button id="buttonLogId" name="buttonLogId" class="btn btn-success" onclick="document.getElementById('create_pit_stop').submit();">Log Pitstop</button>
|
|
||||||
<button id="buttonAbortId" name="buttonAbortId" class="btn btn-warning" onclick="window.location.href='{{ url_for('get_pit_stops') }}'" type="button" >Abort</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
{% endblock %}
|
|
||||||
|
{% endblock %}
|
||||||
|
|
Loading…
Reference in New Issue