This commit is contained in:
parent
af5d4ae6b3
commit
43e3f9c98b
|
@ -256,36 +256,42 @@ def create_pit_stop_form(vid, cid):
|
||||||
if consumable not in vehicle.consumables:
|
if consumable not in vehicle.consumables:
|
||||||
return redirect(url_for('select_consumable_for_new_pitstop', vid=vid))
|
return redirect(url_for('select_consumable_for_new_pitstop', vid=vid))
|
||||||
|
|
||||||
# the last pitstop is required to be able to check the monotonicy of date and odometer
|
|
||||||
|
|
||||||
if len(vehicle.pitstops) > 0:
|
|
||||||
last_pitstop = vehicle.pitstops[-1]
|
|
||||||
else:
|
|
||||||
last_pitstop = Pitstop(0, 0, date(1970, 1, 1), 0, None)
|
|
||||||
|
|
||||||
form = CreatePitstopForm()
|
form = CreatePitstopForm()
|
||||||
form.set_pitstop(last_pitstop)
|
|
||||||
form.litres.label = '%s (%s)' % (consumable.name, consumable.unit)
|
|
||||||
|
|
||||||
|
# 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():
|
if form.validate_on_submit():
|
||||||
new_stop = Pitstop(form.odometer.data, form.litres.data, form.date.data, form.costs.data, cid)
|
new_stop = Pitstop(form.odometer.data, form.litres.data, form.date.data, form.costs.data, cid)
|
||||||
db.session.add(new_stop)
|
db.session.add(new_stop)
|
||||||
vehicle.pitstops.append(new_stop)
|
vehicle.pitstops.append(new_stop)
|
||||||
|
try:
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
tools.db_log_add(new_stop)
|
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)))
|
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.costs.default = last_pitstop.costs
|
|
||||||
form.process()
|
form.process()
|
||||||
messages = {
|
return render_template('createPitStopForm.html', form=form, vehicle=vehicle, messages=form.get_hint_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)),
|
|
||||||
'costs': 'Costs must be higher than 0.01 €.'
|
|
||||||
}
|
|
||||||
return render_template('createPitStopForm.html', form=form, vehicle=vehicle, messages=messages)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/pitstops/delete/<int:pid>', methods=['GET', 'POST'])
|
@app.route('/pitstops/delete/<int:pid>', methods=['GET', 'POST'])
|
||||||
|
@ -311,9 +317,10 @@ def delete_pit_stop_form(pid):
|
||||||
@app.route('/pitstops/edit/<int:pid>', methods=['GET', 'POST'])
|
@app.route('/pitstops/edit/<int:pid>', methods=['GET', 'POST'])
|
||||||
@login_required
|
@login_required
|
||||||
def edit_pit_stop_form(pid):
|
def edit_pit_stop_form(pid):
|
||||||
edit_pitstop = Pitstop.query.filter(Pitstop.id == pid).first()
|
edit_pitstop = Pitstop.query.get(pid).first()
|
||||||
if edit_pitstop is None:
|
if edit_pitstop is None:
|
||||||
return redirect(url_for('get_pit_stops'))
|
return redirect(url_for('get_pit_stops'))
|
||||||
|
|
||||||
vehicle = Vehicle.query.filter(Vehicle.id == edit_pitstop.vehicle_id).first()
|
vehicle = Vehicle.query.filter(Vehicle.id == edit_pitstop.vehicle_id).first()
|
||||||
if vehicle not in current_user.vehicles:
|
if vehicle not in current_user.vehicles:
|
||||||
return redirect(url_for('get_pit_stops'))
|
return redirect(url_for('get_pit_stops'))
|
||||||
|
|
|
@ -11,6 +11,9 @@ vehicles_consumables = db.Table('vehicles_consumables',
|
||||||
|
|
||||||
|
|
||||||
class Role(db.Model, RoleMixin):
|
class Role(db.Model, RoleMixin):
|
||||||
|
"""
|
||||||
|
Entity to handle different roles for users: Typically user and admin exist
|
||||||
|
"""
|
||||||
id = db.Column(db.Integer(), primary_key=True)
|
id = db.Column(db.Integer(), primary_key=True)
|
||||||
name = db.Column(db.String(80), unique=True)
|
name = db.Column(db.String(80), unique=True)
|
||||||
description = db.Column(db.String(255))
|
description = db.Column(db.String(255))
|
||||||
|
@ -23,11 +26,15 @@ class Role(db.Model, RoleMixin):
|
||||||
|
|
||||||
|
|
||||||
class User(db.Model, UserMixin):
|
class User(db.Model, UserMixin):
|
||||||
|
"""
|
||||||
|
Entity to represent a user including login data and links to roles and vehicles.
|
||||||
|
"""
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
email = db.Column(db.String(255), unique=True)
|
email = db.Column(db.String(255), unique=True)
|
||||||
password = db.Column(db.String(255))
|
password = db.Column(db.String(255))
|
||||||
active = db.Column(db.Boolean())
|
active = db.Column(db.Boolean())
|
||||||
confirmed_at = db.Column(db.DateTime())
|
confirmed_at = db.Column(db.DateTime())
|
||||||
|
|
||||||
vehicles = db.relationship(
|
vehicles = db.relationship(
|
||||||
'Vehicle'
|
'Vehicle'
|
||||||
)
|
)
|
||||||
|
@ -42,6 +49,14 @@ class User(db.Model, UserMixin):
|
||||||
|
|
||||||
|
|
||||||
class Vehicle(db.Model):
|
class Vehicle(db.Model):
|
||||||
|
"""
|
||||||
|
Entity to represent a vehicle.
|
||||||
|
Attributes:
|
||||||
|
* name of the vehilce
|
||||||
|
* the id of the owner
|
||||||
|
* list of pitstops
|
||||||
|
* list of possible consumables
|
||||||
|
"""
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
owner_id = db.Column(db.Integer, db.ForeignKey('user.id'))
|
owner_id = db.Column(db.Integer, db.ForeignKey('user.id'))
|
||||||
name = db.Column(db.String(255))
|
name = db.Column(db.String(255))
|
||||||
|
@ -52,7 +67,10 @@ class Vehicle(db.Model):
|
||||||
'Consumable',
|
'Consumable',
|
||||||
secondary=vehicles_consumables
|
secondary=vehicles_consumables
|
||||||
)
|
)
|
||||||
__table_args__ = (db.UniqueConstraint('owner_id', 'name', name='_owner_name_uniq'),)
|
# allow vehicle names to be duplicated between different owners but must still be uniq for each owner
|
||||||
|
__table_args__ = (db.UniqueConstraint('owner_id',
|
||||||
|
'name',
|
||||||
|
name='_owner_name_uniq'),)
|
||||||
|
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
@ -62,31 +80,55 @@ class Vehicle(db.Model):
|
||||||
|
|
||||||
|
|
||||||
class Pitstop(db.Model):
|
class Pitstop(db.Model):
|
||||||
|
"""
|
||||||
|
Entity to represent a pitstop for a single consumable.
|
||||||
|
Attributes:
|
||||||
|
* the date of the pitstop
|
||||||
|
* the odometer of the pitstop
|
||||||
|
* the id of the fuelled consumable
|
||||||
|
* amount of consumable used
|
||||||
|
* the costs of the consumable
|
||||||
|
* the id of the vehicle that was refuelled
|
||||||
|
"""
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
date = db.Column(db.Date)
|
date = db.Column(db.Date)
|
||||||
odometer = db.Column(db.Integer)
|
odometer = db.Column(db.Integer)
|
||||||
consumable_id = db.Column(db.Integer, db.ForeignKey('consumable.id'))
|
consumable_id = db.Column(db.Integer, db.ForeignKey('consumable.id'))
|
||||||
consumable = db.relationship('Consumable')
|
amount = db.Column(db.Numeric(5, 2))
|
||||||
litres = db.Column(db.Numeric(5, 2))
|
|
||||||
costs = db.Column(db.Numeric(5, 2), default=0)
|
costs = db.Column(db.Numeric(5, 2), default=0)
|
||||||
vehicle_id = db.Column(db.Integer, db.ForeignKey('vehicle.id'))
|
vehicle_id = db.Column(db.Integer, db.ForeignKey('vehicle.id'))
|
||||||
|
# short cut to access the fuelled consumable of the pitstop
|
||||||
|
consumable = db.relationship('Consumable')
|
||||||
|
# this uniqueness constraint makes sure that for each consumable and each vehicle only one pitstop exists at the
|
||||||
|
# same odometer
|
||||||
|
__table_args__ = (db.UniqueConstraint('odometer',
|
||||||
|
'consumable_id',
|
||||||
|
'vehicle_id',
|
||||||
|
name='_odometer_consumable_vehicle_uniq'),)
|
||||||
|
|
||||||
def __init__(self, odometer, litres, date, costs, consumable_id):
|
def __init__(self, odometer, amount, date, costs, consumable_id):
|
||||||
self.odometer = odometer
|
self.odometer = odometer
|
||||||
self.litres = litres
|
self.amount = amount
|
||||||
self.date = date
|
self.date = date
|
||||||
self.costs = costs
|
self.costs = costs
|
||||||
self.consumable_id = consumable_id
|
self.consumable_id = consumable_id
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<Pitstop odometer="%r" litres="%r" date="%r" vehicle_id="%r" consumable_id="%r">' % \
|
return '<Pitstop odometer="%r" amount="%r" date="%r" vehicle_id="%r" consumable_id="%r">' % \
|
||||||
(self.odometer, self.litres, self.date, self.vehicle_id, self.consumable_id)
|
(self.odometer, self.amount, self.date, self.vehicle_id, self.consumable_id)
|
||||||
|
|
||||||
|
|
||||||
class Consumable(db.Model):
|
class Consumable(db.Model):
|
||||||
|
"""
|
||||||
|
Entity to represent a material that be consumed by a vehilce.
|
||||||
|
Attributes:
|
||||||
|
* name (must be globally unique)
|
||||||
|
* unit
|
||||||
|
"""
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
name = db.Column(db.String(255), unique=True)
|
name = db.Column(db.String(255), unique=True)
|
||||||
unit = db.Column(db.String(255))
|
unit = db.Column(db.String(255))
|
||||||
|
|
||||||
vehicles = db.relationship(
|
vehicles = db.relationship(
|
||||||
'Vehicle',
|
'Vehicle',
|
||||||
secondary=vehicles_consumables
|
secondary=vehicles_consumables
|
||||||
|
|
|
@ -5,6 +5,13 @@ from datetime import date
|
||||||
|
|
||||||
|
|
||||||
def date_check(form, field):
|
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:
|
if field.data < form.last_pitstop.date:
|
||||||
raise ValidationError('The new date must not be before %s' % form.last_pitstop.date)
|
raise ValidationError('The new date must not be before %s' % form.last_pitstop.date)
|
||||||
if field.data > date.today():
|
if field.data > date.today():
|
||||||
|
@ -12,7 +19,15 @@ def date_check(form, field):
|
||||||
|
|
||||||
|
|
||||||
def odometer_check(form, field):
|
def odometer_check(form, field):
|
||||||
if field.data <= form.last_pitstop.odometer:
|
"""
|
||||||
|
|
||||||
|
: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)
|
raise ValidationError('The new odometer value must be higher than %i km' % form.last_pitstop.odometer)
|
||||||
|
|
||||||
|
|
||||||
|
@ -27,6 +42,12 @@ def costs_check(form, field):
|
||||||
|
|
||||||
|
|
||||||
def edit_costs_check(form, field):
|
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)
|
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:
|
if costs_check_required and field.data is not None and field.data <= 0:
|
||||||
raise ValidationError('Costs must be above 0.01 €.')
|
raise ValidationError('Costs must be above 0.01 €.')
|
||||||
|
@ -49,10 +70,44 @@ class CreatePitstopForm(Form):
|
||||||
costs = DecimalField('Costs (€, overall)', places=2, validators=[costs_check])
|
costs = DecimalField('Costs (€, overall)', places=2, validators=[costs_check])
|
||||||
submit = SubmitField(label='Do it!')
|
submit = SubmitField(label='Do it!')
|
||||||
last_pitstop = None
|
last_pitstop = None
|
||||||
|
same_odometer_allowed = True
|
||||||
|
|
||||||
def set_pitstop(self, last_pitstop):
|
def set_pitstop(self, last_pitstop):
|
||||||
self.last_pitstop = 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 = self.last_pitstop.date
|
||||||
|
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):
|
class EditVehicleForm(Form):
|
||||||
name = StringField('Name', validators=[Length(1, 255)])
|
name = StringField('Name', validators=[Length(1, 255)])
|
||||||
|
@ -79,10 +134,24 @@ class EditPitstopForm(Form):
|
||||||
costs = DecimalField('Costs (€, overall)', places=2, validators=[edit_costs_check])
|
costs = DecimalField('Costs (€, overall)', places=2, validators=[edit_costs_check])
|
||||||
submit = SubmitField(label='Update it!')
|
submit = SubmitField(label='Update it!')
|
||||||
last_pitstop = None
|
last_pitstop = None
|
||||||
|
same_odometer_allowed = True
|
||||||
|
|
||||||
def set_pitstop(self, last_pitstop):
|
def set_pitstop(self, last_pitstop):
|
||||||
self.last_pitstop = 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):
|
class CreateConsumableForm(Form):
|
||||||
name = StringField('Name', validators=[Length(1, 255)])
|
name = StringField('Name', validators=[Length(1, 255)])
|
||||||
|
|
|
@ -26,4 +26,5 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="col-md-2" ></div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
{% extends "layout.html" %}
|
{% extends "layout.html" %}
|
||||||
{% block body %}
|
{% block body %}
|
||||||
<div id="content">
|
<div class="col-md-2" ></div>
|
||||||
|
<div class="col-md-8">
|
||||||
<ul id="tabs" class="nav nav-tabs" data-tabs="tabs">
|
<ul id="tabs" class="nav nav-tabs" data-tabs="tabs">
|
||||||
{% for vehicle in current_user.vehicles %}
|
{% for vehicle in current_user.vehicles %}
|
||||||
<li {% if loop.first %}class="active" {%endif %}>
|
<li {% if loop.first %}class="active" {%endif %}>
|
||||||
|
@ -15,96 +16,38 @@
|
||||||
<div class="tab-pane {% if loop.first %}active{% endif %}" id="v{{vehicle.id}}">
|
<div class="tab-pane {% if loop.first %}active{% endif %}" id="v{{vehicle.id}}">
|
||||||
<h3>{{vehicle.name}}</h3>
|
<h3>{{vehicle.name}}</h3>
|
||||||
{% if vehicle.pitstops %}
|
{% if vehicle.pitstops %}
|
||||||
<div class="table-responsive">
|
{% for pitstop in vehicle.pitstops|reverse %}
|
||||||
|
<div class="panel panel-default">
|
||||||
|
<div class="panel-body">
|
||||||
<table class="table table-striped table-bordered table-condensed">
|
<table class="table table-striped table-bordered table-condensed">
|
||||||
<tr>
|
<tr>
|
||||||
<th>
|
<th>Date</th>
|
||||||
Date<br/>
|
<td>{{pitstop.date}}</td>
|
||||||
Days
|
|
||||||
</th>
|
|
||||||
<th>
|
|
||||||
Odometer<br/>
|
|
||||||
Distance
|
|
||||||
</th>
|
|
||||||
<th>
|
|
||||||
Litres<br/>
|
|
||||||
Average
|
|
||||||
</th>
|
|
||||||
<th>
|
|
||||||
Costs<br />
|
|
||||||
Costs per Litre
|
|
||||||
</th>
|
|
||||||
</tr>
|
</tr>
|
||||||
{% for pitstop in vehicle.pitstops|reverse %}
|
<tr>
|
||||||
{% if not loop.last %}
|
<th>Odometer</th>
|
||||||
{% set days = (pitstop.date - vehicle.pitstops[vehicle.pitstops|length - loop.index - 1].date).days %}
|
<td>{{pitstop.odometer}} km</td>
|
||||||
{% set distance = pitstop.odometer - vehicle.pitstops[vehicle.pitstops|length - loop.index - 1].odometer %}
|
|
||||||
{% set average = (pitstop.litres / distance) * 100 %}
|
|
||||||
<tr class='pitstop'>
|
|
||||||
<td>
|
|
||||||
{{pitstop.date}}<br/>
|
|
||||||
{{ days }} days
|
|
||||||
</td>
|
|
||||||
<td>
|
|
||||||
{{pitstop.odometer}} km<br/>
|
|
||||||
{{distance}} km
|
|
||||||
</td>
|
|
||||||
<td>
|
|
||||||
{{pitstop.litres}} l<br/>
|
|
||||||
{{average | round(2)}} l/100km
|
|
||||||
</td>
|
|
||||||
<td>
|
|
||||||
{{pitstop.costs}} €<br />
|
|
||||||
{{ (pitstop.costs / pitstop.litres) | round(2) }} €/l
|
|
||||||
</td>
|
|
||||||
</tr>
|
</tr>
|
||||||
{% if loop.first %}
|
<tr>
|
||||||
<tr class='pitstop'>
|
<th>{{ pitstop.consumable.name }}</th>
|
||||||
<td colspan='4'>
|
<td>{{pitstop.amount}} {{ pitstop.consumable.unit }}</td>
|
||||||
<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>
|
|
||||||
</td>
|
|
||||||
</tr>
|
</tr>
|
||||||
{% endif %}
|
<tr>
|
||||||
{% else %}
|
<th>Costs</th>
|
||||||
<tr class='pitstop'>
|
<td>{{pitstop.costs}} €</td>
|
||||||
<td>
|
|
||||||
{{pitstop.date}}<br/>
|
|
||||||
-- days
|
|
||||||
</td>
|
|
||||||
<td>
|
|
||||||
{{pitstop.odometer}} km<br/>
|
|
||||||
-- km
|
|
||||||
</td>
|
|
||||||
<td>
|
|
||||||
{{pitstop.litres}} l<br/>
|
|
||||||
-- l/100km
|
|
||||||
</td>
|
|
||||||
<td>
|
|
||||||
{{pitstop.costs}} €<br />
|
|
||||||
{{ (pitstop.costs / pitstop.litres) | round(2) }} €/l
|
|
||||||
</td>
|
|
||||||
</tr>
|
</tr>
|
||||||
{% if loop.first %}
|
|
||||||
<tr class='pitstop'>
|
|
||||||
<td colspan='4'>
|
|
||||||
<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>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
{% endif %}
|
|
||||||
{% endif %}
|
|
||||||
{% endfor %}
|
|
||||||
</table>
|
</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>
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
{% else %}
|
{% else %}
|
||||||
<div class="alert alert-warning" role="alert">
|
<div class="alert alert-warning" role="alert">
|
||||||
not enough data: <a href="{{ url_for('select_consumable_for_new_pitstop', vid=vehicle.id) }}">log a pitstop</a>?
|
not enough data: <a href="{{ url_for('select_consumable_for_new_pitstop', vid=vehicle.id) }}">log a pitstop</a>?
|
||||||
|
@ -114,6 +57,7 @@
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="col-md-2" ></div>
|
||||||
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
import logging
|
import logging
|
||||||
|
from datetime import date
|
||||||
|
from dis import code_info
|
||||||
|
|
||||||
|
from rollerverbrauch.entities import \
|
||||||
|
Pitstop
|
||||||
|
|
||||||
class VehicleStats:
|
class VehicleStats:
|
||||||
def __init__(self, vehicle):
|
def __init__(self, vehicle):
|
||||||
|
@ -83,3 +87,83 @@ def check_vehicle_name_is_unique(current_user, name_field):
|
||||||
name_field.errors.append('Vehicle "%s" already exists.' % vehicle_name)
|
name_field.errors.append('Vehicle "%s" already exists.' % vehicle_name)
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def get_latest_pitstop_for_vehicle(vehicle_id):
|
||||||
|
"""
|
||||||
|
return the latest pit stop for the vehicle with the given id.
|
||||||
|
:param vehicle_id: the id of the vehicle
|
||||||
|
:return: the latest pitstop or None if no pitstop exists
|
||||||
|
"""
|
||||||
|
latest_pitstop = Pitstop.query\
|
||||||
|
.filter(Pitstop.vehicle_id == vehicle_id)\
|
||||||
|
.order_by(Pitstop.id.desc())\
|
||||||
|
.first()
|
||||||
|
return latest_pitstop
|
||||||
|
|
||||||
|
|
||||||
|
def get_latest_pitstop_for_vehicle_and_consumable(vehicle_id, consumable_id):
|
||||||
|
"""
|
||||||
|
return the latest pit stop for the vehicle and consumable with the given ids.
|
||||||
|
:param vehicle_id: the id of the vehicle
|
||||||
|
:param consumable_id: the id of the consumable
|
||||||
|
:return: the latest pitstop or None if no pitstop exists
|
||||||
|
"""
|
||||||
|
latest_pitstop_consumable = Pitstop.query\
|
||||||
|
.filter(Pitstop.vehicle_id == vehicle_id)\
|
||||||
|
.filter(Pitstop.consumable_id == consumable_id)\
|
||||||
|
.order_by(Pitstop.id.desc())\
|
||||||
|
.first()
|
||||||
|
return latest_pitstop_consumable
|
||||||
|
|
||||||
|
|
||||||
|
def compute_lower_limits_for_new_pitstop(latest_pitstop, last_pitstop_consumable, consumable_id):
|
||||||
|
"""
|
||||||
|
This function figures out the lower limits for date and odometer of a new pitstop.
|
||||||
|
:param latest_pitstop:
|
||||||
|
:param last_pitstop_consumable:
|
||||||
|
:param consumable_id:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
odometer = 0
|
||||||
|
date_of_pitstop = date(1970, 1, 1)
|
||||||
|
amount = 0
|
||||||
|
costs = 0
|
||||||
|
|
||||||
|
if latest_pitstop is not None:
|
||||||
|
odometer = latest_pitstop.odometer
|
||||||
|
date_of_pitstop = latest_pitstop.date
|
||||||
|
|
||||||
|
if last_pitstop_consumable is not None:
|
||||||
|
amount = last_pitstop_consumable.amount
|
||||||
|
costs = last_pitstop_consumable.costs
|
||||||
|
|
||||||
|
return Pitstop(odometer, amount, date_of_pitstop, costs, consumable_id)
|
||||||
|
|
||||||
|
|
||||||
|
# if latest_pitstop is not None:
|
||||||
|
# if last_pitstop_consumable is not None and last_pitstop_consumable != latest_pitstop:
|
||||||
|
# if latest_pitstop.id > last_pitstop_consumable.id:
|
||||||
|
# return Pitstop(latest_pitstop.odometer,
|
||||||
|
# last_pitstop_consumable.amount,
|
||||||
|
# latest_pitstop.date,
|
||||||
|
# last_pitstop_consumable.costs,
|
||||||
|
# consumable_id)
|
||||||
|
# else:
|
||||||
|
# return Pitstop(last_pitstop_consumable.odometer,
|
||||||
|
# last_pitstop_consumable.amount,
|
||||||
|
# last_pitstop_consumable.date,
|
||||||
|
# last_pitstop_consumable.costs,
|
||||||
|
# consumable_id)
|
||||||
|
# else:
|
||||||
|
# # either only one pitstop exists or both are the same
|
||||||
|
# litres = 0
|
||||||
|
# costs = 0
|
||||||
|
# if latest_pitstop.consumable_id == last_pitstop_consumable.consumable_id:
|
||||||
|
# litres = latest_pitstop.amount
|
||||||
|
# costs = latest_pitstop.costs
|
||||||
|
# return Pitstop(latest_pitstop.odometer, litres, latest_pitstop.date, costs, consumable_id)
|
||||||
|
# else:
|
||||||
|
# # No existing pitstop at all: insert fake data
|
||||||
|
# return Pitstop(0, 0, date(1970, 1, 1), 0, None)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue