finished handling of services
This commit is contained in:
parent
60a1b307e4
commit
6e665b9f2f
|
@ -7,7 +7,8 @@ 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_latest_pitstop_for_vehicle_and_consumable, compute_lower_limits_for_new_pitstop, pitstop_service_key, \
|
||||
get_event_line_for_vehicle
|
||||
from .. import app, db
|
||||
|
||||
|
||||
|
@ -62,15 +63,14 @@ def create_pit_stop_form(vid, cid):
|
|||
|
||||
form = CreatePitstopForm()
|
||||
|
||||
# the last pitstop is required to be able to check the monotonicy of date and odometer
|
||||
last_pitstop = get_latest_pitstop_for_vehicle(vid)
|
||||
last_pitstop_consumable = 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(compute_lower_limits_for_new_pitstop(last_pitstop, last_pitstop_consumable, cid))
|
||||
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)
|
||||
|
|
|
@ -2,9 +2,9 @@ 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, Pitstop
|
||||
from ..forms import CreateServiceForm, DeleteServiceForm, EditServiceForm
|
||||
from ..tools import db_log_update, db_log_delete, db_log_add, get_latest_pitstop_for_vehicle
|
||||
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
|
||||
|
||||
|
||||
|
@ -16,9 +16,14 @@ def create_service_for_vehicle(vid):
|
|||
return redirect(url_for('get_account_page'))
|
||||
|
||||
form = CreateServiceForm()
|
||||
last_pitstop = get_latest_pitstop_for_vehicle(vid)
|
||||
form.set_pitstop(last_pitstop)
|
||||
form.same_odometer_allowed = True
|
||||
|
||||
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()
|
||||
|
||||
|
@ -27,8 +32,7 @@ def create_service_for_vehicle(vid):
|
|||
db.session.add(new_service)
|
||||
vehicle.services.append(new_service)
|
||||
db.session.commit()
|
||||
print(new_service)
|
||||
return redirect(url_for('get_account_page'))
|
||||
return redirect(url_for('get_pit_stops', _anchor='v' + str(vehicle.id)))
|
||||
|
||||
form.process()
|
||||
return render_template('createServiceForm.html', form=form, vehicle=vehicle, messages=[])
|
||||
|
@ -40,6 +44,7 @@ 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'))
|
||||
|
@ -65,15 +70,15 @@ def edit_service_form(sid):
|
|||
if vehicle not in current_user.vehicles:
|
||||
return redirect(url_for('get_pit_stops'))
|
||||
|
||||
# last_pitstop_pos = vehicle.pitstops.index(edit_service) - 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)
|
||||
last_pitstop = Pitstop(0, 0, date(1970, 1, 1), 0, 0)
|
||||
|
||||
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
|
||||
|
@ -90,9 +95,26 @@ def edit_service_form(sid):
|
|||
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))
|
||||
'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)
|
||||
|
||||
|
||||
|
|
|
@ -37,6 +37,7 @@
|
|||
</td>
|
||||
<td>
|
||||
{{ vehicle.pitstops | length }} pitstops<br />
|
||||
{{ vehicle.services | length }} special expenses<br />
|
||||
{{ vehicle.consumables | length }} consumables
|
||||
</td>
|
||||
<td>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
<div class='panel panel-default'>
|
||||
<div class='panel-body'>
|
||||
<h3>Delete service?</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'>{{ service.date }}</td>
|
||||
|
@ -17,7 +17,7 @@
|
|||
</tr>
|
||||
<tr>
|
||||
<th style='text-align:right'>Description</th>
|
||||
<td style='text-align: left'>{{ service.description }}</td>
|
||||
<td style='text-align: left' class="markdown">{{ service.description | markdown | safe}}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th style='text-align:right'>Costs (overall)</th>
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
{% macro navigation() -%}
|
||||
{% if current_user.email %}
|
||||
<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') %}
|
||||
|
|
|
@ -59,7 +59,7 @@
|
|||
</tr>
|
||||
<tr>
|
||||
<th>Description</th>
|
||||
<td id="vehicle_{{vindex}}_pitstop_{{loop.index}}_anmount">{{field.description}}</td>
|
||||
<td id="vehicle_{{vindex}}_pitstop_{{loop.index}}_anmount" class="markdown">{{field.description | markdown | safe}}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Costs</th>
|
||||
|
|
19
app/tools.py
19
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:
|
||||
|
@ -180,3 +181,11 @@ 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
|
Loading…
Reference in New Issue