finished handling of services

This commit is contained in:
Joachim Lusiardi 2016-11-06 13:22:54 +01:00
parent 60a1b307e4
commit 6e665b9f2f
8 changed files with 69 additions and 36 deletions

View File

@ -7,7 +7,8 @@ from datetime import date
from ..entities import Vehicle, Consumable, Pitstop from ..entities import Vehicle, Consumable, Pitstop
from ..forms import SelectVehicleForm, SelectConsumableForm, CreatePitstopForm, EditPitstopForm, DeletePitStopForm 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, \ 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 from .. import app, db
@ -62,15 +63,14 @@ def create_pit_stop_form(vid, cid):
form = CreatePitstopForm() form = CreatePitstopForm()
# the last pitstop is required to be able to check the monotonicy of date and odometer data = get_event_line_for_vehicle(vehicle)
last_pitstop = get_latest_pitstop_for_vehicle(vid) if len(data) > 0:
last_pitstop_consumable = get_latest_pitstop_for_vehicle_and_consumable(vid, cid) 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)
# we can enter the same odometer if the pitstops are not equal else:
form.same_odometer_allowed = (last_pitstop != last_pitstop_consumable) form.set_pitstop(Pitstop(date(1970, 1, 1), 0, vid, 0, ''))
form.set_pitstop(Pitstop(0, 0, date(1970, 1, 1), 0, cid))
# set the lower limits for odometer andd date and the values for amount and costs of the last stop form.same_odometer_allowed = True
form.set_pitstop(compute_lower_limits_for_new_pitstop(last_pitstop, last_pitstop_consumable, cid))
# set the label of the litres field to make the user comfortable # set the label of the litres field to make the user comfortable
form.set_consumable(consumable) form.set_consumable(consumable)

View File

@ -2,9 +2,9 @@ from flask import url_for, redirect, render_template
from flask_security import login_required, current_user from flask_security import login_required, current_user
from datetime import date from datetime import date
from ..entities import Vehicle, Service, Pitstop from ..entities import Vehicle, Service
from ..forms import CreateServiceForm, DeleteServiceForm, EditServiceForm from ..forms import CreateServiceForm, DeleteServiceForm, EditServiceForm, SelectVehicleForm
from ..tools import db_log_update, db_log_delete, db_log_add, get_latest_pitstop_for_vehicle from ..tools import db_log_update, db_log_delete, get_event_line_for_vehicle, get_latest_pitstop_for_vehicle
from .. import app, db from .. import app, db
@ -16,8 +16,13 @@ def create_service_for_vehicle(vid):
return redirect(url_for('get_account_page')) return redirect(url_for('get_account_page'))
form = CreateServiceForm() form = CreateServiceForm()
last_pitstop = get_latest_pitstop_for_vehicle(vid)
form.set_pitstop(last_pitstop) 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.same_odometer_allowed = True
form.preinit_with_data() form.preinit_with_data()
@ -27,8 +32,7 @@ def create_service_for_vehicle(vid):
db.session.add(new_service) db.session.add(new_service)
vehicle.services.append(new_service) vehicle.services.append(new_service)
db.session.commit() db.session.commit()
print(new_service) return redirect(url_for('get_pit_stops', _anchor='v' + str(vehicle.id)))
return redirect(url_for('get_account_page'))
form.process() form.process()
return render_template('createServiceForm.html', form=form, vehicle=vehicle, messages=[]) 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() service = Service.query.filter(Service.id == sid).first()
if service is None: if service is None:
return redirect(url_for('get_pit_stops')) return redirect(url_for('get_pit_stops'))
vehicle = Vehicle.query.filter(Vehicle.id == service.vehicle_id).first() vehicle = Vehicle.query.filter(Vehicle.id == service.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'))
@ -65,15 +70,15 @@ def edit_service_form(sid):
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'))
# 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() 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.set_pitstop(last_pitstop)
form.same_odometer_allowed = True
if form.validate_on_submit(): if form.validate_on_submit():
edit_service.costs = form.costs.data edit_service.costs = form.costs.data
@ -90,9 +95,26 @@ def edit_service_form(sid):
form.costs.default = edit_service.costs form.costs.default = edit_service.costs
form.process() form.process()
messages = { messages = {
# 'date': 'Date must be between %s and %s (including).' % (str(last_pitstop.date), str(date.today())), '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)) 'odometer': 'Odometer must be greater than %s km.' % (str(last_pitstop.odometer))
} }
if edit_service.costs is not None and edit_service.costs > 0: if edit_service.costs is not None and edit_service.costs > 0:
messages['costs'] = 'Costs must be higher than 0.01 €.' messages['costs'] = 'Costs must be higher than 0.01 €.'
return render_template('editServiceForm.html', form=form, vehicle=vehicle, messages=messages) 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)

