Updates create pitstop form

The form now uses the new odometer_date_check to allow entering
pitstops that are before the currently last pitstop.
This commit is contained in:
Joachim Lusiardi 2018-08-11 14:53:08 +02:00
parent 25705562ad
commit 13090d2735
2 changed files with 10 additions and 15 deletions

View File

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

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_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, ''))