add logging for performed db operations
This commit is contained in:
parent
e936f1c60f
commit
fd206de263
|
@ -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')
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
||||
|
|
|
@ -34,7 +34,7 @@ class User(db.Model, UserMixin):
|
|||
)
|
||||
|
||||
def __repr__(self):
|
||||
return '<User %r>' % self.email
|
||||
return '<User id="%r" email="%r" ' % (self.id, self.email)
|
||||
|
||||
|
||||
class Vehicle(db.Model):
|
||||
|
@ -49,7 +49,7 @@ class Vehicle(db.Model):
|
|||
self.name = name
|
||||
|
||||
def __repr__(self):
|
||||
return '<Vehicle %r>' % self.name
|
||||
return '<Vehicle id="%r" owner_id="%r" name="%r" />' % (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 '<Pitstop %r km, %r l>' % (self.odometer, self.litres)
|
||||
return '<Pitstop odometer="%r" litres="%r" date="%r" vehicle_id="%r">' % (self.odometer, self.litres, self.date, self.vehicle_id)
|
|
@ -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))
|
||||
|
|
Loading…
Reference in New Issue