mirror of
https://github.com/eduardogsilva/wireguard_webadmin.git
synced 2026-02-20 11:36:18 +00:00
improve time interval forms
This commit is contained in:
@@ -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
|
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 ScheduleSlotForm(forms.ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = ScheduleSlot
|
model = ScheduleSlot
|
||||||
@@ -98,7 +109,7 @@ class ScheduleSlotForm(forms.ModelForm):
|
|||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
cleaned = super().clean()
|
cleaned = super().clean()
|
||||||
|
week_minutes = 7 * 24 * 60
|
||||||
start_weekday = cleaned.get("start_weekday")
|
start_weekday = cleaned.get("start_weekday")
|
||||||
start_time = cleaned.get("start_time")
|
start_time = cleaned.get("start_time")
|
||||||
end_weekday = cleaned.get("end_weekday")
|
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)
|
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)
|
qs = ScheduleSlot.objects.filter(profile=self.profile)
|
||||||
if self.instance and self.instance.pk:
|
if self.instance and self.instance.pk:
|
||||||
qs = qs.exclude(pk=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
|
return cleaned
|
||||||
@@ -86,7 +86,7 @@ def view_manage_scheduler_slot(request):
|
|||||||
cancel_url = f"{reverse('manage_scheduler_profile')}?uuid={profile.uuid}"
|
cancel_url = f"{reverse('manage_scheduler_profile')}?uuid={profile.uuid}"
|
||||||
|
|
||||||
if request.method == 'POST':
|
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():
|
if form.is_valid():
|
||||||
new_slot = form.save(commit=False)
|
new_slot = form.save(commit=False)
|
||||||
new_slot.profile = profile
|
new_slot.profile = profile
|
||||||
@@ -94,7 +94,7 @@ def view_manage_scheduler_slot(request):
|
|||||||
messages.success(request, _('Time Interval saved successfully.'))
|
messages.success(request, _('Time Interval saved successfully.'))
|
||||||
return redirect(cancel_url)
|
return redirect(cancel_url)
|
||||||
else:
|
else:
|
||||||
form = ScheduleSlotForm(instance=slot, cancel_url=cancel_url)
|
form = ScheduleSlotForm(instance=slot, cancel_url=cancel_url, profile=profile)
|
||||||
|
|
||||||
context = {
|
context = {
|
||||||
'form': form,
|
'form': form,
|
||||||
@@ -123,4 +123,4 @@ def view_delete_scheduler_slot(request):
|
|||||||
'cancel_url': cancel_url,
|
'cancel_url': cancel_url,
|
||||||
'text': _('Are you sure you want to delete this time interval?')
|
'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)
|
||||||
|
|||||||
@@ -74,9 +74,9 @@
|
|||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-12">
|
<div class="col-12">
|
||||||
<ul>
|
<ul>
|
||||||
<li>{% trans 'Current time' %}: {{ now }}</li>
|
<li><b>{% trans 'Current time' %}:</b> {{ now }}</li>
|
||||||
<li>{% trans 'Becomes active at' %}: {{ profile.next_dates.enable|default_if_none:'' }}</li>
|
<li><b>{% trans 'Becomes active at' %}:</b> {{ profile.next_dates.enable|default_if_none:'' }}</li>
|
||||||
<li>{% trans 'Becomes inactive at' %}: {{ profile.next_dates.disable|default_if_none:'' }}</li>
|
<li><b>{% trans 'Becomes inactive at' %}:</b> {{ profile.next_dates.disable|default_if_none:'' }}</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user