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
|
2016-04-17 08:59:11 +02:00
|
|
|
from flask.ext.security import Security, SQLAlchemyUserDatastore, \
|
|
|
|
UserMixin, RoleMixin, login_required, utils
|
2016-04-19 06:47:10 +02:00
|
|
|
from flask.ext.security.core import current_user
|
2016-04-16 15:48:24 +02:00
|
|
|
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
|
2016-04-19 06:47:10 +02:00
|
|
|
from wtforms import DateField, IntegerField, DecimalField, SelectField
|
2016-04-18 07:25:40 +02:00
|
|
|
from wtforms.validators import DataRequired, ValidationError
|
2016-04-18 08:13:27 +02:00
|
|
|
import os
|
2015-08-03 22:24:55 +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
|
2016-04-17 08:59:11 +02:00
|
|
|
db = SQLAlchemy(app)
|
2016-04-16 15:48:24 +02:00
|
|
|
|
2016-04-17 08:59:11 +02:00
|
|
|
app.config['SECRET_KEY'] = 'development key'
|
|
|
|
app.config['SECURITY_PASSWORD_HASH'] = 'pbkdf2_sha512'
|
|
|
|
app.config['SECURITY_PASSWORD_SALT'] = 'xxxxxxxxxxxxxxxxxxxxxx'
|
2015-03-06 21:58:53 +01:00
|
|
|
app.config.from_object(__name__)
|
|
|
|
|
2016-04-14 07:32:06 +02:00
|
|
|
|
2016-04-17 08:59:11 +02:00
|
|
|
roles_users = db.Table('roles_users',
|
2016-04-19 06:47:10 +02:00
|
|
|
db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
|
|
|
|
db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))
|
2016-04-16 15:48:24 +02:00
|
|
|
|
2016-04-17 08:59:11 +02:00
|
|
|
|
|
|
|
class Role(db.Model, RoleMixin):
|
|
|
|
id = db.Column(db.Integer(), primary_key=True)
|
|
|
|
name = db.Column(db.String(80), unique=True)
|
|
|
|
description = db.Column(db.String(255))
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.name)
|
|
|
|
|
|
|
|
|
|
|
|
class User(db.Model, UserMixin):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
email = db.Column(db.String(255), unique=True)
|
|
|
|
password = db.Column(db.String(255))
|
|
|
|
active = db.Column(db.Boolean())
|
|
|
|
confirmed_at = db.Column(db.DateTime())
|
2016-04-19 06:47:10 +02:00
|
|
|
vehicles = db.relationship(
|
|
|
|
'Vehicle'
|
|
|
|
)
|
2016-04-17 08:59:11 +02:00
|
|
|
roles = db.relationship(
|
|
|
|
'Role',
|
|
|
|
secondary=roles_users,
|
|
|
|
backref=db.backref('users', lazy='dynamic')
|
|
|
|
)
|
2016-04-16 15:48:24 +02:00
|
|
|
|
|
|
|
def __repr__(self):
|
2016-04-19 06:47:10 +02:00
|
|
|
return '<User %r>' % self.email
|
|
|
|
|
|
|
|
|
|
|
|
class Vehicle(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
owner_id = db.Column(db.Integer, db.ForeignKey('user.id'))
|
|
|
|
name = db.Column(db.String(255), unique=True)
|
|
|
|
pitstops = db.relationship(
|
|
|
|
'Pitstop'
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<Vehicle %r>' % self.name
|
2016-04-16 15:48:24 +02:00
|
|
|
|
|
|
|
|
2016-04-17 08:59:11 +02:00
|
|
|
class Pitstop(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
date = db.Column(db.Date)
|
|
|
|
odometer = db.Column(db.Integer)
|
|
|
|
litres = db.Column(db.Numeric(5,2))
|
2016-04-19 06:47:10 +02:00
|
|
|
vehicle_id = db.Column(db.Integer, db.ForeignKey('vehicle.id'))
|
2016-04-16 15:48:24 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2016-04-17 08:59:11 +02:00
|
|
|
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
|
|
|
|
security = Security(app, user_datastore)
|
2016-04-16 15:48:24 +02:00
|
|
|
|
2016-04-17 08:59:11 +02:00
|
|
|
@app.before_first_request
|
|
|
|
def before_first_request():
|
|
|
|
db.create_all()
|
2015-08-03 22:24:55 +02:00
|
|
|
|
2016-04-17 08:59:11 +02:00
|
|
|
user_datastore.find_or_create_role(name='admin', description='Administrator')
|
|
|
|
user_datastore.find_or_create_role(name='end-user', description='End user')
|
2016-04-14 07:32:06 +02:00
|
|
|
|
2016-04-17 08:59:11 +02:00
|
|
|
encrypted_password = utils.encrypt_password('password')
|
|
|
|
if not user_datastore.get_user('someone@example.com'):
|
|
|
|
user_datastore.create_user(email='someone@example.com', password=encrypted_password)
|
|
|
|
if not user_datastore.get_user('admin@example.com'):
|
|
|
|
user_datastore.create_user(email='admin@example.com', password=encrypted_password)
|
2015-08-03 22:24:55 +02:00
|
|
|
|
2016-04-17 08:59:11 +02:00
|
|
|
# Commit any database changes; the User and Roles must exist before we can add a Role to the User
|
|
|
|
db.session.commit()
|
2016-04-14 07:32:06 +02:00
|
|
|
|
2016-04-17 08:59:11 +02:00
|
|
|
# Give one User has the "end-user" role, while the other has the "admin" role. (This will have no effect if the
|
|
|
|
# Users already have these Roles.) Again, commit any database changes.
|
|
|
|
user_datastore.add_role_to_user('someone@example.com', 'end-user')
|
|
|
|
user_datastore.add_role_to_user('admin@example.com', 'admin')
|
|
|
|
db.session.commit()
|
2015-08-03 22:24:55 +02:00
|
|
|
|
2016-04-19 06:47:10 +02:00
|
|
|
user = User.query.filter(User.email=='someone@example.com').first()
|
|
|
|
if len(user.vehicles) == 0:
|
|
|
|
v = Vehicle('someone')
|
|
|
|
db.session.add(v)
|
|
|
|
user.vehicles.append(v)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
user = User.query.filter(User.email=='admin@example.com').first()
|
|
|
|
if len(user.vehicles) == 0:
|
|
|
|
v = Vehicle('admin_1')
|
|
|
|
db.session.add(v)
|
|
|
|
user.vehicles.append(v)
|
|
|
|
v = Vehicle('admin_2')
|
|
|
|
db.session.add(v)
|
|
|
|
user.vehicles.append(v)
|
|
|
|
db.session.commit()
|
|
|
|
|
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.route('/')
|
2016-04-17 08:59:11 +02:00
|
|
|
@login_required
|
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
|
|
|
|
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)
|
2016-04-17 08:59:11 +02:00
|
|
|
|
2016-04-18 07:25:40 +02:00
|
|
|
|
|
|
|
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')
|
2015-03-06 21:58:53 +01:00
|
|
|
|
2016-04-14 07:32:06 +02:00
|
|
|
|
2016-04-17 12:50:31 +02:00
|
|
|
class CreatePitstopForm(Form):
|
2016-04-19 06:47:10 +02:00
|
|
|
vehicle = SelectField('Vehicle', coerce=int)
|
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'])
|
2016-04-17 08:59:11 +02:00
|
|
|
@login_required
|
2015-03-16 22:06:59 +01: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:
|
2016-04-18 20:58:20 +02:00
|
|
|
last_pitstop = Pitstop(0, 0, date.today())
|
2016-04-18 07:25:40 +02:00
|
|
|
|
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-19 06:47:10 +02:00
|
|
|
print(current_user.vehicles)
|
|
|
|
|
|
|
|
form.vehicle.choices = [(g.id, g.name) for g in current_user.vehicles]
|
2016-04-17 12:50:31 +02:00
|
|
|
if form.validate_on_submit():
|
2016-04-19 06:47:10 +02:00
|
|
|
print(form.vehicle.data)
|
|
|
|
v = Vehicle.query.filter(Vehicle.id==form.vehicle.data).first()
|
2016-04-17 12:50:31 +02:00
|
|
|
new_stop = Pitstop(form.odometer.data, form.litres.data, form.date.data)
|
2016-04-18 21:43:01 +02:00
|
|
|
db.session.add(new_stop)
|
2016-04-19 06:47:10 +02:00
|
|
|
v.pitstops.append(new_stop)
|
2016-04-18 21:43:01 +02:00
|
|
|
db.session.commit()
|
2016-04-17 12:50:31 +02:00
|
|
|
return redirect(url_for('get_pit_stops'))
|
|
|
|
|
|
|
|
# dynamically set values
|
|
|
|
form.odometer.default = last_pitstop.odometer
|
|
|
|
form.litres.default = last_pitstop.litres
|
2016-04-18 20:58:20 +02:00
|
|
|
form.date.default = date.today()
|
2016-04-17 12:50:31 +02:00
|
|
|
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'])
|
2016-04-17 08:59:11 +02:00
|
|
|
@login_required
|
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'])
|
2016-04-17 08:59:11 +02:00
|
|
|
@login_required
|
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'])
|
2016-04-17 08:59:11 +02:00
|
|
|
@login_required
|
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 08:59:11 +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__':
|
2016-04-18 08:13:27 +02:00
|
|
|
DEBUG = 'DEBUG' in os.environ and os.environ['DEBUG'] != 'False'
|
|
|
|
app.run(debug=DEBUG, host='0.0.0.0')
|