diff --git a/core/__pycache__/forms.cpython-311.pyc b/core/__pycache__/forms.cpython-311.pyc index f805a3e..f417e72 100644 Binary files a/core/__pycache__/forms.cpython-311.pyc and b/core/__pycache__/forms.cpython-311.pyc differ diff --git a/core/__pycache__/views.cpython-311.pyc b/core/__pycache__/views.cpython-311.pyc index 148c612..e7615ff 100644 Binary files a/core/__pycache__/views.cpython-311.pyc and b/core/__pycache__/views.cpython-311.pyc differ diff --git a/core/forms.py b/core/forms.py index 97aca69..8bc2105 100644 --- a/core/forms.py +++ b/core/forms.py @@ -1,8 +1,21 @@ +from datetime import timedelta + from django import forms from .models import Event +WEEKDAY_CHOICES = [ + ('0', 'Monday'), + ('1', 'Tuesday'), + ('2', 'Wednesday'), + ('3', 'Thursday'), + ('4', 'Friday'), + ('5', 'Saturday'), + ('6', 'Sunday'), +] + + class EventForm(forms.ModelForm): start = forms.DateTimeField( input_formats=['%Y-%m-%dT%H:%M'], @@ -18,6 +31,25 @@ class EventForm(forms.ModelForm): format='%Y-%m-%dT%H:%M', ), ) + recurrence_start_date = forms.DateField( + required=False, + label='Repeat from', + widget=forms.DateInput(attrs={'type': 'date', 'class': 'form-control'}), + help_text='Optional: first date in the weekly recurrence window.', + ) + recurrence_end_date = forms.DateField( + required=False, + label='Repeat until', + widget=forms.DateInput(attrs={'type': 'date', 'class': 'form-control'}), + help_text='Optional: last date in the weekly recurrence window.', + ) + recurrence_weekday = forms.ChoiceField( + required=False, + label='Weekday', + choices=[('', 'Select a weekday')] + WEEKDAY_CHOICES, + widget=forms.Select(attrs={'class': 'form-select'}), + help_text='Optional: create one event each week on this weekday.', + ) class Meta: model = Event @@ -46,4 +78,28 @@ class EventForm(forms.ModelForm): end = cleaned_data.get('end') if start and end and end <= start: self.add_error('end', 'End time must be after the start time.') + + recurrence_start_date = cleaned_data.get('recurrence_start_date') + recurrence_end_date = cleaned_data.get('recurrence_end_date') + recurrence_weekday = cleaned_data.get('recurrence_weekday') + recurrence_requested = any([recurrence_start_date, recurrence_end_date, recurrence_weekday]) + + if recurrence_requested: + if not recurrence_start_date: + self.add_error('recurrence_start_date', 'Enter the first date for the recurring series.') + if not recurrence_end_date: + self.add_error('recurrence_end_date', 'Enter the last date for the recurring series.') + if not recurrence_weekday: + self.add_error('recurrence_weekday', 'Choose which weekday should repeat each week.') + + if recurrence_start_date and recurrence_end_date and recurrence_end_date < recurrence_start_date: + self.add_error('recurrence_end_date', 'The recurring series must end on or after the start date.') + + if recurrence_start_date and recurrence_end_date and recurrence_weekday: + weekday_index = int(recurrence_weekday) + days_until_first = (weekday_index - recurrence_start_date.weekday()) % 7 + first_occurrence = recurrence_start_date + timedelta(days=days_until_first) + if first_occurrence > recurrence_end_date: + self.add_error('recurrence_weekday', 'No selected weekday falls inside that recurrence date range.') + return cleaned_data diff --git a/core/templates/core/event_form.html b/core/templates/core/event_form.html index f90b3fe..29f2bf1 100644 --- a/core/templates/core/event_form.html +++ b/core/templates/core/event_form.html @@ -16,23 +16,81 @@