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
|
|
|
|
|
|
|
|
from .checks import *
|
|
|
|
|
|
|
|
|
2016-11-06 18:53:13 +01:00
|
|
|
class DeletePitStopForm(FlaskForm):
|
2016-11-01 16:36:28 +01:00
|
|
|
submit = SubmitField(label='Really delete this pitstop!')
|
|
|
|
|
|
|
|
|
2016-11-06 18:53:13 +01:00
|
|
|
class EditPitstopForm(FlaskForm):
|
2018-08-16 18:33:09 +02:00
|
|
|
date = DateField('Date of Pitstop')
|
|
|
|
odometer = IntegerField('Odometer (km)', validators=[edit_odometer_date_check])
|
2016-11-01 16:36:28 +01:00
|
|
|
litres = DecimalField('Litres (l)', places=2, validators=[litres_check])
|
2018-08-16 18:33:09 +02:00
|
|
|
costs = DecimalField('Costs (€, overall)', places=2, validators=[costs_check])
|
2016-11-01 16:36:28 +01:00
|
|
|
submit = SubmitField(label='Update it!')
|
|
|
|
same_odometer_allowed = True
|
2018-08-16 18:33:09 +02:00
|
|
|
pitstops = []
|
|
|
|
|
|
|
|
def set_pitstops(self, pitstops):
|
|
|
|
self.pitstops = pitstops
|
2016-11-01 16:36:28 +01:00
|
|
|
|
|
|
|
def set_consumable(self, consumable):
|
|
|
|
self.litres.label = '%s (%s)' % (consumable.name, consumable.unit)
|
|
|
|
|
|
|
|
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.litres.data:
|
|
|
|
self.litres.default = self.litres.data
|
|
|
|
if self.costs.data:
|
|
|
|
self.costs.default = self.costs.data
|
|
|
|
|
2018-08-16 18:33:09 +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 €.'
|
|
|
|
}
|
|
|
|
return messages
|
2016-11-01 16:36:28 +01:00
|
|
|
|
2018-08-17 07:15:27 +02:00
|
|
|
|
2016-11-06 18:53:13 +01:00
|
|
|
class CreatePitstopForm(FlaskForm):
|
2018-08-11 14:53:08 +02:00
|
|
|
date = DateField('Date of Pitstop')
|
|
|
|
odometer = IntegerField('Odometer (km)', validators=[odometer_date_check])
|
2016-11-01 16:36:28 +01:00
|
|
|
litres = DecimalField('Litres (l)', places=2, validators=[litres_check])
|
|
|
|
costs = DecimalField('Costs (€, overall)', places=2, validators=[costs_check])
|
|
|
|
submit = SubmitField(label='Do it!')
|
|
|
|
same_odometer_allowed = True
|
2018-08-11 14:53:08 +02:00
|
|
|
pitstops = []
|
2016-11-01 16:36:28 +01:00
|
|
|
|
2018-08-11 14:53:08 +02:00
|
|
|
def set_pitstops(self, pitstops):
|
|
|
|
self.pitstops = pitstops
|
2016-11-01 16:36:28 +01:00
|
|
|
|
|
|
|
def set_consumable(self, consumable):
|
|
|
|
self.litres.label = '%s (%s)' % (consumable.name, consumable.unit)
|
|
|
|
|
|
|
|
def preinit_with_data(self):
|
|
|
|
if self.date.data:
|
|
|
|
self.date.default = self.date.data
|
|
|
|
else:
|
|
|
|
self.date.default = date.today()
|
2018-08-11 19:38:51 +02:00
|
|
|
|
2016-11-01 16:36:28 +01:00
|
|
|
if self.odometer.data:
|
|
|
|
self.odometer.default = self.odometer.data
|
2018-08-11 19:38:51 +02:00
|
|
|
elif len(self.pitstops) > 0:
|
2018-08-11 14:53:08 +02:00
|
|
|
self.odometer.default = self.pitstops[-1].odometer
|
2018-08-11 19:38:51 +02:00
|
|
|
else:
|
|
|
|
self.odometer.default = 0
|
|
|
|
|
2016-11-01 16:36:28 +01:00
|
|
|
if self.litres.data:
|
|
|
|
self.litres.default = self.litres.data
|
2018-08-15 21:45:22 +02:00
|
|
|
elif len(self.pitstops) > 0 and 'amount' in self.pitstops[-1].__dict__:
|
2018-08-11 14:53:08 +02:00
|
|
|
self.litres.default = self.pitstops[-1].amount
|
2018-08-11 19:38:51 +02:00
|
|
|
else:
|
|
|
|
self.litres.default = 0
|
|
|
|
|
2016-11-01 16:36:28 +01:00
|
|
|
if self.costs.data:
|
|
|
|
self.costs.default = self.costs.data
|
2018-08-11 19:38:51 +02:00
|
|
|
elif len(self.pitstops) > 0:
|
2018-08-11 14:53:08 +02:00
|
|
|
self.costs.default = self.pitstops[-1].costs
|
2018-08-11 19:38:51 +02:00
|
|
|
else:
|
|
|
|
self.costs.default = 0
|
2016-11-01 16:36:28 +01:00
|
|
|
|
|
|
|
def get_hint_messages(self):
|
|
|
|
messages = {
|
2018-08-11 14:53:08 +02:00
|
|
|
'litres': 'Litres must be higher than 0.01 L.',
|
2016-11-01 16:36:28 +01:00
|
|
|
'costs': 'Costs must be higher than 0.01 €.'
|
|
|
|
}
|
|
|
|
return messages
|
|
|
|
|
|
|
|
|