rollerverbrauch/app/__init__.py

115 lines
3.4 KiB
Python
Raw Normal View History

2017-11-07 22:33:14 +01:00
from flask import Flask, make_response
2016-11-01 18:01:52 +01:00
from flask import g
2016-07-03 19:29:30 +02:00
from flask_mail import Mail
2016-11-01 18:01:52 +01:00
from flask_security import Security, SQLAlchemyUserDatastore, user_registered
2016-04-23 23:08:39 +02:00
from flask_sqlalchemy import SQLAlchemy
2016-07-03 19:29:30 +02:00
import os
from config import config
2017-11-07 22:33:14 +01:00
from flask_security.forms import LoginForm
from flask_limiter.util import get_remote_address
2016-04-23 23:08:39 +02:00
2016-11-01 16:36:28 +01:00
from .forms import *
2016-04-23 23:08:39 +02:00
2016-07-03 19:29:30 +02:00
app = Flask(__name__)
app.config.from_object(config[os.getenv('FLASK_CONFIG') or 'default'])
2017-11-07 22:33:14 +01:00
@app.errorhandler(429)
def ratelimit_handler(e):
return make_response(
jsonify(error="ratelimit exceeded %s" % e.description)
, 429
)
2016-07-03 19:29:30 +02:00
db = SQLAlchemy(app)
mail = Mail(app)
2016-11-01 18:01:52 +01:00
from .entities import *
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)
2016-04-23 23:08:39 +02:00
2016-04-27 07:23:28 +02:00
# required to activate the filters
from .filters import *
from .tools import *
2016-11-01 18:01:52 +01:00
from .routes import *
2016-04-23 23:08:39 +02:00
@user_registered.connect_via(app)
2016-11-01 19:48:08 +01:00
def user_registered_sighandler(application, user, confirm_token):
2016-04-23 23:08:39 +02:00
"""
Called after a user was created
"""
role = user_datastore.find_role('user')
user_datastore.add_role_to_user(user, role)
2016-11-01 19:48:08 +01:00
if user.email == application.config['ADMIN_MAIL']:
# if the user selected the preconfigured email for the admin account
role = user_datastore.find_role('admin')
user_datastore.add_role_to_user(user, role)
2016-04-28 07:10:44 +02:00
new_vehicle = Vehicle('default vehicle')
db.session.add(new_vehicle)
user.vehicles.append(new_vehicle)
2016-04-23 23:24:25 +02:00
db.session.commit()
tools.db_log_add(user)
tools.db_log_add(new_vehicle)
2016-04-23 23:08:39 +02:00
def assure_consumable(name, ext_id, unit):
if not Consumable.query.filter(Consumable.ext_id == ext_id).first():
c = Consumable(name, ext_id, unit)
db.session.add(c)
2016-04-23 23:08:39 +02:00
@app.before_first_request
def before_first_request():
db.create_all()
# make sure all consumables from tankerkoenig exist: diesel, e5, e10
assure_consumable('Diesel', 'diesel', 'L')
assure_consumable('Super','e5', 'L')
assure_consumable('Super E10','e10', 'L')
2016-04-23 23:08:39 +02:00
user_datastore.find_or_create_role(name='admin', description='Role for administrators')
user_datastore.find_or_create_role(name='user', description='Role for all users.')
db.session.commit()
@app.before_request
def before_request():
g.data = {}
@app.route('/')
def index():
2016-05-18 08:17:53 +02:00
if current_user.is_authenticated:
return redirect(url_for('get_pit_stops'))
else:
user_count = len(User.query.all())
consumables = Consumable.query.all()
per_consumable = {}
for consumable in consumables:
per_consumable[consumable.id] = {
'name': consumable.name,
'unit': consumable.unit,
'amount': 0
}
2016-05-18 08:17:53 +02:00
vehicles = Vehicle.query.all()
kilometers = 0
for vehicle in vehicles:
stats = tools.VehicleStats(vehicle)
for consumable in stats.consumables:
per_consumable[consumable.id]['amount'] += consumable.overall_amount
2016-05-18 08:17:53 +02:00
kilometers += stats.overall_distance
vehicle_count = len(vehicles)
pitstop_count = len(Pitstop.query.all())
data = {
2016-07-04 20:19:59 +02:00
'users': user_count,
2016-05-18 08:17:53 +02:00
'vehicles': vehicle_count,
'pitstops': pitstop_count,
'kilometers': kilometers,
'consumables': per_consumable
2016-05-18 08:17:53 +02:00
}
2016-05-18 08:17:53 +02:00
return render_template('index.html', login_user_form=LoginForm(), data=data)
2016-04-23 23:08:39 +02:00