from flask_wtf import FlaskForm from wtforms import DateField, IntegerField, DecimalField, SubmitField from .checks import * class DeletePitStopForm(FlaskForm): submit = SubmitField(label='Really delete this pitstop!') class EditPitstopForm(FlaskForm): date = DateField('Date of Pitstop', validators=[date_check]) odometer = IntegerField('Odometer (km)', validators=[odometer_check]) litres = DecimalField('Litres (l)', places=2, validators=[litres_check]) costs = DecimalField('Costs (€, overall)', places=2, validators=[edit_costs_check]) submit = SubmitField(label='Update it!') last_pitstop = None same_odometer_allowed = True def set_pitstop(self, last_pitstop): self.last_pitstop = last_pitstop 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 class CreatePitstopForm(FlaskForm): date = DateField('Date of Pitstop') odometer = IntegerField('Odometer (km)', validators=[odometer_date_check]) 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 pitstops = [] def set_pitstops(self, pitstops): self.pitstops = pitstops 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() if self.odometer.data: self.odometer.default = self.odometer.data else: self.odometer.default = self.pitstops[-1].odometer if self.litres.data: self.litres.default = self.litres.data else: self.litres.default = self.pitstops[-1].amount if self.costs.data: self.costs.default = self.costs.data else: self.costs.default = self.pitstops[-1].costs 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