Adds check to verify the proper order of pitstops

Pitstops must keep the order of date and odometer. That means if
a pitstop's date is between two other pitstops' date than the
odometer values must be as well.
This commit is contained in:
Joachim Lusiardi 2018-08-11 14:11:09 +02:00
parent c42a667ca4
commit 1f2e33bec4
1 changed files with 25 additions and 0 deletions

View File

@ -2,6 +2,31 @@ from wtforms.validators import ValidationError
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.
:param form:
:param field:
:return:
"""
odometer = form.odometer.data
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)
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):
"""
Checks that the date of the pitstop is not before the date of the latest pitstop and not after today.