From 6397d050aefb1860c8880c7bc1938d20ea0b7626 Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Wed, 15 Aug 2018 21:45:22 +0200 Subject: [PATCH] services and pitstops each now respect their data on creation On creation, the tupels of date and odometer are checked that the service or pitstop does not violate the ordering in. --- app/forms/pitstop.py | 2 +- app/forms/service.py | 14 ++++++++------ app/routes/pitstop.py | 2 +- app/routes/service.py | 4 ++-- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/app/forms/pitstop.py b/app/forms/pitstop.py index 27ff4da..2df0ae1 100644 --- a/app/forms/pitstop.py +++ b/app/forms/pitstop.py @@ -64,7 +64,7 @@ class CreatePitstopForm(FlaskForm): if self.litres.data: self.litres.default = self.litres.data - elif len(self.pitstops) > 0: + elif len(self.pitstops) > 0 and 'amount' in self.pitstops[-1].__dict__: self.litres.default = self.pitstops[-1].amount else: self.litres.default = 0 diff --git a/app/forms/service.py b/app/forms/service.py index 3e11c62..76ef794 100644 --- a/app/forms/service.py +++ b/app/forms/service.py @@ -6,15 +6,15 @@ from .checks import * class CreateServiceForm(FlaskForm): - date = DateField('Date of Service', validators=[date_check]) - odometer = IntegerField('Odometer (km)', validators=[odometer_check]) + date = DateField('Date of Pitstop') + odometer = IntegerField('Odometer (km)', validators=[odometer_date_check]) costs = DecimalField('Costs (€, overall)', places=2, validators=[costs_check]) description = TextAreaField('Description', validators=[Length(1, 4096)]) submit = SubmitField(label='Do it!') - last_pitstop = None + pitstops = [] - def set_pitstop(self, last_pitstop): - self.last_pitstop = last_pitstop + def set_pitstops(self, pitstops): + self.pitstops = pitstops def preinit_with_data(self): if self.date.data: @@ -24,8 +24,10 @@ class CreateServiceForm(FlaskForm): if self.odometer.data: self.odometer.default = self.odometer.data + elif len(self.pitstops) > 0: + self.odometer.default = self.pitstops[-1].odometer else: - self.odometer.default = self.last_pitstop.odometer + self.odometer.default = 0 if self.costs.data: self.costs.default = self.costs.data diff --git a/app/routes/pitstop.py b/app/routes/pitstop.py index 9692496..75f4e5c 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_pitstops(vehicle.pitstops) + form.set_pitstops(data) form.same_odometer_allowed = (type(data[-1]) != Pitstop) or (data[-1].consumable.id != cid) else: form.set_pitstops([]) diff --git a/app/routes/service.py b/app/routes/service.py index 4a5a9dd..6c73520 100644 --- a/app/routes/service.py +++ b/app/routes/service.py @@ -19,10 +19,10 @@ def create_service_for_vehicle(vid): data = get_event_line_for_vehicle(vehicle) if len(data) > 0: - form.set_pitstop(Service(data[-1].date, data[-1].odometer, vid, 0, '')) + form.set_pitstops(data) form.same_odometer_allowed = type(data[-1]) != Service else: - form.set_pitstop(Service(date(1970, 1, 1), 0, vid, 0, '')) + form.set_pitstops([]) form.same_odometer_allowed = True form.preinit_with_data()