rollerverbrauch/app/__init__.py

96 lines
2.8 KiB
Python
Raw Normal View History

2016-11-01 18:01:52 +01:00
from flask import Flask
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-07-03 19:29:30 +02:00
from flask_security.forms import LoginForm
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
2016-05-18 08:17:53 +02:00
from flask.ext.security.forms import LoginForm
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'])
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
@app.before_first_request
def before_first_request():
db.create_all()
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