fix issue with creating the first ever pitstop for vehicle

This commit is contained in:
Joachim Lusiardi 2018-08-11 19:38:51 +02:00
parent 889a3e6476
commit b3cb88e870
3 changed files with 25 additions and 13 deletions

View File

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

View File

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

View File

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