forms.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import datetime
  2. from django import forms
  3. from django.utils.translation import gettext_lazy as _
  4. from .widgets import DatePickerWidget, SlotPickerWidget
  5. def get_hours():
  6. return [(x, datetime.time(x, 0)) for x in range(24)]
  7. def get_interval():
  8. return [(x, datetime.timedelta(minutes=x)) for x in [15, 30, 60]]
  9. class CreateEventForm(forms.Form):
  10. event_name = forms.CharField(label=_('Event name'), max_length=100, help_text=_('The name of your event. This will be displayed to participants.'))
  11. event_date = forms.Field(label=_('Event date'), widget=DatePickerWidget)
  12. start_time = forms.ChoiceField(label=_('Start time'), initial=9, choices=get_hours, help_text=_('The first time slot available for participants to select.'))
  13. end_time = forms.ChoiceField(label=_('End time'), initial=20, choices=get_hours, help_text=_('The last time slot available for participants to select.'))
  14. slot_interval = forms.ChoiceField(label=_('Time slot interval'), initial=15, choices=get_interval, help_text=_('The interval between time slots.'))
  15. def clean(self) -> dict:
  16. cleaned_data = super().clean()
  17. if (not "start_time" in cleaned_data) or (not "end_time" in cleaned_data):
  18. return cleaned_data
  19. if cleaned_data['start_time'] == cleaned_data['end_time']:
  20. self.add_error('start_time', _('Start time must be different from end time'))
  21. return cleaned_data
  22. class LoginForm(forms.Form):
  23. username = forms.CharField(label=_('Username'), max_length=100, help_text=_('This will be displayed to other participants.'))
  24. password = forms.CharField(label=_('Password'), max_length=100, widget=forms.PasswordInput, required=False, help_text=_('Fill in a password to create a new account. Leave empty to login as a guest.'))
  25. class UpdateSlotsForm(forms.Form):
  26. slots = forms.Field(label=_('Slots'), help_text=_('Click on a time slot to select it. Click again to deselect it. You can also drag the mouse to select multiple slots.'))
  27. def __init__(self, *args, event, n_slots, participant, **kwargs):
  28. super().__init__(*args, **kwargs)
  29. self.fields['slots'].widget = SlotPickerWidget(event=event, n_slots=n_slots, participant=participant)