From 73b2ccc195ee62fcb07359cf338443b8706bc18c Mon Sep 17 00:00:00 2001 From: joachim lusiardi Date: Mon, 16 Mar 2015 22:06:59 +0100 Subject: [PATCH] started working on services --- app/db/__init__.py | 69 ++++++++++++ app/db/__pycache__/__init__.cpython-34.pyc | Bin 0 -> 3102 bytes app/main.py | 122 +++++++++------------ app/schema.sql | 8 ++ app/templates/layout.html | 21 +++- app/templates/newPitStopForm.html | 13 ++- app/templates/pitstops.html | 8 +- app/templates/services.html | 60 ++++++++++ app/templates/statistics.html | 16 +-- 9 files changed, 231 insertions(+), 86 deletions(-) create mode 100644 app/db/__init__.py create mode 100644 app/db/__pycache__/__init__.cpython-34.pyc create mode 100644 app/templates/services.html diff --git a/app/db/__init__.py b/app/db/__init__.py new file mode 100644 index 0000000..d0bd3fa --- /dev/null +++ b/app/db/__init__.py @@ -0,0 +1,69 @@ +''' + + +@author: shing19m +''' + +from datetime import datetime +import sqlite3 + + +class Db(object): + ''' + classdocs + ''' + + + def __init__(self, db_file): + ''' + Constructor + ''' + self.db = sqlite3.connect(db_file) + + def __del__(self): + self.db.close() + + def getAllPitStops(self): + return self._perform_query('select * from pitstops order by id asc') + + def getAllServices(self): + return self._perform_query('select * from services') + + def getLastPitStop(self): + pitstops = self._perform_query('select * from pitstops order by id desc limit 1') + if len(pitstops) == 0: + return {'date': datetime.strftime(datetime.now(), '%Y-%m-%d'), 'odometer': 0, 'litres': 0} + return pitstops[0] + + def get_service_warning_info(self): + info = self._perform_query('select (odometer_planned - (select odometer from pitstops order by id desc limit 1)) km_left, tasks from services where date is null order by odometer_planned asc limit 1;') + if len(info) == 0: + return None + return info[0] + + def get_next_undone_service(self): + services = self._perform_query('select * from services where date is null limit 1') + if len(services) == 0: + return None + return services[0] + + def _perform_query(self, query): + cursor = self.db.execute(query) + names = list(map(lambda x: x[0], cursor.description)) + result = [] + for row in cursor.fetchall(): + row_result = {} + for index in range(0, len(names)): + row_result[names[index]] = row[index] + result.append(row_result) + return result + + def addPitStop(self, date, odometer, litres): + self.db.execute('insert into pitstops (date, odometer, litres) values (?, ?, ?)', [date, odometer, litres]) + self.db.commit() + + def init_db(self, resource): + with resource as f: + sql_commands = f.read() + self.db.cursor().executescript(sql_commands) + self.db.commit() diff --git a/app/db/__pycache__/__init__.cpython-34.pyc b/app/db/__pycache__/__init__.cpython-34.pyc new file mode 100644 index 0000000000000000000000000000000000000000..12fb5009e2daae63509e302634e7b5f3dfae1b2b GIT binary patch literal 3102 zcmb_e-EZ4e6hHomlcs66Zmj!alO@z?-nvC&XcDZHjZNZ#DFOnFMM9S2d(*me?DX1M zyQqB{&+x|ofcTI6$`ddA0X*?L*KyjEGK7TNdvbiw@xAAFzV7{_wbZ=+W$l~I2GMVH z?6JW=!pJs21W^yvAYoE$(9ocsK?c8D6r0pD=`&%IsL>D5s*$wljYU1oSk(2L$%SUK z`6w9Y+o}4<&9)waM=I>aTJfmocAeVVGl%0@gs>cNL2qXIuM8W{<25PbyJ&P^D z>e!FBsrK&GNH`?wdLB%J#DeDQB$^}^HIK;_i6zZ1kXR;hM)OcOOJYUyizLpGIInpq zzeD1J=9fsU0@B68eA=I!Z@PHGILI=QhFST>X7j_pJf2`=s~|bijzI?oy#XciHIS}n zPOj+>d8sF9lI3a~=Bb*^bv&!6XS;Ef%lkz=Op`>0d0~paXBKuQ<3X=3`u-q_RYM$Dj0{`?O-%&i+v3D0|I{dmxl((+5okqF}{HSq3U`@$1{}!QBe!yG?Sj8>Uh&;mSlm2 zb>9~<_Wg@^DYrBxO}(X_p4)Ne(j_RDJ0c(8Qh2g6xdDg3J?<@cpwgi`it;Q^N12-{ zA(h*I?MA{4vJgqP?2n`xq-yBzj-`7250))&$^3B~e-Y(fUgyd&16N?+y!1cVxjJuW zCe^Dblo_mGSZ_C`J5|1Rsxg&xW(+&b#?WE<8q9I09U7EZIf;ULX_9(Ikwv*lJ0-OV z(L|xWLAu)XfUB-n{y6lYi)_Zs<*rxnOxp99PW8QzS?I>mFv{I~ij!y3SbK42ZFpx* z6gIbkqLB(DZ!T5gAmtT&Qqw*utXL+B!4wVD$bkDrVI}DvGK_PoXjB`X%r1`Mvmnc- zL3E*4Bd=JU$*)td+^lfkqwwhUiYN!J8 zP)7v*OYo~d-PGR;RD!mSMlwimK<}7`ZDVq^td&!>|3_cn zo6o?vIplZ_o`$$leS8x2MMj;-{oEfXB28q4Y5i}YuS`Loq8xHg@xw0V{-}%dXE3uK zTAmY!CYNH4(@qx}PP+qCCTh~;mUn2;7SW+i`!Do-jcfJk_n*^YjUosf+HX^iV-TfW zrvnr*I;9DXOLU0$8XeT>@`1w=<~DXEJwLE%r$Ly6uCe<>Pjo%IGYb<}Et}uP&{2lT zuns-I?C1p29z%yU_D}k*u@MKuz6dry;BPT#@*cPOxlVj{BTmC0&Nji6+GScUlm|&& z?#pnDMq$TMmKWAA7!`|L9V!~-QJVA`1DS{0K^zw~6(n0yCvxH7sFR8C8fCMn>84*e z;aFvxN^T$9UIRHCD>L zGLJ3fM$8ZY)-ZJ3kNOJk2XG}qk0>K3Te|HU)0{xat8*16-DZ>^MRPYw^7QDOyUA6* z?p9U4j?&gW)O{7iW0|=(KUsJA-}5Z(PIVndzzyjTE!nfn)A=ms6d&k{ft?IuGZ13h zN3`$!00eaz5oX)K9?x-cKGOvpJQCK_QPBqd_|wBTk+&SB>X_=*#b> zGJO)w?iIDdRx%JJiZv|TTbgD@nF{I_=B9{>2I82GRVWK%&}-o`XYGO6jVzSKS z3=_WWsLM>QGSNNvHgjzzN4-{a>_BCKEMn-}WXp1_meaEIp99WmEw*f}gA?;U>vNSV gc7Nfd{T*D3wDh#4i^fI?VUv?GlK=n! literal 0 HcmV?d00001 diff --git a/app/main.py b/app/main.py index 060b330..78399e4 100644 --- a/app/main.py +++ b/app/main.py @@ -1,13 +1,15 @@ -import sqlite3 from datetime import datetime from flask import Flask from flask import render_template -from flask import url_for from flask import request, redirect, g -from contextlib import closing -import os, os.path +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 @@ -16,37 +18,30 @@ USERNAME = 'admin' PASSWORD = 'default' app.config.from_object(__name__) -def connect_db(): - result = sqlite3.connect(app.config['DATABASE']) - return result - -def init_db(): - with closing(connect_db()) as db: - with app.open_resource('schema.sql', mode='r') as f: - sql_commands = f.read() - print(sql_commands) - db.cursor().executescript(sql_commands) - db.commit() - @app.before_request def before_request(): - g.db = connect_db() + g.db2 = db.Db(app.config['DATABASE']) + g.data = {} + add_service_warning(g.data) @app.teardown_request def teardown_request(exception): - db = getattr(g, 'db', None) - if db is not None: - db.close() + pass @app.route('/') def index(): - #data = {'pitstopsUrl': url_for('getPitStops')} - #return render_template('index.html', data=data) - return redirect(url_for('getPitStops')) + 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 createPitStop(): - last_pitstop = getLastPitStop() +def create_pit_stop(): + last_pitstop = g.db2.getLastPitStop() errorMsg = {} date = request.form['date'] @@ -85,27 +80,31 @@ def createPitStop(): data = {'last': {'date': date, 'odometer': odometer, 'litres': litres}, 'error': errorMsg} return render_template('newPitStopForm.html', data=data) - addPitStop(date, odometer, litres) + g.db2.addPitStop(date, odometer, litres) - return redirect(url_for('getPitStops')) + return redirect(url_for('get_pit_stops')) @app.route('/pitstops/createForm', methods=['GET']) -def createPitStopForm(): - values = getLastPitStop() +def create_pit_stop_form(): + values = g.db2.getLastPitStop() values['date'] = time.strftime("%Y-%m-%d") - data = {'last': values, 'error': None} - return render_template('newPitStopForm.html', data=data) + 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 getPitStops(): - data = preparePitStops(getAllPitStops()) - data.reverse() - data = {'pitstops': data} - return render_template('pitstops.html', data=data) +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('/statistics', methods=['GET']) -def getStatistics(): - pitstops = getAllPitStops() +def get_statistics(): + pitstops = g.db2.getAllPitStops() count = len(pitstops) distance = 0 sumLitres = 0 @@ -117,50 +116,33 @@ def getStatistics(): sumLitres = 0 for pitstop in pitstops: sumLitres += pitstop['litres'] - averageLitresFuelled = round(sumLitres/count, 2) + averageLitresFuelled = sumLitres/count if count > 1: distance = pitstops[-1]['odometer'] - pitstops[0]['odometer'] - averageDistance = round(distance/(count - 1), 2) - averageLitresUsed = round(100 * (sumLitres-pitstops[0]['litres'])/distance, 2) - data = { - 'distance':distance, - 'count': count, - 'litres': round(sumLitres, 2), - 'averageDistance': averageDistance, - 'averageListresFuelled': averageLitresFuelled, - 'averageListresUsed': averageLitresUsed} - return render_template('statistics.html', data=data) + 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 preparePitStops(pitstops): +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'] = round(100 * curr['litres']/curr['distance'], 2) + 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 -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.strftime(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__': if not os.path.isfile(DATABASE) or os.stat(DATABASE).st_size == 0: - init_db() + 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') \ No newline at end of file diff --git a/app/schema.sql b/app/schema.sql index 4baa685..132e4ca 100644 --- a/app/schema.sql +++ b/app/schema.sql @@ -4,4 +4,12 @@ create table pitstops ( `date` TEXT NOT NULL, `odometer` INTEGER NOT NULL, `litres` REAL NOT NULL +); +drop table if exists services; +CREATE TABLE `services` ( + `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + `date` TEXT, + `odometer_planned` INTEGER NOT NULL, + `odometer_done` INTEGER, + `tasks` TEXT NOT NULL ); \ No newline at end of file diff --git a/app/templates/layout.html b/app/templates/layout.html index e6387a2..e231671 100644 --- a/app/templates/layout.html +++ b/app/templates/layout.html @@ -1,3 +1,22 @@ +{% macro alert_level(km_left) -%} + {% if km_left < 0 %} + alert-danger + {% elif km_left < 300 %} + alert-warning + {% else %} + alert-success + {% endif %} +{%- endmacro %} + +{% macro service_warning() -%} + +{%- endmacro %} + @@ -38,7 +57,7 @@ - Rollerverbrauch + Rollerverbrauch