2016-11-06 18:53:13 +01:00
|
|
|
from flask_wtf import FlaskForm
|
2016-11-01 16:36:28 +01:00
|
|
|
from wtforms import DateField, IntegerField, DecimalField, SubmitField, TextAreaField
|
|
|
|
from wtforms.validators import Length
|
|
|
|
|
|
|
|
from .checks import *
|
|
|
|
|
|
|
|
|
2016-11-06 18:53:13 +01:00
|
|
|
class CreateServiceForm(FlaskForm):
|
2021-06-22 21:49:32 +02:00
|
|
|
date = DateField('Date of Service')
|
2018-08-15 21:45:22 +02:00
|
|
|
odometer = IntegerField('Odometer (km)', validators=[odometer_date_check])
|
2016-11-01 16:36:28 +01:00
|
|
|
costs = DecimalField('Costs (€, overall)', places=2, validators=[costs_check])
|
|
|
|
description = TextAreaField('Description', validators=[Length(1, 4096)])
|
|
|
|
submit = SubmitField(label='Do it!')
|
2018-08-15 21:45:22 +02:00
|
|
|
pitstops = []
|
2016-11-01 16:36:28 +01:00
|
|
|
|
2018-08-15 21:45:22 +02:00
|
|
|
def set_pitstops(self, pitstops):
|
|
|
|
self.pitstops = pitstops
|
2016-11-01 16:36:28 +01:00
|
|
|
|
|
|
|
def preinit_with_data(self):
|
|
|
|
if self.date.data:
|
|
|
|
self.date.default = self.date.data
|
|
|
|
else:
|
|
|
|
self.date.default = date.today()
|
|
|
|
|
|
|
|
if self.odometer.data:
|
|
|
|
self.odometer.default = self.odometer.data
|
2018-08-15 21:45:22 +02:00
|
|
|
elif len(self.pitstops) > 0:
|
|
|
|
self.odometer.default = self.pitstops[-1].odometer
|
2016-11-01 16:36:28 +01:00
|
|
|
else:
|
2018-08-15 21:45:22 +02:00
|
|
|
self.odometer.default = 0
|
2016-11-01 16:36:28 +01:00
|
|
|
|
|
|
|
if self.costs.data:
|
|
|
|
self.costs.default = self.costs.data
|
|
|
|
else:
|
|
|
|
self.costs.default = 0
|
|
|
|
|
2018-08-16 06:58:11 +02:00
|
|
|
if self.description.data:
|
|
|
|
self.description.default = self.description.data
|
|
|
|
|
2016-11-01 16:36:28 +01:00
|
|
|
|
2016-11-06 18:53:13 +01:00
|
|
|
class DeleteServiceForm(FlaskForm):
|
2016-11-01 16:36:28 +01:00
|
|
|
submit = SubmitField(label='Really delete this service!')
|
|
|
|
|
|
|
|
|
2016-11-06 18:53:13 +01:00
|
|
|
class EditServiceForm(FlaskForm):
|
2018-08-17 07:15:27 +02:00
|
|
|
date = DateField('Date of Service')
|
|
|
|
odometer = IntegerField('Odometer (km)', validators=[edit_odometer_date_check])
|
2016-11-01 16:36:28 +01:00
|
|
|
costs = DecimalField('Costs (€, overall)', places=2, validators=[costs_check])
|
|
|
|
description = TextAreaField('Description', validators=[Length(1, 4096)])
|
|
|
|
submit = SubmitField(label='Do it!')
|
|
|
|
same_odometer_allowed = True
|
2018-08-17 07:15:27 +02:00
|
|
|
pitstops = []
|
2016-11-01 16:36:28 +01:00
|
|
|
|
2018-08-17 07:15:27 +02:00
|
|
|
def set_pitstops(self, pitstops):
|
|
|
|
self.pitstops = pitstops
|
2016-11-01 16:36:28 +01:00
|
|
|
|
|
|
|
def preinit_with_data(self):
|
|
|
|
if self.date.data:
|
|
|
|
self.date.default = self.date.data
|
|
|
|
|
|
|
|
if self.odometer.data:
|
|
|
|
self.odometer.default = self.odometer.data
|
|
|
|
|
|
|
|
if self.costs.data:
|
|
|
|
self.costs.default = self.costs.data
|
|
|
|
|
2018-08-16 06:58:11 +02:00
|
|
|
if self.description.data:
|
|
|
|
self.description.default = self.description.data
|
2016-11-01 16:36:28 +01:00
|
|
|
|
2018-08-17 07:15:27 +02:00
|
|
|
def get_hint_messages(self):
|
|
|
|
messages = {
|
|
|
|
'litres': 'Litres must be higher than 0.01 L.',
|
|
|
|
'costs': 'Costs must be higher than 0.01 €.'
|
|
|
|
}
|
2021-06-22 21:49:32 +02:00
|
|
|
return messages
|