mirror of
https://github.com/eduardogsilva/wireguard_webadmin.git
synced 2026-02-19 19:26:17 +00:00
Add active field to ScheduleProfile and implement signals for status updates
This commit is contained in:
@@ -4,3 +4,6 @@ from django.apps import AppConfig
|
||||
class SchedulerConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'scheduler'
|
||||
|
||||
def ready(self):
|
||||
pass
|
||||
18
scheduler/migrations/0004_scheduleprofile_active.py
Normal file
18
scheduler/migrations/0004_scheduleprofile_active.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 5.2.11 on 2026-02-04 18:35
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('scheduler', '0003_remove_peerscheduling_schedule_last_calculated_at'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='scheduleprofile',
|
||||
name='active',
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
]
|
||||
@@ -19,6 +19,7 @@ WEEK_DAYS = [
|
||||
|
||||
class ScheduleProfile(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
active = models.BooleanField(default=False)
|
||||
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
updated = models.DateTimeField(auto_now=True)
|
||||
|
||||
29
scheduler/signals.py
Normal file
29
scheduler/signals.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from django.db import transaction
|
||||
from django.db.models.signals import post_save, post_delete
|
||||
from django.dispatch import receiver
|
||||
|
||||
from .models import ScheduleSlot, ScheduleProfile, PeerScheduling
|
||||
|
||||
|
||||
def _recalc_profile_and_reset_peers(profile_id: int | None) -> None:
|
||||
if not profile_id:
|
||||
return
|
||||
|
||||
has_slots = ScheduleSlot.objects.filter(profile_id=profile_id).exists()
|
||||
|
||||
ScheduleProfile.objects.filter(pk=profile_id).update(active=has_slots)
|
||||
|
||||
PeerScheduling.objects.filter(profile_id=profile_id).update(
|
||||
next_scheduled_enable_at=None,
|
||||
next_scheduled_disable_at=None,
|
||||
)
|
||||
|
||||
|
||||
@receiver(post_save, sender=ScheduleSlot)
|
||||
def scheduleslot_post_save(sender, instance: ScheduleSlot, created: bool, **kwargs):
|
||||
transaction.on_commit(lambda: _recalc_profile_and_reset_peers(instance.profile_id))
|
||||
|
||||
|
||||
@receiver(post_delete, sender=ScheduleSlot)
|
||||
def scheduleslot_post_delete(sender, instance: ScheduleSlot, **kwargs):
|
||||
transaction.on_commit(lambda: _recalc_profile_and_reset_peers(instance.profile_id))
|
||||
@@ -74,9 +74,13 @@
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<ul>
|
||||
<li><b>{% trans 'Current time' %}:</b> {{ now }}</li>
|
||||
<li><b>{% trans 'Becomes active at' %}:</b> {{ profile.next_dates.enable|default_if_none:'' }}</li>
|
||||
<li><b>{% trans 'Becomes inactive at' %}:</b> {{ profile.next_dates.disable|default_if_none:'' }}</li>
|
||||
{% if profile.active %}
|
||||
<li><b>{% trans 'Current time' %}:</b> {{ now }}</li>
|
||||
<li><b>{% trans 'Becomes active at' %}:</b> {{ profile.next_dates.enable|default_if_none:'' }}</li>
|
||||
<li><b>{% trans 'Becomes inactive at' %}:</b> {{ profile.next_dates.disable|default_if_none:'' }}</li>
|
||||
{% else %}
|
||||
<li><b>{% trans 'Status' %}:</b> {% trans 'Inactive' %}</li>
|
||||
{% endif %}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -49,7 +49,7 @@ INSTALLED_APPS = [
|
||||
'cluster',
|
||||
'api',
|
||||
'routing_templates',
|
||||
'scheduler'
|
||||
'scheduler.apps.SchedulerConfig'
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
|
||||
Reference in New Issue
Block a user