from flask_wtf import FlaskForm from wtforms import StringField, SubmitField, DateTimeField, BooleanField, HiddenField from wtforms.validators import InputRequired, DataRequired, ValidationError, Length from pytz import timezone from app.models import Game class CreateGameForm(FlaskForm): game_name = StringField('Game Name', validators=[InputRequired(), Length(min=0, max=64)]) start_time_disabled = BooleanField('No start time') start_time = DateTimeField(id='datetimepicker_start', format="%d-%m-%Y %H:%M") end_time_disabled = BooleanField('No end time') end_time = DateTimeField(id='datetimepicker_end', format="%d-%m-%Y %H:%M") timezone = HiddenField(validators=[DataRequired()]) submit = SubmitField('Create') old_name = '' def validate_start_time(self, start_time): self.date_time_validator(self.start_time_disabled, start_time) def validate_end_time(self, end_time): self.date_time_validator(self.end_time_disabled, end_time) if self.start_time.data and end_time.data: if self.start_time.data > end_time.data: raise ValidationError('Start Time must be before End Time.') def date_time_validator(self, disabled, date_time): print(1) if disabled.data: date_time.data = None return clientzone = timezone(self.timezone.data) print(clientzone) print(date_time.data) date_time_utc = clientzone.localize(date_time.data).astimezone(timezone('UTC')) date_time.data = date_time_utc print(date_time.data) def validate_game_name(self, game_name): if game_name.data == '': return if game_name.data == self.old_name: return game = Game.query.filter_by(name=game_name.data).first() if game is not None: raise ValidationError('Please use a different name.')