From fd206de2638e2ee47dbab4e2d22b6eca4c7b525a Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Thu, 28 Apr 2016 06:32:51 +0200 Subject: [PATCH] add logging for performed db operations --- app/main.py | 6 ++++++ app/rollerverbrauch/__init__.py | 14 +++++++++++--- app/rollerverbrauch/entities.py | 6 +++--- app/rollerverbrauch/tools.py | 15 +++++++++++++++ 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/app/main.py b/app/main.py index 77519c8..3bce806 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,12 @@ import os from rollerverbrauch import app +import logging + + +def setup_logging(): + logging.basicConfig(format='%(asctime)s [%(levelname)s]: %(message)s', level=logging.INFO) if __name__ == '__main__': DEBUG = 'DEBUG' in os.environ and os.environ['DEBUG'] != 'False' + setup_logging() app.run(debug=DEBUG, host='0.0.0.0') diff --git a/app/rollerverbrauch/__init__.py b/app/rollerverbrauch/__init__.py index e2eea31..fced2c7 100644 --- a/app/rollerverbrauch/__init__.py +++ b/app/rollerverbrauch/__init__.py @@ -9,7 +9,7 @@ from flask.ext.security import Security, SQLAlchemyUserDatastore, \ from flask.ext.security import user_registered from flask_security.core import current_user from flask_sqlalchemy import SQLAlchemy - +import logging app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////data/rollerverbrauch.db' @@ -24,7 +24,10 @@ db = SQLAlchemy(app) mail = Mail(app) from rollerverbrauch.tools import \ - VehicleStats + VehicleStats, \ + db_log_add, \ + db_log_delete, \ + db_log_update from rollerverbrauch.forms import \ CreatePitstopForm, \ @@ -57,6 +60,8 @@ def user_registered_sighandler(app, user, confirm_token): db.session.add(v) user.vehicles.append(v) db.session.commit() + db_log_add(user) + db_log_add(new_vehicle) @app.before_first_request @@ -87,6 +92,7 @@ def edit_vehicle(vid): if form.validate_on_submit(): vehicle.name = form.name.data db.session.commit() + db_log_update(vehicle) return redirect(url_for('get_account_page')) form.name.default = vehicle.name @@ -108,6 +114,7 @@ def delete_vehicle(vid): if form.validate_on_submit(): db.session.delete(vehicle) db.session.commit() + db_log_delete(vehicle) return redirect(url_for('get_account_page')) return render_template('deleteVehicleForm.html', form=form, vehicle=vehicle) @@ -123,6 +130,7 @@ def create_vehicle(): db.session.add(new_vehicle) current_user.vehicles.append(new_vehicle) db.session.commit() + db_log_add(new_vehicle) return redirect(url_for('get_account_page')) return render_template('createVehicleForm.html', form=form) @@ -164,6 +172,7 @@ def create_pit_stop_form(vid): db.session.add(new_stop) vehicle.pitstops.append(new_stop) db.session.commit() + db_log_add(new_stop) return redirect(url_for('get_pit_stops', _anchor= 'v' + str(vehicle.id))) form.odometer.default = last_pitstop.odometer @@ -195,7 +204,6 @@ def get_admin_page(): @app.route('/account', methods=['GET']) @login_required def get_account_page(): - print(current_user) return render_template('account.html', data=g.data) diff --git a/app/rollerverbrauch/entities.py b/app/rollerverbrauch/entities.py index 8c40b1c..f89069b 100644 --- a/app/rollerverbrauch/entities.py +++ b/app/rollerverbrauch/entities.py @@ -34,7 +34,7 @@ class User(db.Model, UserMixin): ) def __repr__(self): - return '' % self.email + return '' % self.name + return '' % (self.id, self.owner_id, self.name) class Pitstop(db.Model): @@ -65,4 +65,4 @@ class Pitstop(db.Model): self.date = date def __repr__(self): - return '' % (self.odometer, self.litres) \ No newline at end of file + return '' % (self.odometer, self.litres, self.date, self.vehicle_id) \ No newline at end of file diff --git a/app/rollerverbrauch/tools.py b/app/rollerverbrauch/tools.py index a6281dc..0a51317 100644 --- a/app/rollerverbrauch/tools.py +++ b/app/rollerverbrauch/tools.py @@ -1,3 +1,6 @@ +import logging + + class VehicleStats: def __init__(self, vehicle): self.name = vehicle.name @@ -16,3 +19,15 @@ class VehicleStats: self.overall_distance = vehicle.pitstops[-1].odometer - vehicle.pitstops[0].odometer self.average_distance = self.overall_distance / (self.pitstop_count - 1) self.average_litres_used = 100 * (self.overall_litres - vehicle.pitstops[0].litres) / self.overall_distance + + +def db_log_add(entity): + logging.info('db_add: %s' % str(entity)) + + +def db_log_delete(entity): + logging.info('db_delete: %s' % str(entity)) + + +def db_log_update(entity): + logging.info('db_update: %s' % str(entity))