commit af7ef156fd4c46c8d300ef94cba149a9e7f7c575 Author: joachim lusiardi Date: Tue Mar 3 22:49:43 2015 +0100 initial commit diff --git a/main.py b/main.py new file mode 100644 index 0000000..a71c7d7 --- /dev/null +++ b/main.py @@ -0,0 +1,86 @@ +import sqlite3 +import re +from flask import Flask +from flask import render_template +from flask import url_for +from flask import request, redirect, g +import datetime + +app = Flask(__name__) +DATABASE = '/tmp/rollerverbrauch.db' +DEBUG = True +SECRET_KEY = 'development key' +USERNAME = 'admin' +PASSWORD = 'default' +app.config.from_object(__name__) + +def connect_db(): + result = sqlite3.connect(app.config['DATABASE']) + return result + +@app.before_request +def before_request(): + g.db = connect_db() + +@app.teardown_request +def teardown_request(exception): + db = getattr(g, 'db', None) + if db is not None: + db.close() + +@app.route('/') +def index(): + #data = {'pitstopsUrl': url_for('getPitStops')} + #return render_template('index.html', data=data) + return redirect(url_for('getPitStops')) + +@app.route('/pitstops', methods=['POST']) +def createPitStop(): + date = request.form['date'] + odometer = request.form['odometer'] + litres = request.form['litres'] + + # error checking here + + addPitStop(date, odometer, litres) + + return redirect(url_for('getPitStops')) + +@app.route('/pitstops/createForm', methods=['GET']) +def createPitStopForm(): + data = {'last':getLastPitStop(), 'error': None} + return render_template('newPitStopForm.html', data=data) + +@app.route('/pitstops', methods=['GET']) +def getPitStops(): + data = {'pitstops': preparePitStops(getAllPitStops())} + return render_template('pitstops.html', data=data) + +def preparePitStops(pitstops): + for index in range(1, len(pitstops)): + last = pitstops[index - 1] + curr = pitstops[index] + curr['distance'] = curr['odometer'] - last['odometer'] + curr['average'] = round(100 * curr['litres']/curr['distance'], 2) + return pitstops + +def getLastPitStop(): + cur = g.db.execute('select date, odometer, litres from pitstops order by date desc limit 1') + pitstops = [dict(date=row[0], odometer=row[1], litres=row[2]) for row in cur.fetchall()] + if len(pitstops) == 0: + return {'date': datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d'), 'odometer': 0, 'litres': 0} + return pitstops[0] + +def getAllPitStops(): + cur = g.db.execute('select date, odometer, litres from pitstops order by id asc') + pitstops = [dict(date=row[0], odometer=row[1], litres=row[2]) for row in cur.fetchall()] + return pitstops + +def addPitStop(date, odometer, litres): + g.db.execute('insert into pitstops (date, odometer, litres) values (?, ?, ?)', [date, odometer, litres]) + g.db.commit() + + + +if __name__ == '__main__': + app.run(debug=True) \ No newline at end of file diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..4baa685 --- /dev/null +++ b/schema.sql @@ -0,0 +1,7 @@ +drop table if exists pitstops; +create table pitstops ( + `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + `date` TEXT NOT NULL, + `odometer` INTEGER NOT NULL, + `litres` REAL NOT NULL +); \ No newline at end of file diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..941da9d --- /dev/null +++ b/static/style.css @@ -0,0 +1,7 @@ +.right-aligned { + text-align: right; +} + +.error { + color: red +} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..d5f8750 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,5 @@ +{% extends "layout.html" %} +{% block body %} + Pitstop List + Create Pitstop +{% endblock %} \ No newline at end of file diff --git a/templates/layout.html b/templates/layout.html new file mode 100644 index 0000000..e7c983a --- /dev/null +++ b/templates/layout.html @@ -0,0 +1,12 @@ + + + + Rollerverbrauch + + + +

Rollerverbrauch

+ {% block body %} + {% endblock %} + + \ No newline at end of file diff --git a/templates/newPitStopForm.html b/templates/newPitStopForm.html new file mode 100644 index 0000000..0e479c1 --- /dev/null +++ b/templates/newPitStopForm.html @@ -0,0 +1,19 @@ +{% extends "layout.html" %} +{% block body %} + {% if data.error %} +
+

Error: {{ data.error }}

+
+ {% endif %} +
+ + +
+ + + + + +
+ +{% endblock %} \ No newline at end of file diff --git a/templates/pitstops.html b/templates/pitstops.html new file mode 100644 index 0000000..f66c3d1 --- /dev/null +++ b/templates/pitstops.html @@ -0,0 +1,44 @@ +{% extends "layout.html" %} +{% block body %} + +Create Pitstop + + + + + + + + + {% for pitstop in data['pitstops'] %} + + + + + + + + {% endfor %} +
+ Date + + Odometer + + Distance + + Litres + + Average +
+ {{pitstop.date}} + + {{pitstop.odometer}} km + + {% if pitstop.distance %}{{pitstop.distance}}{% else %} --{% endif %} km + + {{pitstop.litres}} l + + {% if pitstop.average %}{{pitstop.average}}{% else %} --{% endif %} l/100km +