diff --git a/app/routes/pitstop.py b/app/routes/pitstop.py
index ea8d630..cb54f3b 100644
--- a/app/routes/pitstop.py
+++ b/app/routes/pitstop.py
@@ -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)
diff --git a/app/routes/service.py b/app/routes/service.py
index 5272051..4a5a9dd 100644
--- a/app/routes/service.py
+++ b/app/routes/service.py
@@ -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)
+
+
diff --git a/app/templates/account.html b/app/templates/account.html
index 01bf40a..004a005 100644
--- a/app/templates/account.html
+++ b/app/templates/account.html
@@ -37,6 +37,7 @@
{{ vehicle.pitstops | length }} pitstops
+ {{ vehicle.services | length }} special expenses
{{ vehicle.consumables | length }} consumables
|
diff --git a/app/templates/deletePitstopForm.html b/app/templates/deletePitstopForm.html
index 871ec67..635ff68 100644
--- a/app/templates/deletePitstopForm.html
+++ b/app/templates/deletePitstopForm.html
@@ -6,7 +6,7 @@
Delete pitstop?
-
+
Date of Pitstop |
{{ pitstop.date }} |
diff --git a/app/templates/deleteServiceForm.html b/app/templates/deleteServiceForm.html
index 06f2bec..9448197 100644
--- a/app/templates/deleteServiceForm.html
+++ b/app/templates/deleteServiceForm.html
@@ -6,7 +6,7 @@
Delete service?
-
+
Date of Pitstop |
{{ service.date }} |
@@ -17,7 +17,7 @@
Description |
- {{ service.description }} |
+ {{ service.description | markdown | safe}} |
Costs (overall) |
diff --git a/app/templates/layout.html b/app/templates/layout.html
index c511fd7..3420b4d 100644
--- a/app/templates/layout.html
+++ b/app/templates/layout.html
@@ -1,6 +1,7 @@
{% macro navigation() -%}
{% if current_user.email %}
Create Pitstop
+ Create Service
Statistics
Account
{% if current_user.has_role('admin') %}
diff --git a/app/templates/pitstops.html b/app/templates/pitstops.html
index 4a5ba12..0d177e1 100644
--- a/app/templates/pitstops.html
+++ b/app/templates/pitstops.html
@@ -59,7 +59,7 @@
Description |
- {{field.description}} |
+ {{field.description | markdown | safe}} |
Costs |
diff --git a/app/tools.py b/app/tools.py
index f1e1b53..ee56ae7 100644
--- a/app/tools.py
+++ b/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
\ No newline at end of file
|