diff --git a/app/forms/checks.py b/app/forms/checks.py index ff8b1bb..fb44931 100644 --- a/app/forms/checks.py +++ b/app/forms/checks.py @@ -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.