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.
This commit is contained in:
Joachim Lusiardi 2018-08-15 21:45:22 +02:00
parent bf4bb607d5
commit 6397d050ae
4 changed files with 12 additions and 10 deletions

View File

@ -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

View File

@ -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

View File

@ -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([])

View File

@ -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()