From b3cb88e870db01c30902a4c50a6391a6cca45b87 Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Sat, 11 Aug 2018 19:38:51 +0200 Subject: [PATCH] fix issue with creating the first ever pitstop for vehicle --- app/forms/checks.py | 21 ++++++++++++--------- app/forms/pitstop.py | 15 ++++++++++++--- app/routes/pitstop.py | 2 +- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/app/forms/checks.py b/app/forms/checks.py index fb44931..f997c4d 100644 --- a/app/forms/checks.py +++ b/app/forms/checks.py @@ -14,17 +14,20 @@ def odometer_date_check(form, field): date = form.date.data pitstops = form.pitstops - 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 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) - 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)) + 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)) - 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) def date_check(form, field): diff --git a/app/forms/pitstop.py b/app/forms/pitstop.py index 7b27c21..27ff4da 100644 --- a/app/forms/pitstop.py +++ b/app/forms/pitstop.py @@ -54,18 +54,27 @@ class CreatePitstopForm(FlaskForm): self.date.default = self.date.data else: self.date.default = date.today() + if self.odometer.data: self.odometer.default = self.odometer.data - else: + elif len(self.pitstops) > 0: self.odometer.default = self.pitstops[-1].odometer + else: + self.odometer.default = 0 + if self.litres.data: self.litres.default = self.litres.data - else: + elif len(self.pitstops) > 0: self.litres.default = self.pitstops[-1].amount + else: + self.litres.default = 0 + if self.costs.data: self.costs.default = self.costs.data - else: + elif len(self.pitstops) > 0: self.costs.default = self.pitstops[-1].costs + else: + self.costs.default = 0 def get_hint_messages(self): messages = { diff --git a/app/routes/pitstop.py b/app/routes/pitstop.py index 7861a06..9692496 100644 --- a/app/routes/pitstop.py +++ b/app/routes/pitstop.py @@ -67,7 +67,7 @@ def create_pit_stop_form(vid, cid): form.set_pitstops(vehicle.pitstops) form.same_odometer_allowed = (type(data[-1]) != Pitstop) or (data[-1].consumable.id != cid) else: - form.set_pitstops([Pitstop(date(1970, 1, 1), 0, vid, 0, ''),Pitstop(0, 0, date(1970, 1, 1), 0, cid)]) + form.set_pitstops([]) form.same_odometer_allowed = True # set the label of the litres field to make the user comfortable