first shot

This commit is contained in:
2021-06-17 18:28:19 +02:00
parent 4196111529
commit 42356137c4
18 changed files with 839 additions and 137 deletions

View File

@@ -4,3 +4,4 @@ from .checks import *
from .consumable import *
from .service import *
from .vehicle import *
from .regular_cost import *

View File

@@ -2,10 +2,34 @@ from wtforms.validators import ValidationError
from datetime import date
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))
def odometer_date_check(form, field):
"""
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.
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:
@@ -16,23 +40,37 @@ def odometer_date_check(form, field):
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)
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)
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):
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 (
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 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).
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:
@@ -44,31 +82,50 @@ def edit_odometer_date_check(form, field):
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)
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)
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):
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 (
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):
"""
Checks that the date of the pitstop is not before the date of the latest pitstop and not after today.
Checks that the date of the pitstop is not before the date of the latest
pitstop and not after today.
: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:
raise ValidationError('The new date must not be before %s' % form.last_pitstop.date)
raise ValidationError(
"The new date must not be before %s" % form.last_pitstop.date
)
if field.data > date.today():
raise ValidationError('The new date must not be after %s' % date.today())
raise ValidationError(
"The new date must not be after %s" % date.today()
)
def odometer_check(form, field):
@@ -78,20 +135,29 @@ def odometer_check(form, field):
:param field:
:return:
"""
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)
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
)
if 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)
raise ValidationError(
"The new odometer value must be higher than %i km"
% form.last_pitstop.odometer
)
def litres_check(form, field):
if field.data is not None and field.data <= 0:
raise ValidationError('You must fuel at least 0.1 l')
raise ValidationError("You must fuel at least 0.1 l")
def costs_check(form, field):
if field.data is not None and field.data <= 0:
raise ValidationError('Costs must be above 0.01 €.')
raise ValidationError("Costs must be above 0.01 €.")
def edit_costs_check(form, field):
@@ -101,8 +167,8 @@ def edit_costs_check(form, field):
:param field:
:return:
"""
costs_check_required = (form.costs.default is not None and form.costs.default > 0)
costs_check_required = (
form.costs.default is not None and form.costs.default > 0
)
if costs_check_required and field.data is not None and field.data <= 0:
raise ValidationError('Costs must be above 0.01 €.')
raise ValidationError("Costs must be above 0.01 €.")

86
app/forms/regular_cost.py Normal file
View File

@@ -0,0 +1,86 @@
from flask_wtf import FlaskForm
from wtforms import (
DateField,
IntegerField,
DecimalField,
SubmitField,
TextAreaField,
StringField,
)
from wtforms.validators import Length
from .checks import *
from wtforms.validators import Optional
class DeleteRegularCostForm(FlaskForm):
submit = SubmitField(label="Really delete this regular cost!")
class EndRegularCostForm(FlaskForm):
submit = SubmitField(label="Really end this regular cost!")
class EditRegularCostForm(FlaskForm):
start_at = DateField("Date of first instance")
ends_at = DateField(
"Date of last instance", validators=[Optional(strip_whitespace=True)]
)
days = StringField("Days for instance", validators=[regular_costs_days_check])
costs = DecimalField("Costs (€, per instance)", places=2, validators=[costs_check])
description = TextAreaField("Description", validators=[Length(1, 4096)])
submit = SubmitField(label="Update it!")
def preinit_with_data(self):
if self.costs.data:
self.costs.default = self.costs.data
if self.start_at.data:
self.start_at.default = self.start_at.data
if self.ends_at.data:
self.ends_at.default = self.ends_at.data
if self.days.data:
self.days.default = self.days.data
if self.description.data:
self.description.default = self.description.data
def get_hint_messages(self):
messages = {"costs": "Costs must be higher than 0.01 €."}
return messages
class CreateRegularCostForm(FlaskForm):
start_at = DateField("Date of first instance")
ends_at = DateField(
"Date of last instance", validators=[Optional(strip_whitespace=True)]
)
days = StringField("Days for instance", validators=[regular_costs_days_check])
costs = DecimalField("Costs (€, per instance)", places=2, validators=[costs_check])
description = TextAreaField("Description", validators=[Length(1, 4096)])
submit = SubmitField(label="Do it!")
def preinit_with_data(self):
if self.start_at.data:
self.start_at.default = self.start_at.data
else:
self.start_at.default = date.today()
if self.ends_at.data:
self.ends_at.default = self.ends_at.data
else:
self.ends_at.default = None
if self.days.data:
self.days.default = self.days.data
if self.costs.data:
self.costs.default = self.costs.data
else:
self.costs.default = 0
if self.description.data:
self.description.default = self.description.data