2016-04-16 15:48:24 +02:00
|
|
|
from datetime import date
|
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
|
2016-04-16 15:48:24 +02:00
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
import uuid
|
2015-08-03 22:24:55 +02:00
|
|
|
import hashlib
|
|
|
|
from functools import wraps
|
2016-04-17 12:50:31 +02:00
|
|
|
from flask_wtf import Form
|
|
|
|
from wtforms import DateField, IntegerField, DecimalField
|
2016-04-18 07:25:40 +02:00
|
|
|
from wtforms.validators import DataRequired, ValidationError
|
2016-04-17 12:50:31 +02:00
|
|
|
|
2015-03-06 21:58:53 +01:00
|
|
|
app = Flask(__name__)
|
2015-03-09 20:11:03 +01:00
|
|
|
DATABASE = '/data/rollerverbrauch.db'
|
2016-04-16 15:48:24 +02:00
|
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+DATABASE
|
|
|
|
sqldb = SQLAlchemy(app)
|
|
|
|
|
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
|
|
|
|
2016-04-16 15:48:24 +02:00
|
|
|
class User(sqldb.Model):
|
|
|
|
id = sqldb.Column(sqldb.Integer, primary_key=True)
|
|
|
|
username = sqldb.Column(sqldb.String(80), unique=True)
|
|
|
|
email = sqldb.Column(sqldb.String(120), unique=True)
|
|
|
|
salt = sqldb.Column(sqldb.String(8))
|
|
|
|
password_hash = sqldb.Column(sqldb.String(64))
|
|
|
|
|
|
|
|
def __init__(self, username, email, password):
|
|
|
|
self.username = username
|
|
|
|
self.email = email
|
|
|
|
self.salt = uuid.uuid4().hex
|
|
|
|
m = hashlib.sha256(password.encode('utf-8'))
|
|
|
|
m = hashlib.sha256((m.hexdigest() + self.salt).encode('utf-8')).hexdigest()
|
|
|
|
self.password_hash = m
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<User %r>' % self.username
|
|
|
|
|
|
|
|
|
|
|
|
class Pitstop(sqldb.Model):
|
|
|
|
id = sqldb.Column(sqldb.Integer, primary_key=True)
|
|
|
|
date = sqldb.Column(sqldb.Date)
|
|
|
|
odometer = sqldb.Column(sqldb.Integer)
|
|
|
|
litres = sqldb.Column(sqldb.Numeric(5,2))
|
|
|
|
|
|
|
|
def __init__(self, odometer, litres, date):
|
|
|
|
self.odometer = odometer
|
|
|
|
self.litres = litres
|
|
|
|
self.date = date
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<Pitstop %r km, %r l>' % (self.odometer, self.litres)
|
|
|
|
|
|
|
|
sqldb.create_all()
|
|
|
|
if User.query.filter_by(username='jlusiardi').first() is None:
|
|
|
|
user1 = User('jlusiardi', 'joachim@lusiardi.de', 'pitstops')
|
|
|
|
sqldb.session.add(user1)
|
|
|
|
sqldb.session.commit()
|
|
|
|
|
|
|
|
|
2015-08-03 22:24:55 +02:00
|
|
|
def check_auth(username, password):
|
2016-04-16 15:48:24 +02:00
|
|
|
user = User.query.filter_by(username=username).first()
|
|
|
|
if user is None:
|
2015-08-03 22:24:55 +02:00
|
|
|
return False
|
2016-04-16 15:48:24 +02:00
|
|
|
salt = user.salt
|
2015-08-03 22:24:55 +02:00
|
|
|
m = hashlib.sha256(password.encode('utf-8'))
|
|
|
|
m = hashlib.sha256((m.hexdigest()+salt).encode('utf-8'))
|
|
|
|
digest = m.hexdigest()
|
2016-04-16 15:48:24 +02:00
|
|
|
ok = (User.query.filter_by(username=username, password_hash=digest).first() is not None)
|
2015-08-03 22:24:55 +02:00
|
|
|
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.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'))
|
|
|
|
|
2016-04-18 07:25:40 +02:00
|
|
|
|
|
|
|
def date_check(form, field):
|
|
|
|
if field.data < form.pitstop.date:
|
|
|
|
raise ValidationError('The new date must after %s' % form.pitstop.date)
|
|
|
|
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
|
2016-04-17 12:50:31 +02:00
|
|
|
class CreatePitstopForm(Form):
|
2016-04-18 07:25:40 +02:00
|
|
|
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
|
2016-04-17 12:50:31 +02:00
|
|
|
|
2015-03-06 21:58:53 +01:00
|
|
|
|
2016-04-17 12:50:31 +02:00
|
|
|
@app.route('/pitstops/createForm', methods=['GET', 'POST'])
|
2015-08-03 22:24:55 +02:00
|
|
|
@requires_auth
|
2016-04-17 12:50:31 +02:00
|
|
|
def create_pit_stop_form():
|
2016-04-18 07:25:40 +02:00
|
|
|
last_pitstop = Pitstop.query.order_by(Pitstop.id.desc()).first()
|
|
|
|
if last_pitstop is None:
|
|
|
|
last_pitstop = Pitstop(0, 0, datetime.now())
|
|
|
|
|
2016-04-17 12:50:31 +02:00
|
|
|
form = CreatePitstopForm()
|
2016-04-18 07:25:40 +02:00
|
|
|
form.set_pitstop(last_pitstop)
|
2016-04-17 12:50:31 +02:00
|
|
|
if form.validate_on_submit():
|
2016-04-18 07:25:40 +02:00
|
|
|
|
2016-04-17 12:50:31 +02:00
|
|
|
new_stop = Pitstop(form.odometer.data, form.litres.data, form.date.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)
|
2015-03-16 22:06:59 +01:00
|
|
|
|
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-16 15:48:24 +02:00
|
|
|
data = prepare_pit_stops(Pitstop.query.all())
|
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-16 15:48:24 +02:00
|
|
|
pitstops = Pitstop.query.all()
|
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
|
2016-04-17 12:50:31 +02:00
|
|
|
|
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-16 15:48:24 +02:00
|
|
|
sum_litres += pitstop.litres
|
2016-04-14 07:32:06 +02:00
|
|
|
average_litres_fuelled = sum_litres/count
|
2015-03-09 20:06:17 +01:00
|
|
|
if count > 1:
|
2016-04-16 15:48:24 +02:00
|
|
|
distance = pitstops[-1].odometer - pitstops[0].odometer
|
2016-04-14 07:32:06 +02:00
|
|
|
average_distance = distance/(count - 1)
|
2016-04-16 15:48:24 +02:00
|
|
|
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
|
|
|
|
2016-04-16 15:48:24 +02:00
|
|
|
def prepare_pit_stops(pss):
|
|
|
|
pitstops = []
|
|
|
|
for pitstop_index in range(0, len(pss)):
|
|
|
|
p = dict()
|
|
|
|
p['odometer'] = pss[pitstop_index].odometer
|
|
|
|
p['litres'] = pss[pitstop_index].litres
|
|
|
|
p['date'] = pss[pitstop_index].date
|
|
|
|
pitstops.append(p)
|
|
|
|
for pitstop_index in range(1, len(pitstops)):
|
|
|
|
last = pitstops[pitstop_index - 1]
|
|
|
|
curr = pitstops[pitstop_index]
|
2015-03-06 21:58:53 +01:00
|
|
|
curr['distance'] = curr['odometer'] - last['odometer']
|
2015-03-16 22:06:59 +01:00
|
|
|
curr['average'] = 100 * curr['litres']/curr['distance']
|
2016-04-16 15:48:24 +02:00
|
|
|
last_date = last['date']
|
|
|
|
curr_date = curr['date']
|
2015-03-06 21:58:53 +01:00
|
|
|
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-08-03 22:24:55 +02:00
|
|
|
app.run(debug=True, host='0.0.0.0')
|