From 1db04a222f3f16c52d489f4847fbe839775cce29 Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Wed, 4 Feb 2026 15:07:28 -0300 Subject: [PATCH] improve time interval forms --- scheduler/forms.py | 27 ++++++++++++++++++- scheduler/views.py | 6 ++--- templates/scheduler/scheduleprofile_form.html | 6 ++--- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/scheduler/forms.py b/scheduler/forms.py index 52425e8..346aae4 100644 --- a/scheduler/forms.py +++ b/scheduler/forms.py @@ -51,6 +51,17 @@ def _intervals_overlap(a_start, a_end, b_start, b_end) -> bool: return a_start < b_end and b_start < a_end +def _circular_gap(a_start, a_end, b_start, b_end, week_minutes): + if a_start < b_end and b_start < a_end: + return 0 + + gaps = [ + (b_start - a_end) % week_minutes, + (a_start - b_end) % week_minutes, + ] + return min(gaps) + + class ScheduleSlotForm(forms.ModelForm): class Meta: model = ScheduleSlot @@ -98,7 +109,7 @@ class ScheduleSlotForm(forms.ModelForm): def clean(self): cleaned = super().clean() - + week_minutes = 7 * 24 * 60 start_weekday = cleaned.get("start_weekday") start_time = cleaned.get("start_time") end_weekday = cleaned.get("end_weekday") @@ -112,6 +123,14 @@ class ScheduleSlotForm(forms.ModelForm): new_intervals = _slot_to_week_intervals(start_weekday, start_time, end_weekday, end_time) + total_minutes = sum((end - start) for start, end in new_intervals) + + if total_minutes < 10: + raise ValidationError(_("The minimum duration between start and end must be at least 10 minutes.")) + + if total_minutes > (week_minutes - 10): + raise ValidationError(_("The minimum duration between start and end must be at least 10 minutes.")) + qs = ScheduleSlot.objects.filter(profile=self.profile) if self.instance and self.instance.pk: qs = qs.exclude(pk=self.instance.pk) @@ -135,4 +154,10 @@ class ScheduleSlotForm(forms.ModelForm): }, ) + gap = _circular_gap(ns, ne, es, ee, week_minutes) + if gap < 10: + raise ValidationError( + _("There must be at least 10 minutes between time slots.") + ) + return cleaned \ No newline at end of file diff --git a/scheduler/views.py b/scheduler/views.py index bfdb6d3..94d6890 100644 --- a/scheduler/views.py +++ b/scheduler/views.py @@ -86,7 +86,7 @@ def view_manage_scheduler_slot(request): cancel_url = f"{reverse('manage_scheduler_profile')}?uuid={profile.uuid}" if request.method == 'POST': - form = ScheduleSlotForm(request.POST, instance=slot, cancel_url=cancel_url) + form = ScheduleSlotForm(request.POST, instance=slot, cancel_url=cancel_url, profile=profile) if form.is_valid(): new_slot = form.save(commit=False) new_slot.profile = profile @@ -94,7 +94,7 @@ def view_manage_scheduler_slot(request): messages.success(request, _('Time Interval saved successfully.')) return redirect(cancel_url) else: - form = ScheduleSlotForm(instance=slot, cancel_url=cancel_url) + form = ScheduleSlotForm(instance=slot, cancel_url=cancel_url, profile=profile) context = { 'form': form, @@ -123,4 +123,4 @@ def view_delete_scheduler_slot(request): 'cancel_url': cancel_url, 'text': _('Are you sure you want to delete this time interval?') } - return render(request, 'scheduler/generic_delete_confirm.html', context) + return render(request, 'generic_delete_confirmation.html', context) diff --git a/templates/scheduler/scheduleprofile_form.html b/templates/scheduler/scheduleprofile_form.html index 5bb6447..698d64e 100644 --- a/templates/scheduler/scheduleprofile_form.html +++ b/templates/scheduler/scheduleprofile_form.html @@ -74,9 +74,9 @@
    -
  • {% trans 'Current time' %}: {{ now }}
  • -
  • {% trans 'Becomes active at' %}: {{ profile.next_dates.enable|default_if_none:'' }}
  • -
  • {% trans 'Becomes inactive at' %}: {{ profile.next_dates.disable|default_if_none:'' }}
  • +
  • {% trans 'Current time' %}: {{ now }}
  • +
  • {% trans 'Becomes active at' %}: {{ profile.next_dates.enable|default_if_none:'' }}
  • +
  • {% trans 'Becomes inactive at' %}: {{ profile.next_dates.disable|default_if_none:'' }}