all pit stops can now be edited and deleted

not only the last pit stop can be edited and deleted.
This commit is contained in:
2018-08-16 18:33:09 +02:00
parent ae6dfa8ae6
commit 1d767d65a6
4 changed files with 52 additions and 18 deletions

View File

@@ -4,8 +4,8 @@ from datetime import date
def odometer_date_check(form, field):
"""
Checks that the entered date and odometer of the pitstop is conformant to the existing pitstops. That means, if a
pitstops date is between to other pitstops, the odometer should be as well.
Checks that the entered date and odometer of the pit stop is conformant to the existing pit stops. That means, if a
pitstops date is between two other pit stops, the odometer should be as well.
:param form:
:param field:
:return:
@@ -29,6 +29,33 @@ def odometer_date_check(form, field):
% (pitstops[index].odometer,pitstops[index+1].odometer))
def edit_odometer_date_check(form, field):
"""
This makes exactly the same checks as 'odometer_date_check' but the odometers may be the same (to change only amount
and price).
:param form:
:param field:
:return:
"""
odometer = form.odometer.data
date = form.date.data
pitstops = form.pitstops
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)
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))
def date_check(form, field):
"""

View File

@@ -9,13 +9,17 @@ class DeletePitStopForm(FlaskForm):
class EditPitstopForm(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=[edit_odometer_date_check])
litres = DecimalField('Litres (l)', places=2, validators=[litres_check])
costs = DecimalField('Costs (€, overall)', places=2, validators=[edit_costs_check])
costs = DecimalField('Costs (€, overall)', places=2, validators=[costs_check])
submit = SubmitField(label='Update it!')
last_pitstop = None
same_odometer_allowed = True
pitstops = []
def set_pitstops(self, pitstops):
self.pitstops = pitstops
def set_pitstop(self, last_pitstop):
self.last_pitstop = last_pitstop
@@ -33,6 +37,12 @@ class EditPitstopForm(FlaskForm):
if self.costs.data:
self.costs.default = self.costs.data
def get_hint_messages(self):
messages = {
'litres': 'Litres must be higher than 0.01 L.',
'costs': 'Costs must be higher than 0.01 €.'
}
return messages
class CreatePitstopForm(FlaskForm):
date = DateField('Date of Pitstop')