from flask_wtf import FlaskForm from wtforms import ( DateField, IntegerField, DecimalField, SubmitField, TextAreaField, StringField, ) from wtforms.validators import Length from .checks import * from wtforms.validators import Optional class DeleteRegularCostForm(FlaskForm): submit = SubmitField(label="Really delete this regular cost!") class EndRegularCostForm(FlaskForm): submit = SubmitField(label="Really end this regular cost!") class EditRegularCostForm(FlaskForm): start_at = DateField("Date of first instance") ends_at = DateField( "Date of last instance", validators=[Optional(strip_whitespace=True)] ) days = StringField("Days for instance", validators=[regular_costs_days_check]) costs = DecimalField("Costs (€, per instance)", places=2, validators=[costs_check]) description = TextAreaField("Description", validators=[Length(1, 4096)]) submit = SubmitField(label="Update it!") def preinit_with_data(self): if self.costs.data: self.costs.default = self.costs.data if self.start_at.data: self.start_at.default = self.start_at.data if self.ends_at.data: self.ends_at.default = self.ends_at.data if self.days.data: self.days.default = self.days.data if self.description.data: self.description.default = self.description.data def get_hint_messages(self): messages = {"costs": "Costs must be higher than 0.01 €."} return messages class CreateRegularCostForm(FlaskForm): start_at = DateField("Date of first instance") ends_at = DateField( "Date of last instance", validators=[Optional(strip_whitespace=True)] ) days = StringField("Days for instance", validators=[regular_costs_days_check]) costs = DecimalField("Costs (€, per instance)", places=2, validators=[costs_check]) description = TextAreaField("Description", validators=[Length(1, 4096)]) submit = SubmitField(label="Do it!") def preinit_with_data(self): if self.start_at.data: self.start_at.default = self.start_at.data else: self.start_at.default = date.today() if self.ends_at.data: self.ends_at.default = self.ends_at.data else: self.ends_at.default = None if self.days.data: self.days.default = self.days.data 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