mirror of
https://github.com/eduardogsilva/wireguard_webadmin.git
synced 2026-02-20 03:36:16 +00:00
Add next_dates property to ScheduleProfile for upcoming schedule times
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
import uuid
|
import uuid
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.utils import timezone
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from wireguard.models import Peer
|
from wireguard.models import Peer
|
||||||
@@ -25,6 +27,51 @@ class ScheduleProfile(models.Model):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _next_weekday_time(now, weekday, t):
|
||||||
|
|
||||||
|
tz = timezone.get_current_timezone()
|
||||||
|
|
||||||
|
today = timezone.localdate(now)
|
||||||
|
candidate_date = today + timedelta(days=(weekday - today.weekday()) % 7)
|
||||||
|
|
||||||
|
candidate = timezone.make_aware(
|
||||||
|
datetime.combine(candidate_date, t),
|
||||||
|
tz
|
||||||
|
)
|
||||||
|
|
||||||
|
if candidate <= now:
|
||||||
|
candidate_date += timedelta(days=7)
|
||||||
|
candidate = timezone.make_aware(
|
||||||
|
datetime.combine(candidate_date, t),
|
||||||
|
tz
|
||||||
|
)
|
||||||
|
|
||||||
|
return candidate
|
||||||
|
|
||||||
|
@property
|
||||||
|
def next_dates(self):
|
||||||
|
slots = list(self.time_interval.all())
|
||||||
|
if not slots:
|
||||||
|
return {'enable': None, 'disable': None}
|
||||||
|
|
||||||
|
now = timezone.now()
|
||||||
|
|
||||||
|
start_candidates = []
|
||||||
|
end_candidates = []
|
||||||
|
|
||||||
|
for slot in slots:
|
||||||
|
start_dt = ScheduleProfile._next_weekday_time(now, slot.start_weekday, slot.start_time)
|
||||||
|
end_dt = ScheduleProfile._next_weekday_time(now, slot.end_weekday, slot.end_time)
|
||||||
|
|
||||||
|
start_candidates.append(start_dt)
|
||||||
|
end_candidates.append(end_dt)
|
||||||
|
|
||||||
|
return {
|
||||||
|
'enable': min(start_candidates) if start_candidates else None,
|
||||||
|
'disable': min(end_candidates) if end_candidates else None,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class ScheduleSlot(models.Model):
|
class ScheduleSlot(models.Model):
|
||||||
profile = models.ForeignKey(ScheduleProfile, on_delete=models.CASCADE, related_name="time_interval")
|
profile = models.ForeignKey(ScheduleProfile, on_delete=models.CASCADE, related_name="time_interval")
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ from django.contrib import messages
|
|||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
from django.shortcuts import render, get_object_or_404, redirect
|
from django.shortcuts import render, get_object_or_404, redirect
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
from django.utils import timezone
|
||||||
from django.utils.translation import gettext as _
|
from django.utils.translation import gettext as _
|
||||||
|
|
||||||
from scheduler.forms import ScheduleProfileForm
|
from scheduler.forms import ScheduleProfileForm
|
||||||
@@ -44,6 +45,7 @@ def view_manage_scheduler_profile(request):
|
|||||||
'title': title,
|
'title': title,
|
||||||
'profile': profile,
|
'profile': profile,
|
||||||
'slots': slots,
|
'slots': slots,
|
||||||
|
'now': timezone.now(),
|
||||||
}
|
}
|
||||||
return render(request, 'scheduler/scheduleprofile_form.html', context)
|
return render(request, 'scheduler/scheduleprofile_form.html', context)
|
||||||
|
|
||||||
|
|||||||
@@ -70,6 +70,17 @@
|
|||||||
<span><i class="fas fa-circle" style="color: #28a745;"></i> {% trans "Active" %}</span>
|
<span><i class="fas fa-circle" style="color: #28a745;"></i> {% trans "Active" %}</span>
|
||||||
<span><i class="fas fa-circle" style="color: #dc3545;"></i> {% trans "Inactive" %}</span>
|
<span><i class="fas fa-circle" style="color: #dc3545;"></i> {% trans "Inactive" %}</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-12">
|
||||||
|
<ul>
|
||||||
|
<li>{% trans 'Current time' %}: {{ now }}</li>
|
||||||
|
<li>{% trans 'Becomes active at' %}: {{ profile.next_dates.enable|default_if_none:'' }}</li>
|
||||||
|
<li>{% trans 'Becomes inactive at' %}: {{ profile.next_dates.disable|default_if_none:'' }}</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user