From 1f2e33bec4058c04b599d8330d80d87625cbfb91 Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Sat, 11 Aug 2018 14:11:09 +0200 Subject: [PATCH] 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. --- app/forms/checks.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) 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.