Merge branch 'development' into 'master'
Development See merge request !30
This commit is contained in:
commit
af6f271b29
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
@ -0,0 +1 @@
|
||||
*.sh text eol=lf
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,4 +1,6 @@
|
||||
.idea/
|
||||
tests/results/
|
||||
**.DS_Store
|
||||
**.swp
|
||||
**.pyc
|
||||
*.sqlite
|
421
app/__init__.py
421
app/__init__.py
@ -1,32 +1,14 @@
|
||||
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 import g
|
||||
from flask_mail import Mail
|
||||
from flask_security import Security, SQLAlchemyUserDatastore, \
|
||||
login_required, roles_required, user_registered
|
||||
from flask_security.core import current_user
|
||||
from flask_security import Security, SQLAlchemyUserDatastore, user_registered
|
||||
from flask_security.forms import LoginForm
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
import os
|
||||
from config import config
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from flask.ext.security.forms import LoginForm
|
||||
|
||||
from .forms import \
|
||||
CreatePitstopForm, \
|
||||
EditVehicleForm, \
|
||||
DeleteVehicleForm, \
|
||||
SelectVehicleForm, \
|
||||
DeleteAccountForm, \
|
||||
DeletePitStopForm, \
|
||||
EditPitstopForm, \
|
||||
CreateConsumableForm, \
|
||||
EditConsumableForm, \
|
||||
DeletConsumableForm, \
|
||||
SelectConsumableForm
|
||||
from .forms import *
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
@ -35,29 +17,28 @@ app.config.from_object(config[os.getenv('FLASK_CONFIG') or 'default'])
|
||||
db = SQLAlchemy(app)
|
||||
mail = Mail(app)
|
||||
|
||||
from .entities import \
|
||||
User, \
|
||||
Role, \
|
||||
Pitstop, \
|
||||
Vehicle, \
|
||||
Consumable
|
||||
|
||||
# required to activate the filters
|
||||
from .filters import *
|
||||
from .tools import *
|
||||
|
||||
from .entities import *
|
||||
|
||||
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
|
||||
security = Security(app, user_datastore)
|
||||
|
||||
# required to activate the filters
|
||||
from .filters import *
|
||||
from .tools import *
|
||||
from .routes import *
|
||||
|
||||
|
||||
@user_registered.connect_via(app)
|
||||
def user_registered_sighandler(app, user, confirm_token):
|
||||
def user_registered_sighandler(application, user, confirm_token):
|
||||
"""
|
||||
Called after a user was created
|
||||
"""
|
||||
role = user_datastore.find_role('user')
|
||||
user_datastore.add_role_to_user(user, role)
|
||||
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)
|
||||
new_vehicle = Vehicle('default vehicle')
|
||||
db.session.add(new_vehicle)
|
||||
user.vehicles.append(new_vehicle)
|
||||
@ -112,377 +93,3 @@ def index():
|
||||
|
||||
return render_template('index.html', login_user_form=LoginForm(), data=data)
|
||||
|
||||
|
||||
@app.route('/account/vehicle/edit/<int:vid>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def edit_vehicle(vid):
|
||||
vehicle = Vehicle.query.filter(Vehicle.id == vid).first()
|
||||
|
||||
# prevent edit of foreign vehicles
|
||||
if vehicle not in current_user.vehicles:
|
||||
return redirect(url_for('get_account_page'))
|
||||
|
||||
form = EditVehicleForm()
|
||||
form.consumables.choices = [(g.id, g.name) for g in Consumable.query.all()]
|
||||
|
||||
if not form.consumables.data:
|
||||
form.consumables.default = [g.id for g in vehicle.consumables]
|
||||
|
||||
if form.name.data is not None:
|
||||
form.name.default = form.name.data
|
||||
|
||||
if form.validate_on_submit():
|
||||
vehicle.name = form.name.data
|
||||
# we cannot delete consumables where there are pitstops for => report error
|
||||
vehicle.consumables = []
|
||||
for consumable_id in form.consumables.data:
|
||||
consumable = Consumable.query.get(consumable_id)
|
||||
if consumable is not None:
|
||||
vehicle.consumables.append(consumable)
|
||||
try:
|
||||
db.session.commit()
|
||||
tools.db_log_update(vehicle)
|
||||
except IntegrityError:
|
||||
db.session.rollback()
|
||||
form.name.errors.append('"%s" is not unique.' % (form.name.data))
|
||||
return render_template('editVehicleForm.html', form=form)
|
||||
return redirect(url_for('get_account_page'))
|
||||
|
||||
form.name.default = vehicle.name
|
||||
form.process()
|
||||
return render_template('editVehicleForm.html', form=form, vehicle=vehicle)
|
||||
|
||||
|
||||
@app.route('/account/vehicle/delete/<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'))
|
||||
|
||||
if len(current_user.vehicles) == 1:
|
||||
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/vehicle/create', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def create_vehicle():
|
||||
form = EditVehicleForm()
|
||||
form.consumables.choices = [(g.id, g.name) for g in Consumable.query.all()]
|
||||
|
||||
if form.name.data is not None:
|
||||
form.name.default = form.name.data
|
||||
|
||||
if form.consumables.data:
|
||||
form.consumables.default = form.consumables.data
|
||||
else:
|
||||
form.consumables.default = []
|
||||
|
||||
if form.validate_on_submit():
|
||||
if len(form.consumables.data) == 0:
|
||||
form.consumables.errors.append('At least one consumable must be selected.')
|
||||
return render_template('createVehicleForm.html', form=form)
|
||||
|
||||
vehicle_name = form.name.data
|
||||
new_vehicle = Vehicle(vehicle_name)
|
||||
for consumable_id in form.consumables.data:
|
||||
consumable = Consumable.query.get(consumable_id)
|
||||
if consumable is not None:
|
||||
new_vehicle.consumables.append(consumable)
|
||||
db.session.add(new_vehicle)
|
||||
current_user.vehicles.append(new_vehicle)
|
||||
try:
|
||||
db.session.commit()
|
||||
tools.db_log_add(new_vehicle)
|
||||
except IntegrityError:
|
||||
db.session.rollback()
|
||||
form.name.errors.append('"%s" is not unique.' % (form.name.data))
|
||||
return render_template('createVehicleForm.html', form=form)
|
||||
return redirect(url_for('get_account_page'))
|
||||
|
||||
return render_template('createVehicleForm.html', form=form)
|
||||
|
||||
|
||||
@app.route('/pitstops/vehicle/select', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def select_vehicle_for_new_pitstop():
|
||||
if len(current_user.vehicles) == 1:
|
||||
return redirect(url_for('select_consumable_for_new_pitstop', vid=current_user.vehicles[0].id))
|
||||
|
||||
form = SelectVehicleForm()
|
||||
form.vehicle.choices = [(g.id, g.name) for g in current_user.vehicles]
|
||||
|
||||
if form.validate_on_submit():
|
||||
return redirect(url_for('select_consumable_for_new_pitstop', vid=form.vehicle.data))
|
||||
|
||||
return render_template('selectVehicle.html', form=form)
|
||||
|
||||
|
||||
@app.route('/pitstops/vehicle/<int:vid>/consumable/select', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def select_consumable_for_new_pitstop(vid):
|
||||
vehicle = Vehicle.query.get(vid)
|
||||
if vehicle is None or vehicle not in current_user.vehicles:
|
||||
return redirect(url_for('select_vehicle_for_new_pitstop'))
|
||||
|
||||
if len(vehicle.consumables) == 1:
|
||||
return redirect(url_for('create_pit_stop_form', vid=vid, cid=vehicle.consumables[0].id))
|
||||
|
||||
form = SelectConsumableForm()
|
||||
form.consumable.choices = [(g.id, g.name) for g in vehicle.consumables]
|
||||
|
||||
if form.validate_on_submit():
|
||||
return redirect(url_for('create_pit_stop_form', vid=vid, cid=form.consumable.data))
|
||||
|
||||
return render_template('selectConsumableForVehicle.html', vehicle=vehicle, form=form)
|
||||
|
||||
|
||||
@app.route('/pitstops/vehicle/<int:vid>/consumable/<int:cid>/create', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def create_pit_stop_form(vid, cid):
|
||||
vehicle = Vehicle.query.get(vid)
|
||||
if vehicle is None or vehicle not in current_user.vehicles:
|
||||
return redirect(url_for('select_vehicle_for_new_pitstop'))
|
||||
|
||||
consumable = Consumable.query.get(cid)
|
||||
if consumable not in vehicle.consumables:
|
||||
return redirect(url_for('select_consumable_for_new_pitstop', vid=vid))
|
||||
|
||||
form = CreatePitstopForm()
|
||||
|
||||
# the last pitstop is required to be able to check the monotonicy of date and odometer
|
||||
last_pitstop = tools.get_latest_pitstop_for_vehicle(vid)
|
||||
last_pitstop_consumable = tools.get_latest_pitstop_for_vehicle_and_consumable(vid, cid)
|
||||
|
||||
# we can enter the same odometer if the pitstops are not equal
|
||||
form.same_odometer_allowed = (last_pitstop != last_pitstop_consumable)
|
||||
|
||||
# set the lower limits for odometer andd date and the values for amount and costs of the last stop
|
||||
form.set_pitstop(tools.compute_lower_limits_for_new_pitstop(last_pitstop, last_pitstop_consumable, cid))
|
||||
|
||||
# set the label of the litres field to make the user comfortable
|
||||
form.set_consumable(consumable)
|
||||
|
||||
# preinitialize the defaults with potentially existing values from a try before
|
||||
form.preinit_with_data()
|
||||
|
||||
#
|
||||
# Validate should accept same odometer on different consumables
|
||||
#
|
||||
if form.validate_on_submit():
|
||||
new_stop = Pitstop(form.odometer.data, form.litres.data, form.date.data, form.costs.data, cid)
|
||||
db.session.add(new_stop)
|
||||
vehicle.pitstops.append(new_stop)
|
||||
try:
|
||||
db.session.commit()
|
||||
tools.db_log_add(new_stop)
|
||||
except IntegrityError:
|
||||
db.session.rollback()
|
||||
form.odometer.errors.append('Pitstop already present for %s at odometer %s km!' % (consumable.name, form.odometer.data))
|
||||
return render_template('createPitStopForm.html', form=form, vehicle=vehicle, messages=form.get_hint_messages())
|
||||
return redirect(url_for('get_pit_stops', _anchor= 'v' + str(vehicle.id)))
|
||||
|
||||
form.process()
|
||||
return render_template('createPitStopForm.html', form=form, vehicle=vehicle, messages=form.get_hint_messages())
|
||||
|
||||
|
||||
@app.route('/pitstops/delete/<int:pid>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def delete_pit_stop_form(pid):
|
||||
pitstop = Pitstop.query.filter(Pitstop.id == pid).first()
|
||||
if pitstop is None:
|
||||
return redirect(url_for('get_pit_stops'))
|
||||
vehicle = Vehicle.query.filter(Vehicle.id == pitstop.vehicle_id).first()
|
||||
if vehicle not in current_user.vehicles:
|
||||
return redirect(url_for('get_pit_stops'))
|
||||
|
||||
form = DeletePitStopForm()
|
||||
if form.validate_on_submit():
|
||||
db.session.delete(pitstop)
|
||||
db.session.commit()
|
||||
tools.db_log_delete(pitstop)
|
||||
return redirect(url_for('get_pit_stops', _anchor='v' + str(vehicle.id)))
|
||||
|
||||
return render_template('deletePitstopForm.html', form=form, pitstop=pitstop )
|
||||
|
||||
|
||||
@app.route('/pitstops/edit/<int:pid>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def edit_pit_stop_form(pid):
|
||||
edit_pitstop = Pitstop.query.get(pid)
|
||||
if edit_pitstop is None:
|
||||
return redirect(url_for('get_pit_stops'))
|
||||
|
||||
vehicle = Vehicle.query.filter(Vehicle.id == edit_pitstop.vehicle_id).first()
|
||||
if vehicle not in current_user.vehicles:
|
||||
return redirect(url_for('get_pit_stops'))
|
||||
|
||||
last_pitstop_pos = vehicle.pitstops.index(edit_pitstop) - 1
|
||||
if last_pitstop_pos > 0:
|
||||
last_pitstop = vehicle.pitstops[last_pitstop_pos]
|
||||
else:
|
||||
last_pitstop = Pitstop(0, 0, date(1970, 1, 1), 0, 0)
|
||||
|
||||
form = EditPitstopForm()
|
||||
form.set_pitstop(last_pitstop)
|
||||
|
||||
if form.validate_on_submit():
|
||||
edit_pitstop.costs = form.costs.data
|
||||
edit_pitstop.date = form.date.data
|
||||
edit_pitstop.amount = form.litres.data
|
||||
edit_pitstop.odometer = form.odometer.data
|
||||
db.session.commit()
|
||||
tools.db_log_update(edit_pitstop)
|
||||
return redirect(url_for('get_pit_stops', _anchor='v' + str(vehicle.id)))
|
||||
|
||||
form.odometer.default = edit_pitstop.odometer
|
||||
form.litres.default = edit_pitstop.amount
|
||||
form.date.default = edit_pitstop.date
|
||||
form.costs.default = edit_pitstop.costs
|
||||
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))
|
||||
}
|
||||
if edit_pitstop.costs is not None and edit_pitstop.costs > 0:
|
||||
messages['costs'] = 'Costs must be higher than 0.01 €.'
|
||||
return render_template('editPitStopForm.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():
|
||||
users = User.query.all()
|
||||
consumables = Consumable.query.all()
|
||||
for consumable in consumables:
|
||||
consumable.in_use = len(consumable.vehicles) > 0
|
||||
return render_template('admin.html', users=users, consumables=consumables)
|
||||
|
||||
|
||||
@app.route('/admin/consumable/create', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def create_consumable():
|
||||
form = CreateConsumableForm()
|
||||
|
||||
# preinitialize the defaults with potentially existing values from a try before
|
||||
if form.name.data is not None:
|
||||
form.name.default = form.name.data
|
||||
if form.unit.data is not None:
|
||||
form.unit.default = form.unit.data
|
||||
|
||||
if form.validate_on_submit():
|
||||
new_consumable = Consumable(form.name.data, form.unit.data)
|
||||
db.session.add(new_consumable)
|
||||
try:
|
||||
db.session.commit()
|
||||
tools.db_log_add(new_consumable)
|
||||
except IntegrityError:
|
||||
db.session.rollback()
|
||||
form.name.errors.append('"%s" is not unique.' % (form.name.data))
|
||||
return render_template('createConsumableForm.html', form=form)
|
||||
return redirect(url_for('get_admin_page'))
|
||||
|
||||
return render_template('createConsumableForm.html', form=form)
|
||||
|
||||
|
||||
@app.route('/admin/consumable/delete/<int:cid>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def delete_consumable(cid):
|
||||
consumable = Consumable.query.filter(Consumable.id == cid).first()
|
||||
if consumable is None:
|
||||
return redirect(url_for('get_admin_page'))
|
||||
|
||||
form = DeletConsumableForm()
|
||||
|
||||
if form.validate_on_submit():
|
||||
db.session.delete(consumable)
|
||||
db.session.commit()
|
||||
tools.db_log_delete(consumable)
|
||||
return redirect(url_for('get_admin_page'))
|
||||
|
||||
return render_template('deleteConsumableForm.html', form=form, consumable=consumable)
|
||||
|
||||
|
||||
@app.route('/admin/consumable/edit/<int:cid>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def edit_consumable(cid):
|
||||
consumable = Consumable.query.filter(Consumable.id == cid).first()
|
||||
if consumable is None:
|
||||
return redirect(url_for('get_admin_page'))
|
||||
|
||||
form = EditConsumableForm()
|
||||
|
||||
form.name.default = consumable.name
|
||||
form.unit.default = consumable.unit
|
||||
|
||||
# preinitialize the defaults with potentially existing values from a try before
|
||||
if form.name.data is not None:
|
||||
form.name.default = form.name.data
|
||||
if form.unit.data is not None:
|
||||
form.unit.default = form.unit.data
|
||||
|
||||
if form.validate_on_submit():
|
||||
consumable.name = form.name.data
|
||||
consumable.unit = form.unit.data
|
||||
try:
|
||||
db.session.commit()
|
||||
tools.db_log_update(consumable)
|
||||
except IntegrityError:
|
||||
db.session.rollback()
|
||||
form.name.errors.append('"%s" is not unique.' % (form.name.data))
|
||||
return render_template('editConsumableForm.html', form=form)
|
||||
return redirect(url_for('get_admin_page'))
|
||||
|
||||
return render_template('editConsumableForm.html', form=form)
|
||||
|
||||
|
||||
@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)
|
||||
|
@ -63,6 +63,9 @@ class Vehicle(db.Model):
|
||||
pitstops = db.relationship(
|
||||
'Pitstop'
|
||||
)
|
||||
services = db.relationship(
|
||||
'Service'
|
||||
)
|
||||
consumables = db.relationship(
|
||||
'Consumable',
|
||||
secondary=vehicles_consumables
|
||||
@ -140,3 +143,23 @@ class Consumable(db.Model):
|
||||
|
||||
def __repr__(self):
|
||||
return '<Consumable name="%s" unit="%s" />' % (self.name, self.unit)
|
||||
|
||||
|
||||
class Service(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
date = db.Column(db.Date)
|
||||
odometer = db.Column(db.Integer)
|
||||
vehicle_id = db.Column(db.Integer, db.ForeignKey('vehicle.id'))
|
||||
costs = db.Column(db.Numeric(10, 2), default=0)
|
||||
description = db.Column(db.String(4096))
|
||||
|
||||
def __init__(self, date, odometer, vehicle_id, costs, description):
|
||||
self.description = description
|
||||
self.costs = costs
|
||||
self.date = date
|
||||
self.odometer = odometer
|
||||
self.vehicle_id = vehicle_id
|
||||
|
||||
def __repr__(self):
|
||||
return '<Service odometer="%r" date="%r" vehicle_id="%r" costs="%r" description="%r">' % \
|
||||
(self.odometer, self.date, self.vehicle_id, self.costs, self.description)
|
||||
|
@ -1,5 +1,6 @@
|
||||
from app import app
|
||||
import hashlib
|
||||
import markdown
|
||||
|
||||
|
||||
@app.template_filter('none_filter')
|
||||
@ -20,3 +21,8 @@ def md5_filter(value):
|
||||
@app.template_filter('str')
|
||||
def str_filter(value):
|
||||
return str(value)
|
||||
|
||||
|
||||
@app.template_filter('markdown')
|
||||
def md(value):
|
||||
return markdown.markdown(value)
|
||||
|
169
app/forms.py
169
app/forms.py
@ -1,169 +0,0 @@
|
||||
from flask_wtf import Form
|
||||
from wtforms import DateField, IntegerField, DecimalField, StringField, SelectField, SubmitField, SelectMultipleField, BooleanField
|
||||
from wtforms.validators import ValidationError, Length
|
||||
from datetime import date
|
||||
|
||||
|
||||
def date_check(form, field):
|
||||
"""
|
||||
Checks that the date of the pitstop is not before the date of the latest pitstop and not after today.
|
||||
|
||||
:param form: the form where the field is in
|
||||
:param field: the field to check
|
||||
:return: Nothing or a ValidationError if the limits are not kept
|
||||
"""
|
||||
if field.data < form.last_pitstop.date:
|
||||
raise ValidationError('The new date must not be before %s' % form.last_pitstop.date)
|
||||
if field.data > date.today():
|
||||
raise ValidationError('The new date must not be after %s' % date.today())
|
||||
|
||||
|
||||
def odometer_check(form, field):
|
||||
"""
|
||||
|
||||
:param form:
|
||||
:param field:
|
||||
:return:
|
||||
"""
|
||||
if not form.same_odometer_allowed and field.data <= form.last_pitstop.odometer:
|
||||
raise ValidationError('The new odometer value must be higher than %i km' % form.last_pitstop.odometer)
|
||||
if form.same_odometer_allowed and field.data < form.last_pitstop.odometer:
|
||||
raise ValidationError('The new odometer value must be higher than %i km' % form.last_pitstop.odometer)
|
||||
|
||||
|
||||
def litres_check(form, field):
|
||||
if field.data is not None and field.data <= 0:
|
||||
raise ValidationError('You must fuel at least 0.1 l')
|
||||
|
||||
|
||||
def costs_check(form, field):
|
||||
if field.data is not None and field.data <= 0:
|
||||
raise ValidationError('Costs must be above 0.01 €.')
|
||||
|
||||
|
||||
def edit_costs_check(form, field):
|
||||
"""
|
||||
Costs must be given, if a default value was given to the form field.
|
||||
:param form:
|
||||
:param field:
|
||||
:return:
|
||||
"""
|
||||
costs_check_required = (form.costs.default is not None and form.costs.default > 0)
|
||||
if costs_check_required and field.data is not None and field.data <= 0:
|
||||
raise ValidationError('Costs must be above 0.01 €.')
|
||||
|
||||
|
||||
class SelectVehicleForm(Form):
|
||||
vehicle = SelectField('Vehicle', coerce=int)
|
||||
submit = SubmitField(label='Do it!')
|
||||
|
||||
|
||||
class SelectConsumableForm(Form):
|
||||
consumable = SelectField('Consumable', coerce=int)
|
||||
submit = SubmitField(label='Do it!')
|
||||
|
||||
|
||||
class CreatePitstopForm(Form):
|
||||
date = DateField('Date of Pitstop', validators=[date_check])
|
||||
odometer = IntegerField('Odometer (km)', validators=[odometer_check])
|
||||
litres = DecimalField('Litres (l)', places=2, validators=[litres_check])
|
||||
costs = DecimalField('Costs (€, overall)', places=2, validators=[costs_check])
|
||||
submit = SubmitField(label='Do it!')
|
||||
last_pitstop = None
|
||||
same_odometer_allowed = True
|
||||
|
||||
def set_pitstop(self, last_pitstop):
|
||||
self.last_pitstop = last_pitstop
|
||||
|
||||
def set_consumable(self, consumable):
|
||||
self.litres.label = '%s (%s)' % (consumable.name, consumable.unit)
|
||||
|
||||
def preinit_with_data(self):
|
||||
if self.date.data:
|
||||
self.date.default = self.date.data
|
||||
else:
|
||||
self.date.default = date.today()
|
||||
if self.odometer.data:
|
||||
self.odometer.default = self.odometer.data
|
||||
else:
|
||||
self.odometer.default = self.last_pitstop.odometer
|
||||
if self.litres.data:
|
||||
self.litres.default = self.litres.data
|
||||
else:
|
||||
self.litres.default = self.last_pitstop.amount
|
||||
if self.costs.data:
|
||||
self.costs.default = self.costs.data
|
||||
else:
|
||||
self.costs.default = self.last_pitstop.costs
|
||||
|
||||
def get_hint_messages(self):
|
||||
if self.same_odometer_allowed:
|
||||
or_equal = ' or equal to'
|
||||
else:
|
||||
or_equal = ''
|
||||
messages = {
|
||||
'date': 'Date must be between %s and %s (including).' % (str(self.last_pitstop.date), str(date.today())),
|
||||
'odometer': 'Odometer must be greater than%s %s km.' % (or_equal, str(self.last_pitstop.odometer)),
|
||||
'costs': 'Costs must be higher than 0.01 €.'
|
||||
}
|
||||
return messages
|
||||
|
||||
|
||||
class EditVehicleForm(Form):
|
||||
name = StringField('Name', validators=[Length(1, 255)])
|
||||
consumables = SelectMultipleField('Consumables', coerce=int,validators=[])
|
||||
submit = SubmitField(label='Do it!')
|
||||
|
||||
|
||||
class DeleteVehicleForm(Form):
|
||||
submit = SubmitField(label='Do it!')
|
||||
|
||||
|
||||
class DeleteAccountForm(Form):
|
||||
submit = SubmitField(label='Really delete my account!')
|
||||
|
||||
|
||||
class DeletePitStopForm(Form):
|
||||
submit = SubmitField(label='Really delete this pitstop!')
|
||||
|
||||
|
||||
class EditPitstopForm(Form):
|
||||
date = DateField('Date of Pitstop', validators=[date_check])
|
||||
odometer = IntegerField('Odometer (km)', validators=[odometer_check])
|
||||
litres = DecimalField('Litres (l)', places=2, validators=[litres_check])
|
||||
costs = DecimalField('Costs (€, overall)', places=2, validators=[edit_costs_check])
|
||||
submit = SubmitField(label='Update it!')
|
||||
last_pitstop = None
|
||||
same_odometer_allowed = True
|
||||
|
||||
def set_pitstop(self, last_pitstop):
|
||||
self.last_pitstop = last_pitstop
|
||||
|
||||
def set_consumable(self, consumable):
|
||||
self.litres.label = '%s (%s)' % (consumable.name, consumable.unit)
|
||||
|
||||
def preinit_with_data(self):
|
||||
if self.date.data:
|
||||
self.date.default = self.date.data
|
||||
if self.odometer.data:
|
||||
self.odometer.default = self.odometer.data
|
||||
if self.litres.data:
|
||||
self.litres.default = self.litres.data
|
||||
if self.costs.data:
|
||||
self.costs.default = self.costs.data
|
||||
|
||||
|
||||
class CreateConsumableForm(Form):
|
||||
name = StringField('Name', validators=[Length(1, 255)])
|
||||
unit = StringField('Unit', validators=[Length(1, 255)])
|
||||
submit = SubmitField(label='Do it!')
|
||||
|
||||
|
||||
class EditConsumableForm(Form):
|
||||
name = StringField('Name', validators=[Length(1, 255)])
|
||||
unit = StringField('Unit', validators=[Length(1, 255)])
|
||||
submit = SubmitField(label='Do it!')
|
||||
|
||||
|
||||
class DeletConsumableForm(Form):
|
||||
submit = SubmitField(label='Do it!')
|
6
app/forms/__init__.py
Normal file
6
app/forms/__init__.py
Normal file
@ -0,0 +1,6 @@
|
||||
from .misc import *
|
||||
from .pitstop import *
|
||||
from .checks import *
|
||||
from .consumable import *
|
||||
from .service import *
|
||||
from .vehicle import *
|
53
app/forms/checks.py
Normal file
53
app/forms/checks.py
Normal file
@ -0,0 +1,53 @@
|
||||
from wtforms.validators import ValidationError
|
||||
from datetime import date
|
||||
|
||||
|
||||
def date_check(form, field):
|
||||
"""
|
||||
Checks that the date of the pitstop is not before the date of the latest pitstop and not after today.
|
||||
|
||||
:param form: the form where the field is in
|
||||
:param field: the field to check
|
||||
:return: Nothing or a ValidationError if the limits are not kept
|
||||
"""
|
||||
if field.data < form.last_pitstop.date:
|
||||
raise ValidationError('The new date must not be before %s' % form.last_pitstop.date)
|
||||
if field.data > date.today():
|
||||
raise ValidationError('The new date must not be after %s' % date.today())
|
||||
|
||||
|
||||
def odometer_check(form, field):
|
||||
"""
|
||||
|
||||
:param form:
|
||||
:param field:
|
||||
:return:
|
||||
"""
|
||||
if not form.same_odometer_allowed and field.data <= form.last_pitstop.odometer:
|
||||
raise ValidationError('The new odometer value must be higher than %i km' % form.last_pitstop.odometer)
|
||||
if form.same_odometer_allowed and field.data < form.last_pitstop.odometer:
|
||||
raise ValidationError('The new odometer value must be higher than %i km' % form.last_pitstop.odometer)
|
||||
|
||||
|
||||
def litres_check(form, field):
|
||||
if field.data is not None and field.data <= 0:
|
||||
raise ValidationError('You must fuel at least 0.1 l')
|
||||
|
||||
|
||||
def costs_check(form, field):
|
||||
if field.data is not None and field.data <= 0:
|
||||
raise ValidationError('Costs must be above 0.01 €.')
|
||||
|
||||
|
||||
def edit_costs_check(form, field):
|
||||
"""
|
||||
Costs must be given, if a default value was given to the form field.
|
||||
:param form:
|
||||
:param field:
|
||||
:return:
|
||||
"""
|
||||
costs_check_required = (form.costs.default is not None and form.costs.default > 0)
|
||||
if costs_check_required and field.data is not None and field.data <= 0:
|
||||
raise ValidationError('Costs must be above 0.01 €.')
|
||||
|
||||
|
24
app/forms/consumable.py
Normal file
24
app/forms/consumable.py
Normal file
@ -0,0 +1,24 @@
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import SelectField, StringField, SubmitField
|
||||
from wtforms.validators import Length
|
||||
|
||||
|
||||
class SelectConsumableForm(FlaskForm):
|
||||
consumable = SelectField('Consumable', coerce=int)
|
||||
submit = SubmitField(label='Do it!')
|
||||
|
||||
|
||||
class CreateConsumableForm(FlaskForm):
|
||||
name = StringField('Name', validators=[Length(1, 255)])
|
||||
unit = StringField('Unit', validators=[Length(1, 255)])
|
||||
submit = SubmitField(label='Do it!')
|
||||
|
||||
|
||||
class EditConsumableForm(FlaskForm):
|
||||
name = StringField('Name', validators=[Length(1, 255)])
|
||||
unit = StringField('Unit', validators=[Length(1, 255)])
|
||||
submit = SubmitField(label='Do it!')
|
||||
|
||||
|
||||
class DeletConsumableForm(FlaskForm):
|
||||
submit = SubmitField(label='Do it!')
|
6
app/forms/misc.py
Normal file
6
app/forms/misc.py
Normal file
@ -0,0 +1,6 @@
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import SubmitField
|
||||
|
||||
|
||||
class DeleteAccountForm(FlaskForm):
|
||||
submit = SubmitField(label='Really delete my account!')
|
82
app/forms/pitstop.py
Normal file
82
app/forms/pitstop.py
Normal file
@ -0,0 +1,82 @@
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import DateField, IntegerField, DecimalField, SubmitField
|
||||
|
||||
from .checks import *
|
||||
|
||||
|
||||
class DeletePitStopForm(FlaskForm):
|
||||
submit = SubmitField(label='Really delete this pitstop!')
|
||||
|
||||
|
||||
class EditPitstopForm(FlaskForm):
|
||||
date = DateField('Date of Pitstop', validators=[date_check])
|
||||
odometer = IntegerField('Odometer (km)', validators=[odometer_check])
|
||||
litres = DecimalField('Litres (l)', places=2, validators=[litres_check])
|
||||
costs = DecimalField('Costs (€, overall)', places=2, validators=[edit_costs_check])
|
||||
submit = SubmitField(label='Update it!')
|
||||
last_pitstop = None
|
||||
same_odometer_allowed = True
|
||||
|
||||
def set_pitstop(self, last_pitstop):
|
||||
self.last_pitstop = last_pitstop
|
||||
|
||||
def set_consumable(self, consumable):
|
||||
self.litres.label = '%s (%s)' % (consumable.name, consumable.unit)
|
||||
|
||||
def preinit_with_data(self):
|
||||
if self.date.data:
|
||||
self.date.default = self.date.data
|
||||
if self.odometer.data:
|
||||
self.odometer.default = self.odometer.data
|
||||
if self.litres.data:
|
||||
self.litres.default = self.litres.data
|
||||
if self.costs.data:
|
||||
self.costs.default = self.costs.data
|
||||
|
||||
|
||||
class CreatePitstopForm(FlaskForm):
|
||||
date = DateField('Date of Pitstop', validators=[date_check])
|
||||
odometer = IntegerField('Odometer (km)', validators=[odometer_check])
|
||||
litres = DecimalField('Litres (l)', places=2, validators=[litres_check])
|
||||
costs = DecimalField('Costs (€, overall)', places=2, validators=[costs_check])
|
||||
submit = SubmitField(label='Do it!')
|
||||
last_pitstop = None
|
||||
same_odometer_allowed = True
|
||||
|
||||
def set_pitstop(self, last_pitstop):
|
||||
self.last_pitstop = last_pitstop
|
||||
|
||||
def set_consumable(self, consumable):
|
||||
self.litres.label = '%s (%s)' % (consumable.name, consumable.unit)
|
||||
|
||||
def preinit_with_data(self):
|
||||
if self.date.data:
|
||||
self.date.default = self.date.data
|
||||
else:
|
||||
self.date.default = date.today()
|
||||
if self.odometer.data:
|
||||
self.odometer.default = self.odometer.data
|
||||
else:
|
||||
self.odometer.default = self.last_pitstop.odometer
|
||||
if self.litres.data:
|
||||
self.litres.default = self.litres.data
|
||||
else:
|
||||
self.litres.default = self.last_pitstop.amount
|
||||
if self.costs.data:
|
||||
self.costs.default = self.costs.data
|
||||
else:
|
||||
self.costs.default = self.last_pitstop.costs
|
||||
|
||||
def get_hint_messages(self):
|
||||
if self.same_odometer_allowed:
|
||||
or_equal = ' or equal to'
|
||||
else:
|
||||
or_equal = ''
|
||||
messages = {
|
||||
'date': 'Date must be between %s and %s (including).' % (str(self.last_pitstop.date), str(date.today())),
|
||||
'odometer': 'Odometer must be greater than%s %s km.' % (or_equal, str(self.last_pitstop.odometer)),
|
||||
'costs': 'Costs must be higher than 0.01 €.'
|
||||
}
|
||||
return messages
|
||||
|
||||
|
68
app/forms/service.py
Normal file
68
app/forms/service.py
Normal file
@ -0,0 +1,68 @@
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import DateField, IntegerField, DecimalField, SubmitField, TextAreaField
|
||||
from wtforms.validators import Length
|
||||
|
||||
from .checks import *
|
||||
|
||||
|
||||
class CreateServiceForm(FlaskForm):
|
||||
date = DateField('Date of Service', validators=[date_check])
|
||||
odometer = IntegerField('Odometer (km)', validators=[odometer_check])
|
||||
costs = DecimalField('Costs (€, overall)', places=2, validators=[costs_check])
|
||||
description = TextAreaField('Description', validators=[Length(1, 4096)])
|
||||
submit = SubmitField(label='Do it!')
|
||||
last_pitstop = None
|
||||
|
||||
def set_pitstop(self, last_pitstop):
|
||||
self.last_pitstop = last_pitstop
|
||||
|
||||
def preinit_with_data(self):
|
||||
if self.date.data:
|
||||
self.date.default = self.date.data
|
||||
else:
|
||||
self.date.default = date.today()
|
||||
|
||||
if self.odometer.data:
|
||||
self.odometer.default = self.odometer.data
|
||||
else:
|
||||
self.odometer.default = self.last_pitstop.odometer
|
||||
|
||||
if self.costs.data:
|
||||
self.costs.default = self.costs.data
|
||||
else:
|
||||
self.costs.default = 0
|
||||
|
||||
|
||||
class DeleteServiceForm(FlaskForm):
|
||||
submit = SubmitField(label='Really delete this service!')
|
||||
|
||||
|
||||
class EditServiceForm(FlaskForm):
|
||||
date = DateField('Date of Service', validators=[date_check])
|
||||
odometer = IntegerField('Odometer (km)', validators=[odometer_check])
|
||||
costs = DecimalField('Costs (€, overall)', places=2, validators=[costs_check])
|
||||
description = TextAreaField('Description', validators=[Length(1, 4096)])
|
||||
submit = SubmitField(label='Do it!')
|
||||
last_pitstop = None
|
||||
same_odometer_allowed = True
|
||||
|
||||
def set_pitstop(self, last_pitstop):
|
||||
self.last_pitstop = last_pitstop
|
||||
|
||||
def preinit_with_data(self):
|
||||
if self.date.data:
|
||||
self.date.default = self.date.data
|
||||
else:
|
||||
self.date.default = date.today()
|
||||
|
||||
if self.odometer.data:
|
||||
self.odometer.default = self.odometer.data
|
||||
else:
|
||||
self.odometer.default = self.last_pitstop.odometer
|
||||
|
||||
if self.costs.data:
|
||||
self.costs.default = self.costs.data
|
||||
else:
|
||||
self.costs.default = 0
|
||||
|
||||
|
20
app/forms/vehicle.py
Normal file
20
app/forms/vehicle.py
Normal file
@ -0,0 +1,20 @@
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import StringField, SubmitField, SelectField, SelectMultipleField
|
||||
from wtforms.validators import Length
|
||||
|
||||
|
||||
class SelectVehicleForm(FlaskForm):
|
||||
vehicle = SelectField('Vehicle', coerce=int)
|
||||
submit = SubmitField(label='Do it!')
|
||||
|
||||
|
||||
class EditVehicleForm(FlaskForm):
|
||||
name = StringField('Name', validators=[Length(1, 255)])
|
||||
consumables = SelectMultipleField('Consumables', coerce=int,validators=[])
|
||||
submit = SubmitField(label='Do it!')
|
||||
|
||||
|
||||
class DeleteVehicleForm(FlaskForm):
|
||||
submit = SubmitField(label='Do it!')
|
||||
|
||||
|
5
app/routes/__init__.py
Normal file
5
app/routes/__init__.py
Normal file
@ -0,0 +1,5 @@
|
||||
from .account import *
|
||||
from .admin import *
|
||||
from .misc import *
|
||||
from .pitstop import *
|
||||
from .service import *
|
131
app/routes/account.py
Normal file
131
app/routes/account.py
Normal file
@ -0,0 +1,131 @@
|
||||
from flask import url_for, redirect, render_template
|
||||
from flask_security import login_required
|
||||
from flask_security.core import current_user
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from ..entities import Vehicle, Consumable
|
||||
from ..forms import EditVehicleForm, DeleteVehicleForm, DeleteAccountForm
|
||||
from ..tools import db_log_update, db_log_delete, db_log_add
|
||||
from .. import app, db, user_datastore
|
||||
|
||||
|
||||
@app.route('/account', methods=['GET'])
|
||||
@login_required
|
||||
def get_account_page():
|
||||
return render_template('account.html')
|
||||
|
||||
|
||||
@app.route('/account/vehicle/edit/<int:vid>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def edit_vehicle(vid):
|
||||
vehicle = Vehicle.query.filter(Vehicle.id == vid).first()
|
||||
|
||||
# prevent edit of foreign vehicles
|
||||
if vehicle not in current_user.vehicles:
|
||||
return redirect(url_for('get_account_page'))
|
||||
|
||||
form = EditVehicleForm()
|
||||
form.consumables.choices = [(g.id, g.name) for g in Consumable.query.all()]
|
||||
|
||||
if not form.consumables.data:
|
||||
form.consumables.default = [g.id for g in vehicle.consumables]
|
||||
|
||||
if form.name.data is not None:
|
||||
form.name.default = form.name.data
|
||||
|
||||
if form.validate_on_submit():
|
||||
vehicle.name = form.name.data
|
||||
# we cannot delete consumables where there are pitstops for => report error
|
||||
vehicle.consumables = []
|
||||
for consumable_id in form.consumables.data:
|
||||
consumable = Consumable.query.get(consumable_id)
|
||||
if consumable is not None:
|
||||
vehicle.consumables.append(consumable)
|
||||
try:
|
||||
db.session.commit()
|
||||
db_log_update(vehicle)
|
||||
except IntegrityError:
|
||||
db.session.rollback()
|
||||
form.name.errors.append('"%s" is not unique.' % (form.name.data))
|
||||
return render_template('editVehicleForm.html', form=form)
|
||||
return redirect(url_for('get_account_page'))
|
||||
|
||||
form.name.default = vehicle.name
|
||||
form.process()
|
||||
return render_template('editVehicleForm.html', form=form, vehicle=vehicle)
|
||||
|
||||
|
||||
@app.route('/account/vehicle/delete/<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'))
|
||||
|
||||
if len(current_user.vehicles) == 1:
|
||||
return redirect(url_for('get_account_page'))
|
||||
|
||||
form = DeleteVehicleForm()
|
||||
|
||||
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)
|
||||
|
||||
|
||||
@app.route('/account/vehicle/create', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def create_vehicle():
|
||||
form = EditVehicleForm()
|
||||
form.consumables.choices = [(g.id, g.name) for g in Consumable.query.all()]
|
||||
|
||||
if form.name.data is not None:
|
||||
form.name.default = form.name.data
|
||||
|
||||
if form.consumables.data:
|
||||
form.consumables.default = form.consumables.data
|
||||
else:
|
||||
form.consumables.default = []
|
||||
|
||||
if form.validate_on_submit():
|
||||
if len(form.consumables.data) == 0:
|
||||
form.consumables.errors.append('At least one consumable must be selected.')
|
||||
return render_template('createVehicleForm.html', form=form)
|
||||
|
||||
vehicle_name = form.name.data
|
||||
new_vehicle = Vehicle(vehicle_name)
|
||||
for consumable_id in form.consumables.data:
|
||||
consumable = Consumable.query.get(consumable_id)
|
||||
if consumable is not None:
|
||||
new_vehicle.consumables.append(consumable)
|
||||
db.session.add(new_vehicle)
|
||||
current_user.vehicles.append(new_vehicle)
|
||||
try:
|
||||
db.session.commit()
|
||||
db_log_add(new_vehicle)
|
||||
except IntegrityError:
|
||||
db.session.rollback()
|
||||
form.name.errors.append('"%s" is not unique.' % (form.name.data))
|
||||
return render_template('createVehicleForm.html', form=form)
|
||||
return redirect(url_for('get_account_page'))
|
||||
|
||||
return render_template('createVehicleForm.html', form=form)
|
||||
|
||||
|
||||
@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)
|
||||
|
96
app/routes/admin.py
Normal file
96
app/routes/admin.py
Normal file
@ -0,0 +1,96 @@
|
||||
from flask import url_for, redirect, render_template
|
||||
from flask_security import login_required, roles_required
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from ..entities import User, Consumable
|
||||
from ..forms import CreateConsumableForm, DeletConsumableForm, EditConsumableForm
|
||||
from ..tools import db_log_update, db_log_delete, db_log_add
|
||||
from .. import app, db
|
||||
|
||||
|
||||
@app.route('/admin', methods=['GET'])
|
||||
@roles_required('admin')
|
||||
def get_admin_page():
|
||||
users = User.query.all()
|
||||
consumables = Consumable.query.all()
|
||||
for consumable in consumables:
|
||||
consumable.in_use = len(consumable.vehicles) > 0
|
||||
return render_template('admin.html', users=users, consumables=consumables)
|
||||
|
||||
|
||||
@app.route('/admin/consumable/create', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def create_consumable():
|
||||
form = CreateConsumableForm()
|
||||
|
||||
# preinitialize the defaults with potentially existing values from a try before
|
||||
if form.name.data is not None:
|
||||
form.name.default = form.name.data
|
||||
if form.unit.data is not None:
|
||||
form.unit.default = form.unit.data
|
||||
|
||||
if form.validate_on_submit():
|
||||
new_consumable = Consumable(form.name.data, form.unit.data)
|
||||
db.session.add(new_consumable)
|
||||
try:
|
||||
db.session.commit()
|
||||
db_log_add(new_consumable)
|
||||
except IntegrityError:
|
||||
db.session.rollback()
|
||||
form.name.errors.append('"%s" is not unique.' % (form.name.data))
|
||||
return render_template('createConsumableForm.html', form=form)
|
||||
return redirect(url_for('get_admin_page'))
|
||||
|
||||
return render_template('createConsumableForm.html', form=form)
|
||||
|
||||
|
||||
@app.route('/admin/consumable/delete/<int:cid>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def delete_consumable(cid):
|
||||
consumable = Consumable.query.filter(Consumable.id == cid).first()
|
||||
if consumable is None:
|
||||
return redirect(url_for('get_admin_page'))
|
||||
|
||||
form = DeletConsumableForm()
|
||||
|
||||
if form.validate_on_submit():
|
||||
db.session.delete(consumable)
|
||||
db.session.commit()
|
||||
db_log_delete(consumable)
|
||||
return redirect(url_for('get_admin_page'))
|
||||
|
||||
return render_template('deleteConsumableForm.html', form=form, consumable=consumable)
|
||||
|
||||
|
||||
@app.route('/admin/consumable/edit/<int:cid>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def edit_consumable(cid):
|
||||
consumable = Consumable.query.filter(Consumable.id == cid).first()
|
||||
if consumable is None:
|
||||
return redirect(url_for('get_admin_page'))
|
||||
|
||||
form = EditConsumableForm()
|
||||
|
||||
form.name.default = consumable.name
|
||||
form.unit.default = consumable.unit
|
||||
|
||||
# preinitialize the defaults with potentially existing values from a try before
|
||||
if form.name.data is not None:
|
||||
form.name.default = form.name.data
|
||||
if form.unit.data is not None:
|
||||
form.unit.default = form.unit.data
|
||||
|
||||
if form.validate_on_submit():
|
||||
consumable.name = form.name.data
|
||||
consumable.unit = form.unit.data
|
||||
try:
|
||||
db.session.commit()
|
||||
db_log_update(consumable)
|
||||
except IntegrityError:
|
||||
db.session.rollback()
|
||||
form.name.errors.append('"%s" is not unique.' % (form.name.data))
|
||||
return render_template('editConsumableForm.html', form=form)
|
||||
return redirect(url_for('get_admin_page'))
|
||||
|
||||
return render_template('editConsumableForm.html', form=form)
|
||||
|
22
app/routes/misc.py
Normal file
22
app/routes/misc.py
Normal file
@ -0,0 +1,22 @@
|
||||
from flask import render_template
|
||||
from flask_security import login_required
|
||||
from flask_security.core import current_user
|
||||
|
||||
from ..tools import VehicleStats
|
||||
from .. import app
|
||||
|
||||
|
||||
@app.route('/statistics', methods=['GET'])
|
||||
@login_required
|
||||
def get_statistics():
|
||||
stats = []
|
||||
for vehicle in current_user.vehicles:
|
||||
stats.append(VehicleStats(vehicle))
|
||||
return render_template('statistics.html', data=stats)
|
||||
|
||||
|
||||
@app.route('/manual', methods=['GET'])
|
||||
@login_required
|
||||
def get_manual():
|
||||
return render_template('manual.html')
|
||||
|
184
app/routes/pitstop.py
Normal file
184
app/routes/pitstop.py
Normal file
@ -0,0 +1,184 @@
|
||||
from flask import url_for, redirect, render_template, flash
|
||||
from flask_security import login_required
|
||||
from flask_security.core import current_user
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from datetime import date
|
||||
|
||||
from ..entities import Vehicle, Consumable, Pitstop
|
||||
from ..forms import SelectVehicleForm, SelectConsumableForm, CreatePitstopForm, EditPitstopForm, DeletePitStopForm
|
||||
from ..tools import db_log_update, db_log_delete, db_log_add, get_latest_pitstop_for_vehicle, \
|
||||
get_latest_pitstop_for_vehicle_and_consumable, compute_lower_limits_for_new_pitstop, pitstop_service_key, \
|
||||
get_event_line_for_vehicle
|
||||
from .. import app, db
|
||||
|
||||
|
||||
@app.route('/pitstops/vehicle/select', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def select_vehicle_for_new_pitstop():
|
||||
if len(current_user.vehicles) == 1:
|
||||
return redirect(url_for('select_consumable_for_new_pitstop', vid=current_user.vehicles[0].id))
|
||||
|
||||
form = SelectVehicleForm()
|
||||
form.vehicle.choices = [(g.id, g.name) for g in current_user.vehicles]
|
||||
|
||||
if form.validate_on_submit():
|
||||
return redirect(url_for('select_consumable_for_new_pitstop', vid=form.vehicle.data))
|
||||
|
||||
return render_template('selectVehicle.html', form=form)
|
||||
|
||||
|
||||
@app.route('/pitstops/vehicle/<int:vid>/consumable/select', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def select_consumable_for_new_pitstop(vid):
|
||||
vehicle = Vehicle.query.get(vid)
|
||||
if vehicle is None or vehicle not in current_user.vehicles:
|
||||
return redirect(url_for('select_vehicle_for_new_pitstop'))
|
||||
|
||||
if len(vehicle.consumables) == 0:
|
||||
flash('Please choose at least one consumable!', 'warning')
|
||||
return redirect(url_for('edit_vehicle', vid=vid))
|
||||
|
||||
if len(vehicle.consumables) == 1:
|
||||
return redirect(url_for('create_pit_stop_form', vid=vid, cid=vehicle.consumables[0].id))
|
||||
|
||||
form = SelectConsumableForm()
|
||||
form.consumable.choices = [(g.id, g.name) for g in vehicle.consumables]
|
||||
|
||||
if form.validate_on_submit():
|
||||
return redirect(url_for('create_pit_stop_form', vid=vid, cid=form.consumable.data))
|
||||
|
||||
return render_template('selectConsumableForVehicle.html', vehicle=vehicle, form=form)
|
||||
|
||||
|
||||
@app.route('/pitstops/vehicle/<int:vid>/consumable/<int:cid>/create', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def create_pit_stop_form(vid, cid):
|
||||
vehicle = Vehicle.query.get(vid)
|
||||
if vehicle is None or vehicle not in current_user.vehicles:
|
||||
return redirect(url_for('select_vehicle_for_new_pitstop'))
|
||||
|
||||
consumable = Consumable.query.get(cid)
|
||||
if consumable not in vehicle.consumables:
|
||||
return redirect(url_for('select_consumable_for_new_pitstop', vid=vid))
|
||||
|
||||
form = CreatePitstopForm()
|
||||
|
||||
data = get_event_line_for_vehicle(vehicle)
|
||||
if len(data) > 0:
|
||||
form.set_pitstop(Pitstop(data[-1].odometer, 0, data[-1].date, 0, cid))
|
||||
form.same_odometer_allowed = (type(data[-1]) != Pitstop) or (data[-1].consumable.id != cid)
|
||||
else:
|
||||
form.set_pitstop(Pitstop(date(1970, 1, 1), 0, vid, 0, ''))
|
||||
form.set_pitstop(Pitstop(0, 0, date(1970, 1, 1), 0, cid))
|
||||
form.same_odometer_allowed = True
|
||||
|
||||
# set the label of the litres field to make the user comfortable
|
||||
form.set_consumable(consumable)
|
||||
|
||||
# preinitialize the defaults with potentially existing values from a try before
|
||||
form.preinit_with_data()
|
||||
|
||||
#
|
||||
# Validate should accept same odometer on different consumables
|
||||
#
|
||||
if form.validate_on_submit():
|
||||
new_stop = Pitstop(form.odometer.data, form.litres.data, form.date.data, form.costs.data, cid)
|
||||
db.session.add(new_stop)
|
||||
vehicle.pitstops.append(new_stop)
|
||||
try:
|
||||
db.session.commit()
|
||||
db_log_add(new_stop)
|
||||
except IntegrityError:
|
||||
db.session.rollback()
|
||||
form.odometer.errors.append('Pitstop already present for %s at odometer %s km!' % (consumable.name, form.odometer.data))
|
||||
return render_template('createPitStopForm.html', form=form, vehicle=vehicle, messages=form.get_hint_messages())
|
||||
return redirect(url_for('get_pit_stops', _anchor= 'v' + str(vehicle.id)))
|
||||
|
||||
form.process()
|
||||
return render_template('createPitStopForm.html', form=form, vehicle=vehicle, messages=form.get_hint_messages())
|
||||
|
||||
|
||||
@app.route('/pitstops/delete/<int:pid>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def delete_pit_stop_form(pid):
|
||||
pitstop = Pitstop.query.filter(Pitstop.id == pid).first()
|
||||
if pitstop is None:
|
||||
return redirect(url_for('get_pit_stops'))
|
||||
vehicle = Vehicle.query.filter(Vehicle.id == pitstop.vehicle_id).first()
|
||||
if vehicle not in current_user.vehicles:
|
||||
return redirect(url_for('get_pit_stops'))
|
||||
|
||||
form = DeletePitStopForm()
|
||||
if form.validate_on_submit():
|
||||
db.session.delete(pitstop)
|
||||
db.session.commit()
|
||||
db_log_delete(pitstop)
|
||||
return redirect(url_for('get_pit_stops', _anchor='v' + str(vehicle.id)))
|
||||
|
||||
return render_template('deletePitstopForm.html', form=form, pitstop=pitstop )
|
||||
|
||||
|
||||
@app.route('/pitstops/edit/<int:pid>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def edit_pit_stop_form(pid):
|
||||
edit_pitstop = Pitstop.query.get(pid)
|
||||
if edit_pitstop is None:
|
||||
return redirect(url_for('get_pit_stops'))
|
||||
|
||||
vehicle = Vehicle.query.filter(Vehicle.id == edit_pitstop.vehicle_id).first()
|
||||
if vehicle not in current_user.vehicles:
|
||||
return redirect(url_for('get_pit_stops'))
|
||||
|
||||
last_pitstop_pos = vehicle.pitstops.index(edit_pitstop) - 1
|
||||
if last_pitstop_pos > 0:
|
||||
last_pitstop = vehicle.pitstops[last_pitstop_pos]
|
||||
else:
|
||||
last_pitstop = Pitstop(0, 0, date(1970, 1, 1), 0, 0)
|
||||
|
||||
form = EditPitstopForm()
|
||||
form.set_pitstop(last_pitstop)
|
||||
|
||||
if form.validate_on_submit():
|
||||
edit_pitstop.costs = form.costs.data
|
||||
edit_pitstop.date = form.date.data
|
||||
edit_pitstop.amount = form.litres.data
|
||||
edit_pitstop.odometer = form.odometer.data
|
||||
db.session.commit()
|
||||
db_log_update(edit_pitstop)
|
||||
return redirect(url_for('get_pit_stops', _anchor='v' + str(vehicle.id)))
|
||||
|
||||
form.odometer.default = edit_pitstop.odometer
|
||||
form.litres.default = edit_pitstop.amount
|
||||
form.date.default = edit_pitstop.date
|
||||
form.costs.default = edit_pitstop.costs
|
||||
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))
|
||||
}
|
||||
if edit_pitstop.costs is not None and edit_pitstop.costs > 0:
|
||||
messages['costs'] = 'Costs must be higher than 0.01 €.'
|
||||
return render_template('editPitStopForm.html', form=form, vehicle=vehicle, messages=messages)
|
||||
|
||||
|
||||
@app.route('/pitstops', methods=['GET'])
|
||||
@login_required
|
||||
def get_pit_stops():
|
||||
user = {
|
||||
'vehicles': []
|
||||
}
|
||||
for vehicle in current_user.vehicles:
|
||||
data = []
|
||||
for pitstop in vehicle.pitstops:
|
||||
data.append(pitstop)
|
||||
for service in vehicle.services:
|
||||
data.append(service)
|
||||
data.sort(key=pitstop_service_key)
|
||||
v = {
|
||||
'id': vehicle.id,
|
||||
'name': vehicle.name,
|
||||
'data': data
|
||||
}
|
||||
user['vehicles'].append(v)
|
||||
|
||||
return render_template('pitstops.html', user=user)
|
120
app/routes/service.py
Normal file
120
app/routes/service.py
Normal file
@ -0,0 +1,120 @@
|
||||
from flask import url_for, redirect, render_template
|
||||
from flask_security import login_required, current_user
|
||||
from datetime import date
|
||||
|
||||
from ..entities import Vehicle, Service
|
||||
from ..forms import CreateServiceForm, DeleteServiceForm, EditServiceForm, SelectVehicleForm
|
||||
from ..tools import db_log_update, db_log_delete, get_event_line_for_vehicle, get_latest_pitstop_for_vehicle
|
||||
from .. import app, db
|
||||
|
||||
|
||||
@app.route('/service/vehicle/<int:vid>/create', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def create_service_for_vehicle(vid):
|
||||
vehicle = Vehicle.query.get(vid)
|
||||
if vehicle is None or vehicle not in current_user.vehicles:
|
||||
return redirect(url_for('get_account_page'))
|
||||
|
||||
form = CreateServiceForm()
|
||||
|
||||
data = get_event_line_for_vehicle(vehicle)
|
||||
if len(data) > 0:
|
||||
form.set_pitstop(Service(data[-1].date, data[-1].odometer, vid, 0, ''))
|
||||
form.same_odometer_allowed = type(data[-1]) != Service
|
||||
else:
|
||||
form.set_pitstop(Service(date(1970, 1, 1), 0, vid, 0, ''))
|
||||
form.same_odometer_allowed = True
|
||||
|
||||
form.preinit_with_data()
|
||||
|
||||
if form.validate_on_submit():
|
||||
new_service = Service(form.date.data, form.odometer.data, vid, form.costs.data, form.description.data)
|
||||
db.session.add(new_service)
|
||||
vehicle.services.append(new_service)
|
||||
db.session.commit()
|
||||
return redirect(url_for('get_pit_stops', _anchor='v' + str(vehicle.id)))
|
||||
|
||||
form.process()
|
||||
return render_template('createServiceForm.html', form=form, vehicle=vehicle, messages=[])
|
||||
|
||||
|
||||
@app.route('/service/delete/<int:sid>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def delete_service_form(sid):
|
||||
service = Service.query.filter(Service.id == sid).first()
|
||||
if service is None:
|
||||
return redirect(url_for('get_pit_stops'))
|
||||
|
||||
vehicle = Vehicle.query.filter(Vehicle.id == service.vehicle_id).first()
|
||||
if vehicle not in current_user.vehicles:
|
||||
return redirect(url_for('get_pit_stops'))
|
||||
|
||||
form = DeleteServiceForm()
|
||||
if form.validate_on_submit():
|
||||
db.session.delete(service)
|
||||
db.session.commit()
|
||||
db_log_delete(service)
|
||||
return redirect(url_for('get_pit_stops', _anchor='v' + str(vehicle.id)))
|
||||
|
||||
return render_template('deleteServiceForm.html', form=form, service=service )
|
||||
|
||||
|
||||
@app.route('/service/edit/<int:sid>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def edit_service_form(sid):
|
||||
edit_service = Service.query.get(sid)
|
||||
if edit_service is None:
|
||||
return redirect(url_for('get_pit_stops'))
|
||||
|
||||
vehicle = Vehicle.query.filter(Vehicle.id == edit_service.vehicle_id).first()
|
||||
if vehicle not in current_user.vehicles:
|
||||
return redirect(url_for('get_pit_stops'))
|
||||
|
||||
form = EditServiceForm()
|
||||
data = get_event_line_for_vehicle(vehicle)
|
||||
data.reverse()
|
||||
if len(data) > 0:
|
||||
last_pitstop = Service(data[-1].date, data[-1].odometer, vehicle.id, 0, '')
|
||||
else:
|
||||
last_pitstop = Service(date(1970, 1, 1), 0, vehicle.id, 0, '')
|
||||
form.set_pitstop(last_pitstop)
|
||||
form.same_odometer_allowed = True
|
||||
|
||||
if form.validate_on_submit():
|
||||
edit_service.costs = form.costs.data
|
||||
edit_service.date = form.date.data
|
||||
edit_service.description = form.description.data
|
||||
edit_service.odometer = form.odometer.data
|
||||
db.session.commit()
|
||||
db_log_update(edit_service)
|
||||
return redirect(url_for('get_pit_stops', _anchor='v' + str(vehicle.id)))
|
||||
|
||||
form.odometer.default = edit_service.odometer
|
||||
form.description.default = edit_service.description
|
||||
form.date.default = edit_service.date
|
||||
form.costs.default = edit_service.costs
|
||||
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))
|
||||
}
|
||||
if edit_service.costs is not None and edit_service.costs > 0:
|
||||
messages['costs'] = 'Costs must be higher than 0.01 €.'
|
||||
return render_template('editServiceForm.html', form=form, vehicle=vehicle, messages=messages)
|
||||
|
||||
|
||||
@app.route('/service/vehicle/select', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def select_vehicle_for_new_service():
|
||||
if len(current_user.vehicles) == 1:
|
||||
return redirect(url_for('create_service_for_vehicle', vid=current_user.vehicles[0].id))
|
||||
|
||||
form = SelectVehicleForm()
|
||||
form.vehicle.choices = [(g.id, g.name) for g in current_user.vehicles]
|
||||
|
||||
if form.validate_on_submit():
|
||||
return redirect(url_for('create_service_for_vehicle', vid=form.vehicle.data))
|
||||
|
||||
return render_template('selectVehicle.html', form=form)
|
||||
|
||||
|
@ -1,9 +1,18 @@
|
||||
.markdown > h1 {
|
||||
font-size: 14px;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.markdown {
|
||||
text-align: left
|
||||
}
|
||||
|
||||
body {
|
||||
padding-top: 50px;
|
||||
}
|
||||
|
||||
.starter-template {
|
||||
padding-top: 30px;
|
||||
padding-top: 0px;
|
||||
padding-bottom: 60px;
|
||||
text-align: center;
|
||||
}
|
||||
@ -59,6 +68,14 @@ h3:after{
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.alert {
|
||||
margin-bottom:0px;
|
||||
}
|
||||
|
||||
.topspace {
|
||||
margin-top: 5px;
|
||||
}
|
||||
|
||||
// for small devices
|
||||
@media only screen
|
||||
and (min-device-width : 320px)
|
||||
|
@ -5,7 +5,7 @@
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">Password</div>
|
||||
<div class="panel-body">
|
||||
<a href='{{ url_for('security.change_password') }}' class="btn btn-primary " role="button">
|
||||
<a href="{{ url_for('security.change_password') }}" id="change_password" class="btn btn-primary " role="button">
|
||||
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> Change
|
||||
</a>
|
||||
</div>
|
||||
@ -13,7 +13,7 @@
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">Vehicles</div>
|
||||
<div class="panel-body">
|
||||
<a href="{{ url_for('create_vehicle') }}" class="btn btn-primary " role="button">
|
||||
<a href="{{ url_for('create_vehicle') }}" id="create_vehicle" class="btn btn-primary " role="button">
|
||||
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> create
|
||||
</a>
|
||||
</div>
|
||||
@ -37,14 +37,21 @@
|
||||
</td>
|
||||
<td>
|
||||
{{ vehicle.pitstops | length }} pitstops<br />
|
||||
{{ vehicle.services | length }} special expenses<br />
|
||||
{{ vehicle.consumables | length }} consumables
|
||||
</td>
|
||||
<td>
|
||||
<a href="{{ url_for('edit_vehicle', vid=vehicle.id) }}" class="btn btn-primary " role="button">
|
||||
<a href="{{ url_for('create_service_for_vehicle', vid=vehicle.id) }}" id="pitstop_{{loop.index}}" class="btn btn-primary " role="button">
|
||||
<span class="glyphicon glyphicon-wrench" aria-hidden="true"></span> add service
|
||||
</a>
|
||||
<a href="{{ url_for('select_consumable_for_new_pitstop', vid=vehicle.id) }}" id="pitstop_{{loop.index}}" class="btn btn-primary " role="button">
|
||||
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> add pitstop
|
||||
</a>
|
||||
<a href="{{ url_for('edit_vehicle', vid=vehicle.id) }}" id="edit_vehicle_{{loop.index}}" class="btn btn-primary " role="button">
|
||||
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> edit
|
||||
</a>
|
||||
{% if current_user.vehicles | length > 1 %}
|
||||
<a href="{{ url_for('delete_vehicle', vid=vehicle.id) }}" class="btn btn-primary btn-warning " role="button">
|
||||
<a href="{{ url_for('delete_vehicle', vid=vehicle.id) }}" id="delete_vehicle_{{loop.index}}" class="btn btn-primary btn-warning " role="button">
|
||||
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> delete
|
||||
</a>
|
||||
{% else %}
|
||||
@ -59,7 +66,7 @@
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">Account</div>
|
||||
<div class="panel-body">
|
||||
<a href='{{ url_for('delete_account') }}' class="btn btn-primary " role="button">
|
||||
<a href="{{ url_for('delete_account') }}" id="delete_account" class="btn btn-primary " role="button">
|
||||
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Delete
|
||||
</a>
|
||||
</div>
|
||||
|
@ -16,7 +16,7 @@
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">Consumables</div>
|
||||
<div class="panel-body">
|
||||
<a href="{{ url_for('create_consumable') }}" class="btn btn-primary " role="button">
|
||||
<a href="{{ url_for('create_consumable') }}" id="create_consumable" class="btn btn-primary " role="button">
|
||||
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> create
|
||||
</a>
|
||||
</div>
|
||||
@ -38,22 +38,16 @@
|
||||
</tr>
|
||||
{% for consumable in consumables %}
|
||||
<tr>
|
||||
<td>
|
||||
{{ consumable.name }}
|
||||
</td>
|
||||
<td>
|
||||
{{ consumable.unit }}
|
||||
</td>
|
||||
<td>
|
||||
{{ consumable.vehicles | length }} vehicles
|
||||
</td>
|
||||
<td id="name_{{loop.index}}">{{ consumable.name }}</td>
|
||||
<td id="unit_{{loop.index}}">{{ consumable.unit }}</td>
|
||||
<td id="count_{{loop.index}}">{{ consumable.vehicles | length }} vehicles</td>
|
||||
<td>
|
||||
{% if not consumable.in_use %}
|
||||
<a href="{{ url_for('delete_consumable', cid=consumable.id) }}" class="btn btn-primary btn-warning " role="button">
|
||||
<a href="{{ url_for('delete_consumable', cid=consumable.id) }}" id="delete_consumable{{loop.index}}" class="btn btn-primary btn-warning " role="button">
|
||||
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> delete
|
||||
</a>
|
||||
{% endif %}
|
||||
<a href="{{ url_for('edit_consumable', cid=consumable.id) }}" class="btn btn-primary " role="button">
|
||||
<a href="{{ url_for('edit_consumable', cid=consumable.id) }}" id="edit_consumable{{loop.index}}" class="btn btn-primary " role="button">
|
||||
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> edit
|
||||
</a>
|
||||
</td>
|
||||
|
30
app/templates/createServiceForm.html
Normal file
30
app/templates/createServiceForm.html
Normal file
@ -0,0 +1,30 @@
|
||||
{% extends "layout.html" %}
|
||||
|
||||
{% block body %}
|
||||
<div class="col-md-2" ></div>
|
||||
<div class="col-md-8">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-body">
|
||||
<h3>New Service for '{{ vehicle.name }}'</h3>
|
||||
<form class='form-horizontal' method="POST">
|
||||
{{ form.hidden_tag() }}
|
||||
{{ render_field_with_errors(form.date) }}
|
||||
<span id="{{form.date.id}}_help" class="help-block">
|
||||
{{messages['date']}}
|
||||
</span>
|
||||
{{ render_field_with_errors(form.odometer) }}
|
||||
<span id="{{form.odometer.id}}_help" class="help-block">
|
||||
{{messages['odometer']}}
|
||||
</span>
|
||||
{{ render_field_with_errors(form.costs) }}
|
||||
<span id="{{form.costs.id}}_help" class="help-block">
|
||||
{{messages['costs']}}
|
||||
</span>
|
||||
{{ render_field_with_errors(form.description) }}
|
||||
{{ render_field_with_errors(form.submit) }}
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-2" ></div>
|
||||
{% endblock %}
|
@ -5,7 +5,7 @@
|
||||
<div class="col-md-8">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-body">
|
||||
<h3>Delete vehicle '{{consumable.name}}'?</h3>
|
||||
<h3>Delete consumable '{{consumable.name}}'?</h3>
|
||||
<form class='form-horizontal' method="POST">
|
||||
{{ form.hidden_tag() }}
|
||||
{{ render_field_with_errors(form.submit) }}
|
||||
|
@ -6,7 +6,7 @@
|
||||
<div class='panel panel-default'>
|
||||
<div class='panel-body'>
|
||||
<h3>Delete pitstop?</h3>
|
||||
<table style='width: 100%'>
|
||||
<table style='width: 100%' class="table table-striped table-bordered table-condensed">
|
||||
<tr>
|
||||
<th style='text-align:right'>Date of Pitstop</th>
|
||||
<td style='text-align: left'>{{ pitstop.date }}</td>
|
||||
|
42
app/templates/deleteServiceForm.html
Normal file
42
app/templates/deleteServiceForm.html
Normal file
@ -0,0 +1,42 @@
|
||||
{% extends 'layout.html' %}
|
||||
|
||||
{% block body %}
|
||||
<div class='col-md-2' ></div>
|
||||
<div class='col-md-8'>
|
||||
<div class='panel panel-default'>
|
||||
<div class='panel-body'>
|
||||
<h3>Delete service?</h3>
|
||||
<table style='width: 100%' class="table table-striped table-bordered table-condensed">
|
||||
<tr>
|
||||
<th style='text-align:right'>Date of Pitstop</th>
|
||||
<td style='text-align: left'>{{ service.date }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th style='text-align:right'>Odometer</th>
|
||||
<td style='text-align: left'>{{ service.odometer }} km</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th style='text-align:right'>Description</th>
|
||||
<td style='text-align: left' class="markdown">{{ service.description | markdown | safe}}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th style='text-align:right'>Costs (overall)</th>
|
||||
<td style='text-align: left'>
|
||||
{% if service.costs %}
|
||||
{{service.costs}}
|
||||
{% else %}
|
||||
--
|
||||
{% endif %}
|
||||
€
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<form class='form-horizontal' method='POST'>
|
||||
{{ form.hidden_tag() }}
|
||||
{{ render_field_with_errors(form.submit) }}
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class='col-md-2' ></div>
|
||||
{% endblock %}
|
@ -5,7 +5,7 @@
|
||||
<div class="col-md-8">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-body">
|
||||
<h3>Edit consumable </h3>
|
||||
<h3>Edit consumable</h3>
|
||||
<form class='form-horizontal' method="POST">
|
||||
{{ form.hidden_tag() }}
|
||||
{{ render_field_with_errors(form.name) }}
|
||||
|
29
app/templates/editServiceForm.html
Normal file
29
app/templates/editServiceForm.html
Normal file
@ -0,0 +1,29 @@
|
||||
{% extends "layout.html" %}
|
||||
|
||||
{% block body %}
|
||||
<div class="col-md-2" ></div>
|
||||
<div class="col-md-8">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-body">
|
||||
<h3>Edit Pitstop for '{{ vehicle.name }}'</h3>
|
||||
<form class='form-horizontal' method="POST">
|
||||
{{ form.hidden_tag() }}
|
||||
{{ render_field_with_errors(form.date) }}
|
||||
<span id="{{form.date.id}}_help" class="help-block">
|
||||
{{messages['date']}}
|
||||
</span>
|
||||
{{ render_field_with_errors(form.odometer) }}
|
||||
<span id="{{form.odometer.id}}_help" class="help-block">
|
||||
{{messages['odometer']}}
|
||||
</span>
|
||||
{{ render_field_with_errors(form.description) }}
|
||||
{{ render_field_with_errors(form.costs) }}
|
||||
<span id="{{form.costs.id}}_help" class="help-block">
|
||||
{{messages['costs']}}
|
||||
</span>
|
||||
{{ render_field_with_errors(form.submit) }}
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
@ -5,7 +5,7 @@
|
||||
<div class="col-md-8">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-body">
|
||||
<h3>Edit vehicle</h3>
|
||||
<h3>Edit vehicle '{{vehicle.name}}'</h3>
|
||||
<form class='form-horizontal' method="POST">
|
||||
{{ form.hidden_tag() }}
|
||||
{{ render_field_with_errors(form.name) }}
|
||||
|
@ -1,15 +1,16 @@
|
||||
{% macro navigation() -%}
|
||||
{% if current_user.email %}
|
||||
<li><a href='{{ url_for('select_vehicle_for_new_pitstop') }}'>Create Pitstop</a></li>
|
||||
<li><a href='{{ url_for('get_statistics') }}'>Statistics</a></li>
|
||||
<li><a href='{{ url_for('get_account_page') }}'>Account</a></li>
|
||||
<li><a id='new_pitstop_link' href='{{ url_for('select_vehicle_for_new_pitstop') }}'>Create Pitstop</a></li>
|
||||
<li><a id='new_service_link' href='{{ url_for('select_vehicle_for_new_service') }}'>Create Service</a></li>
|
||||
<li><a id='statistics_limk' href='{{ url_for('get_statistics') }}'>Statistics</a></li>
|
||||
<li><a id='account_link' href='{{ url_for('get_account_page') }}'>Account</a></li>
|
||||
{% if current_user.has_role('admin') %}
|
||||
<li><a href='{{ url_for('get_admin_page') }}'>Admin</a></li>
|
||||
<li><a id='admin_link' href='{{ url_for('get_admin_page') }}'>Admin</a></li>
|
||||
{% endif %}
|
||||
<li><a href='{{ url_for('security.logout') }}'>Logout</a></li>
|
||||
<li><a id='logout_link' href='{{ url_for('security.logout') }}'>Logout</a></li>
|
||||
{% else %}
|
||||
<li><a href='{{ url_for('security.login') }}'>Login</a></li>
|
||||
<li><a href='{{ url_for('security.register') }}'>Register</a></li>
|
||||
<li><a id='login_link' href='{{ url_for('security.login') }}'>Login</a></li>
|
||||
<li><a id='register_link' href='{{ url_for('security.register') }}'>Register</a></li>
|
||||
{% endif %}
|
||||
{%- endmacro %}
|
||||
|
||||
@ -56,6 +57,8 @@
|
||||
<input class="form-control" type="number" id="{{ field.id }}" name="{{ field.id }}" value="{{ field.default|none_filter }}" step="1" aria-describedby="{{ field.id }}_help" />
|
||||
{% elif field.type == 'DecimalField' %}
|
||||
<input class="form-control" type="number" id="{{ field.id }}" name="{{ field.id }}" value="{{ field.default|none_filter }}" step="{{ 1 / 10 ** field.places}}" aria-describedby="{{ field.id }}_help" />
|
||||
{% elif field.type == 'TextAreaField' %}
|
||||
<textarea class="form-control" id="{{ field.id }}" name="{{ field.id }}">{{ field.default|none_filter }}</textarea>
|
||||
{% else %}
|
||||
{{ field(**kwargs)|safe }}
|
||||
{% endif %}
|
||||
@ -151,8 +154,23 @@
|
||||
</div><!--/.nav-collapse -->
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<div class="container">
|
||||
{% with messages = get_flashed_messages(with_categories=true) %}
|
||||
{% if messages %}
|
||||
{% for category, message in messages %}
|
||||
<div class="row topspace">
|
||||
<div class="col-md-4" ></div>
|
||||
<div class="col-md-4">
|
||||
<div class="alert alert-{{category}} alert-dismissible" role="alert">
|
||||
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
|
||||
{{ message }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-4" ></div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
<div class="container topspace">
|
||||
<div class="starter-template">
|
||||
{% block body %}
|
||||
{% endblock %}
|
||||
|
@ -1,9 +1,95 @@
|
||||
{% extends "layout.html" %}
|
||||
{% extends "layout.html" %}
|
||||
|
||||
{% macro pitstop(field, vindex, loop) -%}
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-body">
|
||||
<div style="text-align: left; font-size: 20px;">
|
||||
<span class="glyphicon glyphicon-filter" aria-hidden="true" style="border: 1px solid black; padding: 5px 5px 3px; border-radius: 5px;"></span>
|
||||
</div>
|
||||
<table class="table table-striped table-bordered table-condensed">
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<td id="vehicle_{{vindex}}_pitstop_{{loop.index}}_date">{{field.date}}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Odometer</th>
|
||||
<td id="vehicle_{{vindex}}_pitstop_{{loop.index}}_odo">{{field.odometer}} km</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>{{ field.consumable.name }}</th>
|
||||
<td id="vehicle_{{vindex}}_pitstop_{{loop.index}}_anmount">{{field.amount}} {{ field.consumable.unit }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Costs</th>
|
||||
<td id="vehicle_{{vindex}}_pitstop_{{loop.index}}_cost">
|
||||
{% if field.costs %}
|
||||
{{field.costs}} €
|
||||
{% else %}
|
||||
-- €
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
{% if loop.first %}
|
||||
<a id="vehicle_{{vindex}}_edit_pitstop_{{loop.index}}" href="{{ url_for('edit_pit_stop_form', pid=field.id) }}" class="btn btn-primary">
|
||||
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> edit
|
||||
</a>
|
||||
<a id="vehicle_{{vindex}}_delete_pitstop_{{loop.index}}" href="{{ url_for('delete_pit_stop_form', pid=field.id) }}" class="btn btn-primary btn-warning ">
|
||||
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> delete
|
||||
</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{%- endmacro %}
|
||||
|
||||
{% macro service(field, vindex, loop) -%}
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-body">
|
||||
<div style="text-align: left; font-size: 20px;">
|
||||
<span class="glyphicon glyphicon-wrench" aria-hidden="true" style="border: 1px solid black; padding: 5px 5px 3px; border-radius: 5px;"></span>
|
||||
</div>
|
||||
<table class="table table-striped table-bordered table-condensed">
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<td id="vehicle_{{vindex}}_pitstop_{{loop.index}}_date">{{field.date}}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Odometer</th>
|
||||
<td id="vehicle_{{vindex}}_pitstop_{{loop.index}}_desc">{{field.odometer}} km</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Description</th>
|
||||
<td id="vehicle_{{vindex}}_pitstop_{{loop.index}}_anmount" class="markdown">{{field.description | markdown | safe}}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Costs</th>
|
||||
<td id="vehicle_{{vindex}}_pitstop_{{loop.index}}_cost">
|
||||
{% if field.costs %}
|
||||
{{field.costs}} €
|
||||
{% else %}
|
||||
-- €
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
{% if loop.first %}
|
||||
<a id="vehicle_{{vindex}}_edit_pitstop_{{loop.index}}" href="{{ url_for('edit_service_form', sid=field.id) }}" class="btn btn-primary">
|
||||
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> edit
|
||||
</a>
|
||||
<a id="vehicle_{{vindex}}_delete_pitstop_{{loop.index}}" href="{{ url_for('delete_service_form', sid=field.id) }}" class="btn btn-primary btn-warning ">
|
||||
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> delete
|
||||
</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{%- endmacro %}
|
||||
|
||||
|
||||
{% block body %}
|
||||
<div class="col-md-2" ></div>
|
||||
<div class="col-md-8">
|
||||
<ul id="tabs" class="nav nav-tabs" data-tabs="tabs">
|
||||
{% for vehicle in current_user.vehicles %}
|
||||
{% for vehicle in user.vehicles %}
|
||||
<li {% if loop.first %}class="active" {%endif %}>
|
||||
<a href="#v{{vehicle.id}}" id="i{{vehicle.id}}" data-toggle="tab">
|
||||
{{ vehicle.name }}
|
||||
@ -12,47 +98,18 @@
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<div id="my-tab-content" class="tab-content">
|
||||
{% for vehicle in current_user.vehicles %}
|
||||
{% for vehicle in user.vehicles %}
|
||||
{% set vehicleloop = loop %}
|
||||
<div class="tab-pane {% if loop.first %}active{% endif %}" id="v{{vehicle.id}}">
|
||||
<h3>{{vehicle.name}}</h3>
|
||||
{% if vehicle.pitstops %}
|
||||
{% for pitstop in vehicle.pitstops|reverse %}
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-body">
|
||||
<table class="table table-striped table-bordered table-condensed">
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<td>{{pitstop.date}}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Odometer</th>
|
||||
<td>{{pitstop.odometer}} km</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>{{ pitstop.consumable.name }}</th>
|
||||
<td>{{pitstop.amount}} {{ pitstop.consumable.unit }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Costs</th>
|
||||
<td>
|
||||
{% if pitstop.costs %}
|
||||
{{pitstop.costs}} €
|
||||
{% else %}
|
||||
-- €
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
{% if loop.first %}
|
||||
<a href="{{ url_for('edit_pit_stop_form', pid=pitstop.id) }}" class="btn btn-primary">
|
||||
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> edit
|
||||
</a>
|
||||
<a href="{{ url_for('delete_pit_stop_form', pid=pitstop.id) }}" class="btn btn-primary btn-warning ">
|
||||
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> delete
|
||||
</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{% if vehicle.data %}
|
||||
{% for data in vehicle.data|reverse %}
|
||||
{% if 'Pitstop' in data.__class__.__name__ %}
|
||||
{{ pitstop(data, vehicleloop.index, loop) }}
|
||||
{% endif %}
|
||||
{% if 'Service' in data.__class__.__name__ %}
|
||||
{{ service(data, vehicleloop.index, loop) }}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<div class="alert alert-warning" role="alert">
|
||||
|
24
app/tools.py
24
app/tools.py
@ -49,16 +49,17 @@ class VehicleStats:
|
||||
for consumable in vehicle.consumables:
|
||||
self.consumables.append(ConsumableStats(vehicle, consumable))
|
||||
|
||||
pitstop_count = len(vehicle.pitstops)
|
||||
events = get_event_line_for_vehicle(vehicle)
|
||||
pitstop_count = len(events)
|
||||
|
||||
if pitstop_count > 0:
|
||||
for pitstop in vehicle.pitstops:
|
||||
self.odometers.append(StatsEvent(pitstop.date, pitstop.odometer))
|
||||
if pitstop.costs is not None:
|
||||
for pitstop in events:
|
||||
self.odometers.append(StatsEvent(pitstop.date, pitstop.odometer))
|
||||
if pitstop.costs is not None:
|
||||
self.overall_costs += pitstop.costs
|
||||
|
||||
if pitstop_count > 1:
|
||||
self.overall_distance = vehicle.pitstops[-1].odometer - vehicle.pitstops[0].odometer
|
||||
self.overall_distance = events[-1].odometer - events[0].odometer
|
||||
|
||||
|
||||
class StatsEvent:
|
||||
@ -175,3 +176,16 @@ def compute_lower_limits_for_new_pitstop(latest_pitstop, last_pitstop_consumable
|
||||
# # No existing pitstop at all: insert fake data
|
||||
# return Pitstop(0, 0, date(1970, 1, 1), 0, None)
|
||||
|
||||
|
||||
def pitstop_service_key(x):
|
||||
return x.odometer, x.date
|
||||
|
||||
|
||||
def get_event_line_for_vehicle(vehicle):
|
||||
data = []
|
||||
for pitstop in vehicle.pitstops:
|
||||
data.append(pitstop)
|
||||
for service in vehicle.services:
|
||||
data.append(service)
|
||||
data.sort(key=pitstop_service_key)
|
||||
return data
|
@ -17,6 +17,7 @@ class Config:
|
||||
MAIL_USE_SSL = False
|
||||
MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
|
||||
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
|
||||
ADMIN_MAIL = 'joachim@lusiardi.de'
|
||||
|
||||
@staticmethod
|
||||
def init_app(app):
|
||||
@ -26,7 +27,7 @@ class Config:
|
||||
class DevelopmentConfig(Config):
|
||||
SECURITY_SEND_REGISTER_EMAIL = False
|
||||
DEBUG = True
|
||||
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:%s@database/pitstops' % (os.environ['DATABASE_ENV_MYSQL_ROOT_PASSWORD'])
|
||||
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:%s@database/pitstops' % (os.environ.get('DATABASE_ENV_MYSQL_ROOT_PASSWORD'))
|
||||
|
||||
|
||||
class TestingConfig(Config):
|
||||
|
11
database_upgrades/upgrade_freckles_to_gillian.sql
Normal file
11
database_upgrades/upgrade_freckles_to_gillian.sql
Normal file
@ -0,0 +1,11 @@
|
||||
CREATE TABLE IF NOT EXISTS `service` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`date` date DEFAULT NULL,
|
||||
`odometer` int(11) DEFAULT NULL,
|
||||
`vehicle_id` int(11) DEFAULT NULL,
|
||||
`costs` decimal(10,2) DEFAULT NULL,
|
||||
`description` varchar(4096) DEFAULT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `vehicle_id` (`vehicle_id`),
|
||||
CONSTRAINT `service_ibfk_1` FOREIGN KEY (`vehicle_id`) REFERENCES `vehicle` (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=latin1;
|
@ -2,19 +2,9 @@ version: '2'
|
||||
services:
|
||||
rollerverbrauch:
|
||||
build: .
|
||||
depends_on:
|
||||
- database
|
||||
volumes:
|
||||
- ./compose_config/:/config
|
||||
environment:
|
||||
- config=/config/config.py
|
||||
- DATABASE_ENV_MYSQL_ROOT_PASSWORD=foobar123
|
||||
- FLASK_CONFIG=testing
|
||||
ports:
|
||||
- 5000
|
||||
database:
|
||||
image: mysql
|
||||
environment:
|
||||
- MYSQL_ROOT_PASSWORD=foobar123
|
||||
- MYSQL_DATABASE=pitstops
|
||||
ports:
|
||||
- 3306
|
||||
- 127.0.0.1:5000:5000
|
||||
|
@ -3,3 +3,4 @@ Flask-SQLAlchemy
|
||||
Flask-Security
|
||||
Flask-WTF
|
||||
PyMySQL
|
||||
markdown
|
||||
|
12
tests/Dockerfile
Normal file
12
tests/Dockerfile
Normal file
@ -0,0 +1,12 @@
|
||||
FROM debian8_python3
|
||||
|
||||
COPY app/requirements.txt /requirements.txt
|
||||
RUN pip3 install -r /requirements.txt; \
|
||||
mkdir /data
|
||||
|
||||
ADD app /app
|
||||
|
||||
VOLUME ["/data"]
|
||||
VOLUME ["/app/config]
|
||||
EXPOSE 5000
|
||||
ENTRYPOINT python3 /app/main.py
|
137
tests/app/main.py
Normal file
137
tests/app/main.py
Normal file
@ -0,0 +1,137 @@
|
||||
import unittest
|
||||
import inspect
|
||||
from selenium import webdriver
|
||||
from selenium.webdriver.common.keys import Keys
|
||||
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
|
||||
from selenium.webdriver.common.by import By
|
||||
from selenium.webdriver.support.ui import WebDriverWait
|
||||
from selenium.webdriver.support import expected_conditions
|
||||
|
||||
|
||||
def wait_net_service(server, port, timeout=None):
|
||||
""" Wait for network service to appear
|
||||
@param timeout: in seconds, if None or 0 wait forever
|
||||
@return: True of False, if timeout is None may return only True or
|
||||
throw unhandled network exception
|
||||
"""
|
||||
import socket
|
||||
import errno
|
||||
|
||||
s = socket.socket()
|
||||
if timeout:
|
||||
from time import time as now
|
||||
# time module is needed to calc timeout shared between two exceptions
|
||||
end = now() + timeout
|
||||
|
||||
while True:
|
||||
try:
|
||||
if timeout:
|
||||
next_timeout = end - now()
|
||||
if next_timeout < 0:
|
||||
return False
|
||||
else:
|
||||
s.settimeout(next_timeout)
|
||||
|
||||
s.connect((server, port))
|
||||
|
||||
except socket.timeout as err:
|
||||
# this exception occurs only if timeout is set
|
||||
if timeout:
|
||||
return False
|
||||
|
||||
except socket.error as err:
|
||||
# catch timeout exception from underlying network library
|
||||
# this one is different from socket.timeout
|
||||
if type(err.args) != tuple or err[0] != errno.ETIMEDOUT:
|
||||
raise
|
||||
else:
|
||||
s.close()
|
||||
return True
|
||||
|
||||
|
||||
class BaseTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.screen_shot_counter = 0
|
||||
self.driver = webdriver.Remote(
|
||||
command_executor='http://selenium:4444/wd/hub',
|
||||
desired_capabilities=DesiredCapabilities.FIREFOX)
|
||||
self.driver.get("http://rollerverbrauch:5000")
|
||||
WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "email")))
|
||||
|
||||
def create_screenshot(self):
|
||||
self.driver.get_screenshot_as_file(
|
||||
'/results/%s_%s_%s.png' % (self.__class__.__name__, inspect.stack()[1][3], str(self.screen_shot_counter)))
|
||||
self.screen_shot_counter += 1
|
||||
|
||||
def tearDown(self):
|
||||
self.driver.close()
|
||||
|
||||
|
||||
class RegisterCheck(BaseTestCase):
|
||||
def setUp(self):
|
||||
self.screen_shot_counter = 0
|
||||
self.driver = webdriver.Remote(
|
||||
command_executor='http://selenium:4444/wd/hub',
|
||||
desired_capabilities=DesiredCapabilities.FIREFOX)
|
||||
self.driver.get("http://rollerverbrauch:5000")
|
||||
WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "email")))
|
||||
|
||||
def test_page_loads(self):
|
||||
self.driver.get("http://rollerverbrauch:5000")
|
||||
self.create_screenshot()
|
||||
self.assertIn("refuel", self.driver.title, "Title must contain reload")
|
||||
|
||||
def test_can_register(self):
|
||||
self.driver.get("http://rollerverbrauch:5000")
|
||||
self.driver.find_element_by_partial_link_text('Register').click()
|
||||
self.create_screenshot()
|
||||
WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "submit")))
|
||||
self.create_screenshot()
|
||||
self.driver.find_element_by_id('email').send_keys('test@test.com')
|
||||
self.driver.find_element_by_id('password').send_keys('test123')
|
||||
self.driver.find_element_by_id('password_confirm').send_keys('test123')
|
||||
self.create_screenshot()
|
||||
self.driver.find_element_by_id('submit').click()
|
||||
WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "i1")))
|
||||
self.create_screenshot()
|
||||
|
||||
def test_register_must_repeat_pwd(self):
|
||||
self.driver.get("http://rollerverbrauch:5000")
|
||||
self.driver.find_element_by_partial_link_text('Register').click()
|
||||
self.create_screenshot()
|
||||
WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "submit")))
|
||||
self.create_screenshot()
|
||||
self.driver.find_element_by_id('email').send_keys('test1@test.com')
|
||||
self.driver.find_element_by_id('password').send_keys('test123')
|
||||
self.create_screenshot()
|
||||
self.driver.find_element_by_id('submit').click()
|
||||
self.create_screenshot()
|
||||
error = self.driver.find_elements_by_class_name('error')
|
||||
self.assertIsNotNone(error[0], 'we expect an error')
|
||||
self.assertIn('Passwords do not match', error[0].text, 'wrong error message')
|
||||
|
||||
def test_register_must_be_equal_pwd(self):
|
||||
self.driver.get("http://rollerverbrauch:5000")
|
||||
self.driver.find_element_by_partial_link_text('Register').click()
|
||||
self.create_screenshot()
|
||||
WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "submit")))
|
||||
self.create_screenshot()
|
||||
self.driver.find_element_by_id('email').send_keys('test1@test.com')
|
||||
self.driver.find_element_by_id('password').send_keys('test123')
|
||||
self.driver.find_element_by_id('password_confirm').send_keys('test1234')
|
||||
self.create_screenshot()
|
||||
self.driver.find_element_by_id('submit').click()
|
||||
self.create_screenshot()
|
||||
error = self.driver.find_elements_by_class_name('error')
|
||||
self.assertIsNotNone(error[0], 'we expect an error')
|
||||
self.assertIn('Passwords do not match', error[0].text, 'wrong error message')
|
||||
|
||||
|
||||
class LoginCheck(BaseTestCase):
|
||||
|
||||
def can_login(self):
|
||||
pass
|
||||
|
||||
if __name__ == "__main__":
|
||||
wait_net_service('selenium', 4444)
|
||||
unittest.main()
|
1
tests/app/requirements.txt
Normal file
1
tests/app/requirements.txt
Normal file
@ -0,0 +1 @@
|
||||
selenium
|
16
tests/compose_config/config.py
Normal file
16
tests/compose_config/config.py
Normal file
@ -0,0 +1,16 @@
|
||||
import os
|
||||
|
||||
#SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:%s@database/pitstops' % (os.environ['DATABASE_ENV_MYSQL_ROOT_PASSWORD'])
|
||||
SQLALCHEMY_DATABASE_URI = 'sqlite:////data/rollerverbrauch.db'
|
||||
|
||||
MAIL_SERVER = ''
|
||||
MAIL_PORT= 25
|
||||
MAIL_USE_TLS = True
|
||||
MAIL_USE_SSL = False
|
||||
MAIL_USERNAME = ''
|
||||
MAIL_PASSWORD = ''
|
||||
SECURITY_EMAIL_SENDER = ''
|
||||
SECURITY_PASSWORD_SALT = 'SecretSalt'
|
||||
SECRET_KEY = 'SecretKey'
|
||||
|
||||
SECURITY_SEND_REGISTER_EMAIL = False
|
22
tests/docker-compose.yml
Normal file
22
tests/docker-compose.yml
Normal file
@ -0,0 +1,22 @@
|
||||
version: '2'
|
||||
services:
|
||||
tests:
|
||||
build: .
|
||||
depends_on:
|
||||
- selenium
|
||||
volumes:
|
||||
- ./results:/results
|
||||
selenium:
|
||||
image: selenium/standalone-firefox
|
||||
depends_on:
|
||||
- rollerverbrauch
|
||||
rollerverbrauch:
|
||||
build: ..
|
||||
volumes:
|
||||
- ./compose_config/:/config
|
||||
environment:
|
||||
- config=/config/config.py
|
||||
- DATABASE_ENV_MYSQL_ROOT_PASSWORD=foobar123
|
||||
ports:
|
||||
- 5000
|
||||
|
7
tests/run_test.sh
Executable file
7
tests/run_test.sh
Executable file
@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
|
||||
rm -f results/*
|
||||
docker-compose build
|
||||
docker-compose up --abort-on-container-exit
|
||||
docker-compose down
|
||||
docker-compose rm --all
|
Loading…
Reference in New Issue
Block a user