254 lines
7.7 KiB
Python
254 lines
7.7 KiB
Python
from datetime import date
|
|
from flask import Flask
|
|
from flask import redirect, g
|
|
from flask import render_template
|
|
from flask import url_for
|
|
from flask.ext.mail import Mail
|
|
from flask.ext.security import Security, SQLAlchemyUserDatastore, \
|
|
UserMixin, RoleMixin, login_required, roles_required
|
|
from flask.ext.security import user_registered
|
|
from flask_security.core import current_user
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
from flask.ext.security.forms import LoginForm
|
|
|
|
app = Flask(__name__)
|
|
app.config['SECURITY_PASSWORD_HASH'] = 'pbkdf2_sha512'
|
|
app.config['SECURITY_REGISTERABLE'] = True
|
|
app.config['SECURITY_CHANGEABLE'] = True
|
|
app.config['SECURITY_RECOVERABLE'] = True
|
|
app.config.from_envvar('config')
|
|
app.config.from_object(__name__)
|
|
|
|
db = SQLAlchemy(app)
|
|
mail = Mail(app)
|
|
|
|
import rollerverbrauch.tools as tools
|
|
|
|
from rollerverbrauch.forms import \
|
|
CreatePitstopForm, \
|
|
EditVehicleForm, \
|
|
DeleteVehicleForm, \
|
|
SelectVehicleForm, \
|
|
DeleteAccountForm
|
|
|
|
from rollerverbrauch.entities import \
|
|
User, \
|
|
Role, \
|
|
Pitstop, \
|
|
Vehicle
|
|
|
|
# required to activate the filters
|
|
import rollerverbrauch.filters
|
|
|
|
|
|
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
|
|
security = Security(app, user_datastore)
|
|
|
|
|
|
@user_registered.connect_via(app)
|
|
def user_registered_sighandler(app, user, confirm_token):
|
|
"""
|
|
Called after a user was created
|
|
"""
|
|
role = user_datastore.find_role('user')
|
|
user_datastore.add_role_to_user(user, role)
|
|
new_vehicle = Vehicle('default vehicle')
|
|
db.session.add(new_vehicle)
|
|
user.vehicles.append(new_vehicle)
|
|
db.session.commit()
|
|
tools.db_log_add(user)
|
|
tools.db_log_add(new_vehicle)
|
|
|
|
|
|
@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():
|
|
if current_user.is_authenticated:
|
|
return redirect(url_for('get_pit_stops'))
|
|
else:
|
|
user_count = len(User.query.all())
|
|
vehicles = Vehicle.query.all()
|
|
litres = 0
|
|
kilometers = 0
|
|
for vehicle in vehicles:
|
|
stats = tools.VehicleStats(vehicle)
|
|
litres += stats.overall_litres
|
|
kilometers += stats.overall_distance
|
|
vehicle_count = len(vehicles)
|
|
pitstop_count = len(Pitstop.query.all())
|
|
data = {
|
|
'users':user_count,
|
|
'vehicles': vehicle_count,
|
|
'pitstops': pitstop_count,
|
|
'litres': litres,
|
|
'kilometers': kilometers
|
|
}
|
|
return render_template('index.html', login_user_form=LoginForm(), data=data)
|
|
|
|
|
|
@app.route('/account/edit_vehicle/<int:vid>', methods=['GET', 'POST'])
|
|
@login_required
|
|
def edit_vehicle(vid):
|
|
vehicle = Vehicle.query.filter(Vehicle.id == vid).first()
|
|
form = EditVehicleForm()
|
|
|
|
if form.validate_on_submit():
|
|
if not tools.check_vehicle_name_is_unique(current_user, form.name):
|
|
return render_template('editVehicleForm.html', form=form)
|
|
vehicle.name = form.name.data
|
|
db.session.commit()
|
|
tools.db_log_update(vehicle)
|
|
return redirect(url_for('get_account_page'))
|
|
|
|
form.name.default = vehicle.name
|
|
form.process()
|
|
return render_template('editVehicleForm.html', form=form)
|
|
|
|
|
|
@app.route('/account/delete_vehicle/<int:vid>', methods=['GET', 'POST'])
|
|
@login_required
|
|
def delete_vehicle(vid):
|
|
vehicle = Vehicle.query.filter(Vehicle.id == vid).first()
|
|
|
|
# prevent deletion of foreign vehicles
|
|
if vehicle not in current_user.vehicles:
|
|
return redirect(url_for('get_account_page'))
|
|
|
|
form = DeleteVehicleForm()
|
|
|
|
if form.validate_on_submit():
|
|
db.session.delete(vehicle)
|
|
db.session.commit()
|
|
tools.db_log_delete(vehicle)
|
|
return redirect(url_for('get_account_page'))
|
|
|
|
return render_template('deleteVehicleForm.html', form=form, vehicle=vehicle)
|
|
|
|
|
|
@app.route('/account/create_vehicle', methods=['GET', 'POST'])
|
|
@login_required
|
|
def create_vehicle():
|
|
form = EditVehicleForm()
|
|
|
|
if form.validate_on_submit():
|
|
vehicle_name = form.name.data
|
|
if not tools.check_vehicle_name_is_unique(current_user, form.name):
|
|
return render_template('createVehicleForm.html', form=form)
|
|
new_vehicle = Vehicle(vehicle_name)
|
|
db.session.add(new_vehicle)
|
|
current_user.vehicles.append(new_vehicle)
|
|
db.session.commit()
|
|
tools.db_log_add(new_vehicle)
|
|
return redirect(url_for('get_account_page'))
|
|
|
|
return render_template('createVehicleForm.html', form=form)
|
|
|
|
|
|
@app.route('/pitstops/select_vehicle', methods=['GET', 'POST'])
|
|
@login_required
|
|
def select_vehicle_for_new_pitstop():
|
|
form = SelectVehicleForm()
|
|
form.vehicle.choices = [(g.id, g.name) for g in current_user.vehicles]
|
|
|
|
if form.validate_on_submit():
|
|
vehicle = Vehicle.query.filter(Vehicle.id == form.vehicle.data).first()
|
|
if vehicle not in current_user.vehicles:
|
|
return render_template('selectVehice.html', form=form)
|
|
|
|
return redirect(url_for('create_pit_stop_form', vid=form.vehicle.data))
|
|
|
|
return render_template('selectVehice.html', form=form)
|
|
|
|
|
|
@app.route('/pitstops/create/<int:vid>', methods=['GET', 'POST'])
|
|
@login_required
|
|
def create_pit_stop_form(vid):
|
|
vehicle = Vehicle.query.filter(Vehicle.id == vid).first()
|
|
if vehicle not in current_user.vehicles:
|
|
return redirect(url_for('select_vehicle_for_new_pitstop'))
|
|
|
|
if len(vehicle.pitstops) > 0:
|
|
last_pitstop = vehicle.pitstops[-1]
|
|
else:
|
|
last_pitstop = Pitstop(0, 0, date(1970, 1, 1))
|
|
|
|
form = CreatePitstopForm()
|
|
form.set_pitstop(last_pitstop)
|
|
|
|
if form.validate_on_submit():
|
|
new_stop = Pitstop(form.odometer.data, form.litres.data, form.date.data)
|
|
db.session.add(new_stop)
|
|
vehicle.pitstops.append(new_stop)
|
|
db.session.commit()
|
|
tools.db_log_add(new_stop)
|
|
return redirect(url_for('get_pit_stops', _anchor= 'v' + str(vehicle.id)))
|
|
|
|
form.odometer.default = last_pitstop.odometer
|
|
form.litres.default = last_pitstop.litres
|
|
form.date.default = date.today()
|
|
form.process()
|
|
messages = {
|
|
'date': 'Date must be between %s and %s (including).' % (str(last_pitstop.date), str(date.today())),
|
|
'odometer': 'Odometer must be greater than %s km.' % (str(last_pitstop.odometer))
|
|
}
|
|
return render_template('newPitStopForm.html', form=form, vehicle=vehicle, messages = messages)
|
|
|
|
|
|
@app.route('/pitstops', methods=['GET'])
|
|
@login_required
|
|
def get_pit_stops():
|
|
return render_template('pitstops.html', user=current_user)
|
|
|
|
|
|
@app.route('/manual', methods=['GET'])
|
|
@login_required
|
|
def get_manual():
|
|
return render_template('manual.html', data=g.data)
|
|
|
|
|
|
@app.route('/admin', methods=['GET'])
|
|
@roles_required('admin')
|
|
def get_admin_page():
|
|
g.data['users'] = User.query.all()
|
|
return render_template('admin.html', data=g.data)
|
|
|
|
|
|
@app.route('/account', methods=['GET'])
|
|
@login_required
|
|
def get_account_page():
|
|
return render_template('account.html', data=g.data)
|
|
|
|
|
|
@app.route('/statistics', methods=['GET'])
|
|
@login_required
|
|
def get_statistics():
|
|
stats = []
|
|
for vehicle in current_user.vehicles:
|
|
stats.append(tools.VehicleStats(vehicle))
|
|
return render_template('statistics.html', data=stats)
|
|
|
|
|
|
@app.route('/account/delete', methods=['GET', 'POST'])
|
|
@login_required
|
|
def delete_account():
|
|
form = DeleteAccountForm()
|
|
|
|
if form.validate_on_submit():
|
|
user_datastore.delete_user(current_user)
|
|
db.session.commit()
|
|
return redirect(url_for('index'))
|
|
|
|
return render_template('deleteAccountForm.html', form=form)
|