initial commit

This commit is contained in:
joachim lusiardi 2015-03-03 22:49:43 +01:00
commit af7ef156fd
7 changed files with 180 additions and 0 deletions

86
main.py Normal file
View File

@ -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)

7
schema.sql Normal file
View File

@ -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
);

7
static/style.css Normal file
View File

@ -0,0 +1,7 @@
.right-aligned {
text-align: right;
}
.error {
color: red
}

5
templates/index.html Normal file
View File

@ -0,0 +1,5 @@
{% extends "layout.html" %}
{% block body %}
<a href='{{ url_for('getPitStops') }}'>Pitstop List</a>
<a href='{{ url_for('createPitStopForm') }}'>Create Pitstop</a>
{% endblock %}

12
templates/layout.html Normal file
View File

@ -0,0 +1,12 @@
<!doctype html>
<html>
<head>
<title>Rollerverbrauch</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<h1>Rollerverbrauch</h1>
{% block body %}
{% endblock %}
</body>
</html>

View File

@ -0,0 +1,19 @@
{% extends "layout.html" %}
{% block body %}
{% if data.error %}
<div>
<p class='error'><strong>Error:</strong> {{ data.error }}</p>
</div>
{% endif %}
<form action="{{ url_for('createPitStop') }}" method='post'>
<label for='date'>Date of Pitstop</label>
<input type='date' name='date' id='date' value='{{ data.last.date }}'/>
<br />
<label for='odometer'>Odometer</label>
<input type='text' name='odometer' id='odometer' value='{{ data.last.odometer }}'/>
<label for='litres'>Litres fuelled</label>
<input type='text' name='litres' id='litres' value='{{ data.last.litres }}'/>
<input type='submit' value='Log pit stop'>
</form>
{% endblock %}

44
templates/pitstops.html Normal file
View File

@ -0,0 +1,44 @@
{% extends "layout.html" %}
{% block body %}
<a href='{{ url_for('createPitStopForm') }}'>Create Pitstop</a>
<table>
<tr>
<th>
Date
</th>
<th>
Odometer
</th>
<th>
Distance
</th>
<th>
Litres
</th>
<th>
Average
</th>
</tr>
{% for pitstop in data['pitstops'] %}
<tr>
<td>
{{pitstop.date}}
</td>
<td class='right-aligned'>
{{pitstop.odometer}} km
</td>
<td class='right-aligned'>
{% if pitstop.distance %}{{pitstop.distance}}{% else %} --{% endif %} km
</td>
<td class='right-aligned'>
{{pitstop.litres}} l
</td>
<td class='right-aligned'>
{% if pitstop.average %}{{pitstop.average}}{% else %} --{% endif %} l/100km
</td>
</tr>
{% endfor %}
</table
{% endblock %}