2015-03-06 21:58:53 +01:00
|
|
|
from datetime import datetime
|
|
|
|
from flask import Flask
|
2015-08-03 22:24:55 +02:00
|
|
|
from flask import render_template, make_response
|
2015-03-06 21:58:53 +01:00
|
|
|
from flask import request, redirect, g
|
2015-03-16 22:06:59 +01:00
|
|
|
from flask import url_for
|
2015-08-03 22:24:55 +02:00
|
|
|
from flask import Response
|
|
|
|
import hashlib
|
2015-03-16 22:06:59 +01:00
|
|
|
import os.path
|
2015-03-12 08:02:17 +01:00
|
|
|
import time
|
2015-03-06 21:58:53 +01:00
|
|
|
|
2015-08-03 22:24:55 +02:00
|
|
|
from functools import wraps
|
|
|
|
|
2015-03-16 22:06:59 +01:00
|
|
|
import db
|
|
|
|
|
2015-03-06 21:58:53 +01:00
|
|
|
app = Flask(__name__)
|
2015-03-09 20:11:03 +01:00
|
|
|
DATABASE = '/data/rollerverbrauch.db'
|
2015-03-06 21:58:53 +01:00
|
|
|
DEBUG = True
|
|
|
|
SECRET_KEY = 'development key'
|
|
|
|
app.config.from_object(__name__)
|
|
|
|
|
2016-04-14 07:32:06 +02:00
|
|
|
|
2015-08-03 22:24:55 +02:00
|
|
|
def check_auth(username, password):
|
|
|
|
salt = g.db2.get_salt_for_user(username)
|
2016-04-14 07:32:06 +02:00
|
|
|
if salt is None:
|
2015-08-03 22:24:55 +02:00
|
|
|
return False
|
|
|
|
m = hashlib.sha256(password.encode('utf-8'))
|
|
|
|
m = hashlib.sha256((m.hexdigest()+salt).encode('utf-8'))
|
|
|
|
digest = m.hexdigest()
|
|
|
|
ok = g.db2.check_password_for_user(username, digest)
|
|
|
|
if not ok:
|
|
|
|
app.logger.error("digest: " + digest)
|
|
|
|
return ok
|
|
|
|
|
2016-04-14 07:32:06 +02:00
|
|
|
|
2015-08-03 22:24:55 +02:00
|
|
|
def authenticate():
|
|
|
|
resp = make_response(render_template('login_required.html'), 401)
|
|
|
|
resp.headers['WWW-Authenticate'] = 'Basic realm="Login Required"'
|
|
|
|
return resp
|
|
|
|
|
2016-04-14 07:32:06 +02:00
|
|
|
|
2015-08-03 22:24:55 +02:00
|
|
|
def requires_auth(f):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated(*args, **kwargs):
|
|
|
|
auth = request.authorization
|
|
|
|
if not auth or not check_auth(auth.username, auth.password):
|
|
|
|
return authenticate()
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
return decorated
|
|
|
|
|
2016-04-14 07:32:06 +02:00
|
|
|
|
2015-03-06 21:58:53 +01:00
|
|
|
@app.before_request
|
|
|
|
def before_request():
|
2015-03-16 22:06:59 +01:00
|
|
|
g.db2 = db.Db(app.config['DATABASE'])
|
|
|
|
g.data = {}
|
2016-04-14 07:32:06 +02:00
|
|
|
|
2015-03-06 21:58:53 +01:00
|
|
|
|
|
|
|
@app.teardown_request
|
|
|
|
def teardown_request(exception):
|
2015-03-16 22:06:59 +01:00
|
|
|
pass
|
2015-03-06 21:58:53 +01:00
|
|
|
|
2016-04-14 07:32:06 +02:00
|
|
|
|
2015-03-06 21:58:53 +01:00
|
|
|
@app.route('/')
|
2015-08-03 22:24:55 +02:00
|
|
|
@requires_auth
|
2015-03-06 21:58:53 +01:00
|
|
|
def index():
|
2015-03-16 22:06:59 +01:00
|
|
|
return redirect(url_for('get_pit_stops'))
|
|
|
|
|
2015-03-06 21:58:53 +01:00
|
|
|
|
|
|
|
@app.route('/pitstops', methods=['POST'])
|
2015-08-03 22:24:55 +02:00
|
|
|
@requires_auth
|
2015-03-16 22:06:59 +01:00
|
|
|
def create_pit_stop():
|
2016-04-14 07:32:06 +02:00
|
|
|
last_pitstop = g.db2.get_last_pit_stop()
|
|
|
|
error_msg = {}
|
2015-03-09 20:06:17 +01:00
|
|
|
|
2015-03-06 21:58:53 +01:00
|
|
|
date = request.form['date']
|
2015-03-09 20:06:17 +01:00
|
|
|
try:
|
|
|
|
date = datetime.strptime(date, '%Y-%m-%d').strftime('%Y-%m-%d')
|
|
|
|
except ValueError:
|
2016-04-14 07:32:06 +02:00
|
|
|
error_msg['date'] = 'invalid date, only YYYY-MM-DD is allowed'
|
2015-03-09 20:06:17 +01:00
|
|
|
date = request.form['date']
|
|
|
|
|
2015-03-06 21:58:53 +01:00
|
|
|
odometer = request.form['odometer']
|
2015-03-09 20:06:17 +01:00
|
|
|
try:
|
|
|
|
odometer = int(odometer)
|
|
|
|
except ValueError:
|
2016-04-14 07:32:06 +02:00
|
|
|
error_msg['odometer'] = 'Illegal Value, only Integers allowed'
|
2015-03-09 20:06:17 +01:00
|
|
|
odometer = None
|
|
|
|
if odometer is not None and odometer <= last_pitstop['odometer']:
|
2016-04-14 07:32:06 +02:00
|
|
|
error_msg['odometer'] = 'Illegal Value, new Value must be bigger as given value'
|
2015-03-09 20:06:17 +01:00
|
|
|
odometer = request.form['odometer']
|
|
|
|
if odometer is None:
|
|
|
|
odometer = request.form['odometer']
|
|
|
|
|
2015-03-06 21:58:53 +01:00
|
|
|
litres = request.form['litres']
|
2015-03-09 20:06:17 +01:00
|
|
|
try:
|
|
|
|
litres = float(litres)
|
|
|
|
except ValueError:
|
2016-04-14 07:32:06 +02:00
|
|
|
error_msg['litres'] = 'Illegal Value, only floating point allowed'
|
2015-03-09 20:06:17 +01:00
|
|
|
litres = None
|
|
|
|
if litres is not None and litres <= 0:
|
2016-04-14 07:32:06 +02:00
|
|
|
error_msg['litres'] = 'Litres must not be 0'
|
2015-03-09 20:06:17 +01:00
|
|
|
litres = request.form['litres']
|
|
|
|
if litres is None:
|
|
|
|
litres = request.form['litres']
|
|
|
|
|
2015-03-06 21:58:53 +01:00
|
|
|
# error checking here
|
2016-04-14 07:32:06 +02:00
|
|
|
if len(error_msg) > 0:
|
|
|
|
data = {'last': {'date': date, 'odometer': odometer, 'litres': litres}, 'error': error_msg}
|
2015-03-09 20:06:17 +01:00
|
|
|
return render_template('newPitStopForm.html', data=data)
|
2015-03-06 21:58:53 +01:00
|
|
|
|
2016-04-14 07:32:06 +02:00
|
|
|
g.db2.add_pit_stop(date, odometer, litres)
|
2015-03-06 21:58:53 +01:00
|
|
|
|
2015-03-16 22:06:59 +01:00
|
|
|
return redirect(url_for('get_pit_stops'))
|
2015-03-06 21:58:53 +01:00
|
|
|
|
2016-04-14 07:32:06 +02:00
|
|
|
|
2015-03-06 21:58:53 +01:00
|
|
|
@app.route('/pitstops/createForm', methods=['GET'])
|
2015-08-03 22:24:55 +02:00
|
|
|
@requires_auth
|
2015-03-16 22:06:59 +01:00
|
|
|
def create_pit_stop_form():
|
2016-04-14 07:32:06 +02:00
|
|
|
values = g.db2.get_last_pit_stop()
|
2015-03-12 08:02:17 +01:00
|
|
|
values['date'] = time.strftime("%Y-%m-%d")
|
2015-03-16 22:06:59 +01:00
|
|
|
g.data['last'] = values
|
|
|
|
g.data['error'] = None
|
|
|
|
return render_template('newPitStopForm.html', data=g.data)
|
|
|
|
|
2015-03-06 21:58:53 +01:00
|
|
|
|
|
|
|
@app.route('/pitstops', methods=['GET'])
|
2015-08-03 22:24:55 +02:00
|
|
|
@requires_auth
|
2015-03-16 22:06:59 +01:00
|
|
|
def get_pit_stops():
|
2016-04-14 07:32:06 +02:00
|
|
|
data = prepare_pit_stops(g.db2.get_all_pit_stops())
|
2015-03-16 22:06:59 +01:00
|
|
|
g.data['pitstops'] = data
|
|
|
|
return render_template('pitstops.html', data=g.data)
|
2015-03-06 21:58:53 +01:00
|
|
|
|
2016-04-14 07:32:06 +02:00
|
|
|
|
2015-03-17 20:57:38 +01:00
|
|
|
@app.route('/manual', methods=['GET'])
|
2015-08-03 22:24:55 +02:00
|
|
|
@requires_auth
|
2015-03-17 20:57:38 +01:00
|
|
|
def get_manual():
|
|
|
|
return render_template('manual.html', data=g.data)
|
|
|
|
|
2016-04-14 07:32:06 +02:00
|
|
|
|
2015-03-07 09:51:38 +01:00
|
|
|
@app.route('/statistics', methods=['GET'])
|
2015-08-03 22:24:55 +02:00
|
|
|
@requires_auth
|
2015-03-16 22:06:59 +01:00
|
|
|
def get_statistics():
|
2016-04-14 07:32:06 +02:00
|
|
|
pitstops = g.db2.get_all_pit_stops()
|
2015-03-07 09:51:38 +01:00
|
|
|
count = len(pitstops)
|
|
|
|
distance = 0
|
2016-04-14 07:32:06 +02:00
|
|
|
sum_litres = 0
|
|
|
|
average_distance = 0
|
|
|
|
average_litres_fuelled = 0
|
|
|
|
average_litres_used = 0
|
2015-03-09 20:06:17 +01:00
|
|
|
|
|
|
|
if count > 0:
|
2016-04-14 07:32:06 +02:00
|
|
|
sum_litres = 0
|
2015-03-07 09:51:38 +01:00
|
|
|
for pitstop in pitstops:
|
2016-04-14 07:32:06 +02:00
|
|
|
sum_litres += pitstop['litres']
|
|
|
|
average_litres_fuelled = sum_litres/count
|
2015-03-09 20:06:17 +01:00
|
|
|
if count > 1:
|
2015-03-07 09:51:38 +01:00
|
|
|
distance = pitstops[-1]['odometer'] - pitstops[0]['odometer']
|
2016-04-14 07:32:06 +02:00
|
|
|
average_distance = distance/(count - 1)
|
|
|
|
average_litres_used = 100 * (sum_litres-pitstops[0]['litres'])/distance
|
2015-03-16 22:06:59 +01:00
|
|
|
g.data['distance'] = distance
|
|
|
|
g.data['count'] = count
|
2016-04-14 07:32:06 +02:00
|
|
|
g.data['litres'] = sum_litres
|
|
|
|
g.data['averageDistance'] = average_distance
|
|
|
|
g.data['averageListresFuelled'] = average_litres_fuelled
|
|
|
|
g.data['averageListresUsed'] = average_litres_used
|
2015-03-16 22:06:59 +01:00
|
|
|
return render_template('statistics.html', data=g.data)
|
|
|
|
|
2016-04-14 07:32:06 +02:00
|
|
|
|
2015-03-16 22:06:59 +01:00
|
|
|
def prepare_pit_stops(pitstops):
|
2015-03-06 21:58:53 +01:00
|
|
|
for index in range(1, len(pitstops)):
|
|
|
|
last = pitstops[index - 1]
|
|
|
|
curr = pitstops[index]
|
|
|
|
curr['distance'] = curr['odometer'] - last['odometer']
|
2015-03-16 22:06:59 +01:00
|
|
|
curr['average'] = 100 * curr['litres']/curr['distance']
|
2015-03-06 21:58:53 +01:00
|
|
|
last_date = datetime.strptime(last['date'], '%Y-%m-%d')
|
|
|
|
curr_date = datetime.strptime(curr['date'], '%Y-%m-%d')
|
|
|
|
curr['days'] = (curr_date - last_date).days
|
2015-03-16 22:06:59 +01:00
|
|
|
pitstops.reverse()
|
2015-03-06 21:58:53 +01:00
|
|
|
return pitstops
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2015-03-07 09:51:38 +01:00
|
|
|
if not os.path.isfile(DATABASE) or os.stat(DATABASE).st_size == 0:
|
2015-03-16 22:06:59 +01:00
|
|
|
db = db.Db(app.config['DATABASE'])
|
|
|
|
db.init_db(app.open_resource('schema.sql', mode='r'))
|
2015-08-03 22:24:55 +02:00
|
|
|
app.run(debug=True, host='0.0.0.0')
|