rollerverbrauch/app/forms/service.py

76 lines
2.4 KiB
Python

from flask_wtf import FlaskForm
from wtforms import DateField, IntegerField, DecimalField, SubmitField, TextAreaField
from wtforms.validators import Length
from .checks import *
class CreateServiceForm(FlaskForm):
date = DateField('Date of Service')
odometer = IntegerField('Odometer (km)', validators=[odometer_date_check])
costs = DecimalField('Costs (€, overall)', places=2, validators=[costs_check])
description = TextAreaField('Description', validators=[Length(1, 4096)])
submit = SubmitField(label='Do it!')
pitstops = []
def set_pitstops(self, pitstops):
self.pitstops = pitstops
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
elif len(self.pitstops) > 0:
self.odometer.default = self.pitstops[-1].odometer
else:
self.odometer.default = 0
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
class DeleteServiceForm(FlaskForm):
submit = SubmitField(label='Really delete this service!')
class EditServiceForm(FlaskForm):
date = DateField('Date of Service')
odometer = IntegerField('Odometer (km)', validators=[edit_odometer_date_check])
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
pitstops = []
def set_pitstops(self, pitstops):
self.pitstops = pitstops
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
if self.description.data:
self.description.default = self.description.data
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