diff --git a/app/forms/checks.py b/app/forms/checks.py
index f997c4d..a7b827f 100644
--- a/app/forms/checks.py
+++ b/app/forms/checks.py
@@ -4,8 +4,8 @@ from datetime import date
def odometer_date_check(form, field):
"""
- Checks that the entered date and odometer of the pitstop is conformant to the existing pitstops. That means, if a
- pitstops date is between to other pitstops, the odometer should be as well.
+ Checks that the entered date and odometer of the pit stop is conformant to the existing pit stops. That means, if a
+ pitstops date is between two other pit stops, the odometer should be as well.
:param form:
:param field:
:return:
@@ -29,6 +29,33 @@ def odometer_date_check(form, field):
% (pitstops[index].odometer,pitstops[index+1].odometer))
+def edit_odometer_date_check(form, field):
+ """
+ This makes exactly the same checks as 'odometer_date_check' but the odometers may be the same (to change only amount
+ and price).
+
+ :param form:
+ :param field:
+ :return:
+ """
+ odometer = form.odometer.data
+ date = form.date.data
+ pitstops = form.pitstops
+
+ if len(pitstops) > 0:
+ if date < pitstops[0].date and odometer > pitstops[0].odometer:
+ raise ValidationError('The new odometer value must be less than %i km' % pitstops[0].odometer)
+
+ if date >= pitstops[-1].date and odometer < pitstops[-1].odometer:
+ raise ValidationError('The new odometer value must be greater than %i km' % pitstops[-1].odometer)
+
+ if len(pitstops) > 1:
+ for index in range(0, len(pitstops)-1):
+ if pitstops[index].date <= date < pitstops[index + 1].date:
+ if odometer < pitstops[index].odometer or odometer > pitstops[index+1].odometer:
+ raise ValidationError('The new odometer value must be greater than %i km and less than %i km'
+ % (pitstops[index].odometer,pitstops[index+1].odometer))
+
def date_check(form, field):
"""
diff --git a/app/forms/pitstop.py b/app/forms/pitstop.py
index 2df0ae1..8c5ad2b 100644
--- a/app/forms/pitstop.py
+++ b/app/forms/pitstop.py
@@ -9,13 +9,17 @@ class DeletePitStopForm(FlaskForm):
class EditPitstopForm(FlaskForm):
- date = DateField('Date of Pitstop', validators=[date_check])
- odometer = IntegerField('Odometer (km)', validators=[odometer_check])
+ date = DateField('Date of Pitstop')
+ odometer = IntegerField('Odometer (km)', validators=[edit_odometer_date_check])
litres = DecimalField('Litres (l)', places=2, validators=[litres_check])
- costs = DecimalField('Costs (€, overall)', places=2, validators=[edit_costs_check])
+ costs = DecimalField('Costs (€, overall)', places=2, validators=[costs_check])
submit = SubmitField(label='Update it!')
last_pitstop = None
same_odometer_allowed = True
+ pitstops = []
+
+ def set_pitstops(self, pitstops):
+ self.pitstops = pitstops
def set_pitstop(self, last_pitstop):
self.last_pitstop = last_pitstop
@@ -33,6 +37,12 @@ class EditPitstopForm(FlaskForm):
if self.costs.data:
self.costs.default = self.costs.data
+ def get_hint_messages(self):
+ messages = {
+ 'litres': 'Litres must be higher than 0.01 L.',
+ 'costs': 'Costs must be higher than 0.01 €.'
+ }
+ return messages
class CreatePitstopForm(FlaskForm):
date = DateField('Date of Pitstop')
diff --git a/app/routes/pitstop.py b/app/routes/pitstop.py
index 75f4e5c..252a21b 100644
--- a/app/routes/pitstop.py
+++ b/app/routes/pitstop.py
@@ -135,7 +135,13 @@ def edit_pit_stop_form(pid):
form = EditPitstopForm()
form.set_pitstop(last_pitstop)
-
+ data = get_event_line_for_vehicle(vehicle)
+ form.set_pitstops(data)
+ if not form.is_submitted():
+ form.odometer.default = edit_pitstop.odometer
+ form.litres.default = edit_pitstop.amount
+ form.date.default = edit_pitstop.date
+ form.costs.default = edit_pitstop.costs
if form.validate_on_submit():
edit_pitstop.costs = form.costs.data
edit_pitstop.date = form.date.data
@@ -145,18 +151,9 @@ def edit_pit_stop_form(pid):
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.preinit_with_data()
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)
+ return render_template('editPitStopForm.html', form=form, vehicle=vehicle, messages=form.get_hint_messages())
@app.route('/pitstops', methods=['GET'])
diff --git a/app/templates/pitstops.html b/app/templates/pitstops.html
index 4767128..7295c8e 100644
--- a/app/templates/pitstops.html
+++ b/app/templates/pitstops.html
@@ -31,10 +31,10 @@
{% if loop.first %}
+ {% endif %}
edit
- {% endif %}
delete