View File

@ -37,6 +37,7 @@
</td> </td>
<td> <td>
{{ vehicle.pitstops | length }} pitstops<br /> {{ vehicle.pitstops | length }} pitstops<br />
{{ vehicle.services | length }} special expenses<br />
{{ vehicle.consumables | length }} consumables {{ vehicle.consumables | length }} consumables
</td> </td>
<td> <td>

View File

@ -6,7 +6,7 @@
<div class='panel panel-default'> <div class='panel panel-default'>
<div class='panel-body'> <div class='panel-body'>
<h3>Delete pitstop?</h3> <h3>Delete pitstop?</h3>
<table style='width: 100%'> <table style='width: 100%' class="table table-striped table-bordered table-condensed">
<tr> <tr>
<th style='text-align:right'>Date of Pitstop</th> <th style='text-align:right'>Date of Pitstop</th>
<td style='text-align: left'>{{ pitstop.date }}</td> <td style='text-align: left'>{{ pitstop.date }}</td>

View File

@ -6,7 +6,7 @@
<div class='panel panel-default'> <div class='panel panel-default'>
<div class='panel-body'> <div class='panel-body'>
<h3>Delete service?</h3> <h3>Delete service?</h3>
<table style='width: 100%'> <table style='width: 100%' class="table table-striped table-bordered table-condensed">
<tr> <tr>
<th style='text-align:right'>Date of Pitstop</th> <th style='text-align:right'>Date of Pitstop</th>
<td style='text-align: left'>{{ service.date }}</td> <td style='text-align: left'>{{ service.date }}</td>
@ -17,7 +17,7 @@
</tr> </tr>
<tr> <tr>
<th style='text-align:right'>Description</th> <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>
<tr> <tr>
<th style='text-align:right'>Costs (overall)</th> <th style='text-align:right'>Costs (overall)</th>

View File

@ -1,6 +1,7 @@
{% macro navigation() -%} {% macro navigation() -%}
{% if current_user.email %} {% 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_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='statistics_limk' href='{{ url_for('get_statistics') }}'>Statistics</a></li>
<li><a id='account_link' href='{{ url_for('get_account_page') }}'>Account</a></li> <li><a id='account_link' href='{{ url_for('get_account_page') }}'>Account</a></li>
{% if current_user.has_role('admin') %} {% if current_user.has_role('admin') %}

View File

@ -59,7 +59,7 @@
</tr> </tr>
<tr> <tr>
<th>Description</th> <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>
<tr> <tr>
<th>Costs</th> <th>Costs</th>

View File

@ -49,16 +49,17 @@ class VehicleStats:
for consumable in vehicle.consumables: for consumable in vehicle.consumables:
self.consumables.append(ConsumableStats(vehicle, consumable)) 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: if pitstop_count > 0:
for pitstop in vehicle.pitstops: for pitstop in events:
self.odometers.append(StatsEvent(pitstop.date, pitstop.odometer)) self.odometers.append(StatsEvent(pitstop.date, pitstop.odometer))
if pitstop.costs is not None: if pitstop.costs is not None:
self.overall_costs += pitstop.costs self.overall_costs += pitstop.costs
if pitstop_count > 1: 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: class StatsEvent:
@ -180,3 +181,11 @@ def pitstop_service_key(x):
return x.odometer, x.date 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