2016-11-01 16:36:28 +01:00
|
|
|
from wtforms.validators import ValidationError
|
|
|
|
from datetime import date
|
|
|
|
|
|
|
|
|
2021-06-17 18:28:19 +02:00
|
|
|
def regular_costs_days_check(form, field):
|
|
|
|
"""
|
|
|
|
Checks the input field to enter multiple days in the following format:
|
|
|
|
`01-15,07-15` for Jan 15th and July 15
|
|
|
|
"""
|
|
|
|
days = form.days.data
|
|
|
|
for day in days.split(","):
|
|
|
|
day = day.strip()
|
|
|
|
if not day:
|
|
|
|
raise ValidationError("Missing Date after ','")
|
|
|
|
try:
|
|
|
|
m, d = day.split("-")
|
|
|
|
m_i = int(m)
|
|
|
|
d_i = int(d)
|
|
|
|
except Exception:
|
|
|
|
raise ValidationError("Malformed Date, must be 'Month-Day'")
|
|
|
|
try:
|
|
|
|
d = date(2021, m_i, d_i)
|
|
|
|
except Exception:
|
|
|
|
raise ValidationError("{}-{} is not a valid date".format(m, d))
|
|
|
|
|
|
|
|
|
2018-08-11 14:11:09 +02:00
|
|
|
def odometer_date_check(form, field):
|
|
|
|
"""
|
2021-06-17 18:28:19 +02:00
|
|
|
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.
|
|
|
|
|
2018-08-11 14:11:09 +02:00
|
|
|
:param form:
|
|
|
|
:param field:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
odometer = form.odometer.data
|
|
|
|
date = form.date.data
|
|
|
|
pitstops = form.pitstops
|
|
|
|
|
2018-08-11 19:38:51 +02:00
|
|
|
if len(pitstops) > 0:
|
|
|
|
if date < pitstops[0].date and odometer >= pitstops[0].odometer:
|
2021-06-17 18:28:19 +02:00
|
|
|
raise ValidationError(
|
|
|
|
"The new odometer value must be less than %i km"
|
|
|
|
% pitstops[0].odometer
|
|
|
|
)
|
2018-08-11 14:11:09 +02:00
|
|
|
|
2018-08-11 19:38:51 +02:00
|
|
|
if date >= pitstops[-1].date and odometer <= pitstops[-1].odometer:
|
2021-06-17 18:28:19 +02:00
|
|
|
raise ValidationError(
|
|
|
|
"The new odometer value must be greater than %i km"
|
|
|
|
% pitstops[-1].odometer
|
|
|
|
)
|
2018-08-11 19:38:51 +02:00
|
|
|
|
|
|
|
if len(pitstops) > 1:
|
2021-06-17 18:28:19 +02:00
|
|
|
for index in range(0, len(pitstops) - 1):
|
2018-08-11 19:38:51 +02:00
|
|
|
if pitstops[index].date <= date < pitstops[index + 1].date:
|
2021-06-17 18:28:19 +02:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
)
|
2018-08-11 14:11:09 +02:00
|
|
|
|
|
|
|
|
2018-08-16 18:33:09 +02:00
|
|
|
def edit_odometer_date_check(form, field):
|
|
|
|
"""
|
2021-06-17 18:28:19 +02:00
|
|
|
This makes exactly the same checks as 'odometer_date_check' but the
|
|
|
|
odometers may be the same (to change only amount and price).
|
2018-08-16 18:33:09 +02:00
|
|
|
|
|
|
|
: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:
|
2021-06-17 18:28:19 +02:00
|
|
|
raise ValidationError(
|
|
|
|
"The new odometer value must be less than %i km"
|
|
|
|
% pitstops[0].odometer
|
|
|
|
)
|
2018-08-16 18:33:09 +02:00
|
|
|
|
|
|
|
if date >= pitstops[-1].date and odometer < pitstops[-1].odometer:
|
2021-06-17 18:28:19 +02:00
|
|
|
raise ValidationError(
|
|
|
|
"The new odometer value must be greater than %i km"
|
|
|
|
% pitstops[-1].odometer
|
|
|
|
)
|
2018-08-16 18:33:09 +02:00
|
|
|
|
|
|
|
if len(pitstops) > 1:
|
2021-06-17 18:28:19 +02:00
|
|
|
for index in range(0, len(pitstops) - 1):
|
2018-08-16 18:33:09 +02:00
|
|
|
if pitstops[index].date <= date < pitstops[index + 1].date:
|
2021-06-17 18:28:19 +02:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
)
|
2018-08-16 18:33:09 +02:00
|
|
|
|
2018-08-11 14:11:09 +02:00
|
|
|
|
2016-11-01 16:36:28 +01:00
|
|
|
def date_check(form, field):
|
|
|
|
"""
|
2021-06-17 18:28:19 +02:00
|
|
|
Checks that the date of the pitstop is not before the date of the latest
|
|
|
|
pitstop and not after today.
|
2016-11-01 16:36:28 +01:00
|
|
|
|
|
|
|
:param form: the form where the field is in
|
|
|
|
:param field: the field to check
|
|
|
|
:return: Nothing or a ValidationError if the limits are not kept
|
|
|
|
"""
|
|
|
|
if field.data < form.last_pitstop.date:
|
2021-06-17 18:28:19 +02:00
|
|
|
raise ValidationError(
|
|
|
|
"The new date must not be before %s" % form.last_pitstop.date
|
|
|
|
)
|
2016-11-01 16:36:28 +01:00
|
|
|
if field.data > date.today():
|
2021-06-17 18:28:19 +02:00
|
|
|
raise ValidationError(
|
|
|
|
"The new date must not be after %s" % date.today()
|
|
|
|
)
|
2016-11-01 16:36:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
def odometer_check(form, field):
|
|
|
|
"""
|
|
|
|
|
|
|
|
:param form:
|
|
|
|
:param field:
|
|
|
|
:return:
|
|
|
|
"""
|
2021-06-17 18:28:19 +02:00
|
|
|
if (
|
|
|
|
not form.same_odometer_allowed
|
|
|
|
and field.data <= form.last_pitstop.odometer
|
|
|
|
):
|
|
|
|
raise ValidationError(
|
|
|
|
"The new odometer value must be higher than %i km"
|
|
|
|
% form.last_pitstop.odometer
|
|
|
|
)
|
2016-11-01 16:36:28 +01:00
|
|
|
if form.same_odometer_allowed and field.data < form.last_pitstop.odometer:
|
2021-06-17 18:28:19 +02:00
|
|
|
raise ValidationError(
|
|
|
|
"The new odometer value must be higher than %i km"
|
|
|
|
% form.last_pitstop.odometer
|
|
|
|
)
|
2016-11-01 16:36:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
def litres_check(form, field):
|
|
|
|
if field.data is not None and field.data <= 0:
|
2021-06-17 18:28:19 +02:00
|
|
|
raise ValidationError("You must fuel at least 0.1 l")
|
2016-11-01 16:36:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
def costs_check(form, field):
|
|
|
|
if field.data is not None and field.data <= 0:
|
2021-06-17 18:28:19 +02:00
|
|
|
raise ValidationError("Costs must be above 0.01 €.")
|
2016-11-01 16:36:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
def edit_costs_check(form, field):
|
|
|
|
"""
|
|
|
|
Costs must be given, if a default value was given to the form field.
|
|
|
|
:param form:
|
|
|
|
:param field:
|
|
|
|
:return:
|
|
|
|
"""
|
2021-06-17 18:28:19 +02:00
|
|
|
costs_check_required = (
|
|
|
|
form.costs.default is not None and form.costs.default > 0
|
|
|
|
)
|
2016-11-01 16:36:28 +01:00
|
|
|
if costs_check_required and field.data is not None and field.data <= 0:
|
2021-06-17 18:28:19 +02:00
|
|
|
raise ValidationError("Costs must be above 0.01 €.")
|