From 13090d273550f42955c56151885a8a799c23dcd9 Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Sat, 11 Aug 2018 14:53:08 +0200 Subject: [PATCH] Updates create pitstop form The form now uses the new odometer_date_check to allow entering pitstops that are before the currently last pitstop. --- app/forms/pitstop.py | 23 +++++++++-------------- app/routes/pitstop.py | 2 +- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/app/forms/pitstop.py b/app/forms/pitstop.py index 842eacb..7b27c21 100644 --- a/app/forms/pitstop.py +++ b/app/forms/pitstop.py @@ -35,16 +35,16 @@ class EditPitstopForm(FlaskForm): class CreatePitstopForm(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=[odometer_date_check]) litres = DecimalField('Litres (l)', places=2, validators=[litres_check]) costs = DecimalField('Costs (€, overall)', places=2, validators=[costs_check]) submit = SubmitField(label='Do it!') - last_pitstop = None same_odometer_allowed = True + pitstops = [] - def set_pitstop(self, last_pitstop): - self.last_pitstop = last_pitstop + def set_pitstops(self, pitstops): + self.pitstops = pitstops def set_consumable(self, consumable): self.litres.label = '%s (%s)' % (consumable.name, consumable.unit) @@ -57,24 +57,19 @@ class CreatePitstopForm(FlaskForm): if self.odometer.data: self.odometer.default = self.odometer.data else: - self.odometer.default = self.last_pitstop.odometer + self.odometer.default = self.pitstops[-1].odometer if self.litres.data: self.litres.default = self.litres.data else: - self.litres.default = self.last_pitstop.amount + self.litres.default = self.pitstops[-1].amount if self.costs.data: self.costs.default = self.costs.data else: - self.costs.default = self.last_pitstop.costs + self.costs.default = self.pitstops[-1].costs def get_hint_messages(self): - if self.same_odometer_allowed: - or_equal = ' or equal to' - else: - or_equal = '' messages = { - 'date': 'Date must be between %s and %s (including).' % (str(self.last_pitstop.date), str(date.today())), - 'odometer': 'Odometer must be greater than%s %s km.' % (or_equal, str(self.last_pitstop.odometer)), + 'litres': 'Litres must be higher than 0.01 L.', 'costs': 'Costs must be higher than 0.01 €.' } return messages diff --git a/app/routes/pitstop.py b/app/routes/pitstop.py index 40b739b..c0e6c81 100644 --- a/app/routes/pitstop.py +++ b/app/routes/pitstop.py @@ -64,7 +64,7 @@ def create_pit_stop_form(vid, 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.set_pitstops(vehicle.pitstops) 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, ''))