from datetime import datetime from flask import Flask from flask import render_template from flask import request, redirect, g from flask import url_for import os.path import time import db #from db import Db app = Flask(__name__) DATABASE = '/data/rollerverbrauch.db' DEBUG = True SECRET_KEY = 'development key' USERNAME = 'admin' PASSWORD = 'default' app.config.from_object(__name__) @app.before_request def before_request(): g.db2 = db.Db(app.config['DATABASE']) g.data = {} add_service_warning(g.data) @app.teardown_request def teardown_request(exception): pass @app.route('/') def index(): 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) @app.route('/pitstops', methods=['POST']) def create_pit_stop(): last_pitstop = g.db2.getLastPitStop() errorMsg = {} date = request.form['date'] 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'] odometer = request.form['odometer'] 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'] litres = request.form['litres'] 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'] # error checking here if len(errorMsg) > 0: data = {'last': {'date': date, 'odometer': odometer, 'litres': litres}, 'error': errorMsg} return render_template('newPitStopForm.html', data=data) g.db2.addPitStop(date, odometer, litres) return redirect(url_for('get_pit_stops')) @app.route('/pitstops/createForm', methods=['GET']) def create_pit_stop_form(): values = g.db2.getLastPitStop() values['date'] = time.strftime("%Y-%m-%d") 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 @app.route('/pitstops', methods=['GET']) def get_pit_stops(): data = prepare_pit_stops(g.db2.getAllPitStops()) g.data['pitstops'] = data return render_template('pitstops.html', data=g.data) @app.route('/manual', methods=['GET']) def get_manual(): return render_template('manual.html', data=g.data) @app.route('/statistics', methods=['GET']) def get_statistics(): pitstops = g.db2.getAllPitStops() count = len(pitstops) distance = 0 sumLitres = 0 averageDistance = 0 averageLitresFuelled = 0 averageLitresUsed = 0 if count > 0: sumLitres = 0 for pitstop in pitstops: sumLitres += pitstop['litres'] averageLitresFuelled = sumLitres/count if count > 1: distance = pitstops[-1]['odometer'] - pitstops[0]['odometer'] 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): for index in range(1, len(pitstops)): last = pitstops[index - 1] curr = pitstops[index] curr['distance'] = curr['odometer'] - last['odometer'] curr['average'] = 100 * curr['litres']/curr['distance'] 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 pitstops.reverse() return pitstops if __name__ == '__main__': if not os.path.isfile(DATABASE) or os.stat(DATABASE).st_size == 0: db = db.Db(app.config['DATABASE']) db.init_db(app.open_resource('schema.sql', mode='r')) app.run(debug=True, host='0.0.0.0')