rollerverbrauch/app/main.py

148 lines
4.6 KiB
Python
Raw Normal View History

2015-03-06 21:58:53 +01:00
from datetime import datetime
from flask import Flask
from flask import render_template
from flask import request, redirect, g
2015-03-16 22:06:59 +01:00
from flask import url_for
import os.path
2015-03-12 08:02:17 +01:00
import time
2015-03-06 21:58:53 +01:00
2015-03-16 22:06:59 +01:00
import db
#from db 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'
USERNAME = 'admin'
PASSWORD = 'default'
app.config.from_object(__name__)
@app.before_request
def before_request():
2015-03-16 22:06:59 +01:00
g.db2 = db.Db(app.config['DATABASE'])
g.data = {}
add_service_warning(g.data)
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
@app.route('/')
def index():
2015-03-16 22:06:59 +01:00
return redirect(url_for('get_pit_stops'))
@app.route('/services')
def get_services():
data = g.db2.getAllServices()
data.reverse()
g.data['services'] = data
return render_template('services.html', data=g.data)
2015-03-06 21:58:53 +01:00
@app.route('/pitstops', methods=['POST'])
2015-03-16 22:06:59 +01:00
def create_pit_stop():
last_pitstop = g.db2.getLastPitStop()
2015-03-09 20:06:17 +01:00
errorMsg = {}
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:
errorMsg['date'] = 'invalid date, only YYYY-MM-DD is allowed'
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:
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']
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:
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']
2015-03-06 21:58:53 +01:00
# error checking here
2015-03-09 20:06:17 +01:00
if len(errorMsg) > 0:
data = {'last': {'date': date, 'odometer': odometer, 'litres': litres}, 'error': errorMsg}
return render_template('newPitStopForm.html', data=data)
2015-03-06 21:58:53 +01:00
2015-03-16 22:06:59 +01:00
g.db2.addPitStop(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
@app.route('/pitstops/createForm', methods=['GET'])
2015-03-16 22:06:59 +01:00
def create_pit_stop_form():
values = g.db2.getLastPitStop()
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)
def add_service_warning(data):
service_info = g.db2.get_service_warning_info()
data['service_info'] = service_info
2015-03-06 21:58:53 +01:00
@app.route('/pitstops', methods=['GET'])
2015-03-16 22:06:59 +01:00
def get_pit_stops():
data = prepare_pit_stops(g.db2.getAllPitStops())
g.data['pitstops'] = data
return render_template('pitstops.html', data=g.data)
2015-03-06 21:58:53 +01:00
2015-03-07 09:51:38 +01:00
@app.route('/statistics', methods=['GET'])
2015-03-16 22:06:59 +01:00
def get_statistics():
pitstops = g.db2.getAllPitStops()
2015-03-07 09:51:38 +01:00
count = len(pitstops)
distance = 0
sumLitres = 0
averageDistance = 0
averageLitresFuelled = 0
averageLitresUsed = 0
2015-03-09 20:06:17 +01:00
if count > 0:
2015-03-07 09:51:38 +01:00
sumLitres = 0
for pitstop in pitstops:
sumLitres += pitstop['litres']
2015-03-16 22:06:59 +01:00
averageLitresFuelled = sumLitres/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']
2015-03-16 22:06:59 +01:00
averageDistance = distance/(count - 1)
averageLitresUsed = 100 * (sumLitres-pitstops[0]['litres'])/distance
g.data['distance'] = distance
g.data['count'] = count
g.data['litres'] = sumLitres
g.data['averageDistance'] = averageDistance
g.data['averageListresFuelled'] = averageLitresFuelled
g.data['averageListresUsed'] = averageLitresUsed
return render_template('statistics.html', data=g.data)
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-03-06 21:58:53 +01:00
app.run(debug=True, host='0.0.0.0')