2016-04-23 23:08:39 +02:00
|
|
|
from flask_wtf import Form
|
2016-06-26 08:19:28 +02:00
|
|
|
from wtforms import DateField, IntegerField, DecimalField, StringField, SelectField, SubmitField, SelectMultipleField, BooleanField
|
2016-04-23 23:24:25 +02:00
|
|
|
from wtforms.validators import ValidationError, Length
|
2016-05-03 22:21:57 +02:00
|
|
|
from datetime import date
|
2016-04-23 23:08:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
def date_check(form, field):
|
2016-05-03 22:21:57 +02:00
|
|
|
if field.data < 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())
|
2016-04-23 23:08:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
def odometer_check(form, field):
|
2016-05-03 22:21:57 +02:00
|
|
|
if field.data <= form.last_pitstop.odometer:
|
|
|
|
raise ValidationError('The new odometer value must be higher than %i km' % form.last_pitstop.odometer)
|
2016-04-23 23:08:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
|
2016-06-21 21:06:41 +02:00
|
|
|
def costs_check(form, field):
|
|
|
|
if field.data is not None and field.data <= 0:
|
|
|
|
raise ValidationError('Costs must be above 0.01 €.')
|
|
|
|
|
|
|
|
|
2016-06-24 07:15:24 +02:00
|
|
|
def edit_costs_check(form, field):
|
|
|
|
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 €.')
|
|
|
|
|
2016-06-26 08:19:28 +02:00
|
|
|
|
2016-04-24 14:09:41 +02:00
|
|
|
class SelectVehicleForm(Form):
|
2016-04-23 23:24:25 +02:00
|
|
|
vehicle = SelectField('Vehicle', coerce=int)
|
2016-04-27 07:23:28 +02:00
|
|
|
submit = SubmitField(label='Do it!')
|
2016-04-24 14:09:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CreatePitstopForm(Form):
|
2016-04-23 23:08:39 +02:00
|
|
|
date = DateField('Date of Pitstop', validators=[date_check])
|
|
|
|
odometer = IntegerField('Odometer (km)', validators=[odometer_check])
|
2016-04-27 07:23:28 +02:00
|
|
|
litres = DecimalField('Litres (l)', places=2, validators=[litres_check])
|
2016-06-21 21:06:41 +02:00
|
|
|
costs = DecimalField('Costs (€, overall)', places=2, validators=[costs_check])
|
2016-04-27 07:23:28 +02:00
|
|
|
submit = SubmitField(label='Do it!')
|
2016-05-03 22:21:57 +02:00
|
|
|
last_pitstop = None
|
2016-04-23 23:08:39 +02:00
|
|
|
|
2016-05-03 22:21:57 +02:00
|
|
|
def set_pitstop(self, last_pitstop):
|
|
|
|
self.last_pitstop = last_pitstop
|
2016-04-23 23:08:39 +02:00
|
|
|
|
|
|
|
|
2016-04-23 23:24:25 +02:00
|
|
|
class EditVehicleForm(Form):
|
|
|
|
name = StringField('Name', validators=[Length(1, 255)])
|
2016-06-26 08:19:28 +02:00
|
|
|
#consumables = SelectMultipleField('Consumables')
|
2016-04-27 07:23:28 +02:00
|
|
|
submit = SubmitField(label='Do it!')
|
2016-04-23 23:24:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
class DeleteVehicleForm(Form):
|
2016-04-27 07:23:28 +02:00
|
|
|
submit = SubmitField(label='Do it!')
|
2016-05-24 23:55:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
class DeleteAccountForm(Form):
|
2016-05-25 09:40:20 +02:00
|
|
|
submit = SubmitField(label='Really delete my account!')
|
2016-06-22 22:48:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
class DeletePitStopForm(Form):
|
|
|
|
submit = SubmitField(label='Really delete this pitstop!')
|
2016-06-24 07:15:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
class EditPitstopForm(Form):
|
|
|
|
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
|
|
|
|
|
|
|
|
def set_pitstop(self, last_pitstop):
|
|
|
|
self.last_pitstop = last_pitstop
|
|
|
|
|
|
|
|
|
2016-06-26 08:19:28 +02:00
|
|
|
class CreateConsumableForm(Form):
|
|
|
|
name = StringField('Name', validators=[Length(1, 255)])
|
|
|
|
unit = StringField('Unit', validators=[Length(1, 255)])
|
|
|
|
submit = SubmitField(label='Do it!')
|
|
|
|
|