49 lines
2.2 KiB
Python
49 lines
2.2 KiB
Python
from flask_wtf import FlaskForm
|
|
from wtforms import SelectField, StringField, TextAreaField, IntegerField, SubmitField, DateTimeLocalField
|
|
from wtforms.validators import DataRequired, Length, NumberRange, Optional, ValidationError
|
|
from flask_babel import lazy_gettext as _l
|
|
from datetime import datetime, timedelta
|
|
|
|
class ReservationForm(FlaskForm):
|
|
classroom_id = SelectField(_l('Classroom'), coerce=int, validators=[DataRequired()])
|
|
commission_id = SelectField(_l('Commission/Subject'), coerce=int, validators=[DataRequired()])
|
|
start_time = DateTimeLocalField(_l('Start Time'), format='%Y-%m-%dT%H:%M', validators=[DataRequired()])
|
|
end_time = DateTimeLocalField(_l('End Time'), format='%Y-%m-%dT%H:%M', validators=[DataRequired()])
|
|
purpose = StringField(_l('Purpose'), validators=[
|
|
DataRequired(),
|
|
Length(min=3, max=500)
|
|
])
|
|
expected_attendees = IntegerField(_l('Expected Attendees'), validators=[
|
|
DataRequired(),
|
|
NumberRange(min=1, max=500)
|
|
])
|
|
notes = TextAreaField(_l('Notes'), validators=[
|
|
Optional(),
|
|
Length(max=1000)
|
|
])
|
|
virtual_link = StringField(_l('Virtual Classroom Link'), validators=[
|
|
Optional(),
|
|
Length(max=500)
|
|
])
|
|
submit = SubmitField(_l('Make Reservation'))
|
|
|
|
def __init__(self, *args, is_edit=False, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.is_edit = is_edit
|
|
|
|
def validate_end_time(self, field):
|
|
if field.data <= self.start_time.data:
|
|
raise ValidationError('End time must be after start time.')
|
|
|
|
# Check if duration is reasonable (max 8 hours)
|
|
if field.data > self.start_time.data + timedelta(hours=8):
|
|
raise ValidationError('Reservation cannot exceed 8 hours.')
|
|
|
|
def validate_start_time(self, field):
|
|
# Don't allow new reservations in the past
|
|
if not self.is_edit and field.data < datetime.now() - timedelta(minutes=10):
|
|
raise ValidationError('Start time cannot be in the past.')
|
|
|
|
# Don't allow reservations too far in the future (max 3 months)
|
|
if field.data > datetime.now() + timedelta(days=90):
|
|
raise ValidationError('Reservations cannot be made more than 3 months in advance.') |