From e63a66bf57cc8aa88f69903f3c4544c47dea24f4 Mon Sep 17 00:00:00 2001 From: SergeantPanda Date: Wed, 7 May 2025 13:50:10 -0500 Subject: [PATCH] Change status to disabled when epg or m3u disabled. Change to idle when enabled. --- apps/epg/api_views.py | 20 +++++++++++--------- apps/epg/models.py | 3 +++ apps/epg/signals.py | 24 +++++++++++++++++++++++- apps/m3u/api_views.py | 20 +++++++++++--------- apps/m3u/models.py | 1 + apps/m3u/signals.py | 24 +++++++++++++++++++++++- 6 files changed, 72 insertions(+), 20 deletions(-) diff --git a/apps/epg/api_views.py b/apps/epg/api_views.py index 610af75c..70bfa87d 100644 --- a/apps/epg/api_views.py +++ b/apps/epg/api_views.py @@ -53,16 +53,18 @@ class EPGSourceViewSet(viewsets.ModelViewSet): return Response(serializer.data, status=status.HTTP_201_CREATED) def partial_update(self, request, *args, **kwargs): - """Handle PATCH requests with special handling for is_active toggle""" - # Check if this is just an is_active toggle - if len(request.data) == 1 and 'is_active' in request.data: - instance = self.get_object() - instance.is_active = request.data['is_active'] - instance.save(update_fields=['is_active']) - serializer = self.get_serializer(instance) - return Response(serializer.data) + """Handle partial updates with special logic for is_active field""" + instance = self.get_object() - # Otherwise, handle as normal update + # Check if we're toggling is_active + if 'is_active' in request.data and instance.is_active != request.data['is_active']: + # Set appropriate status based on new is_active value + if request.data['is_active']: + request.data['status'] = 'idle' + else: + request.data['status'] = 'disabled' + + # Continue with regular partial update return super().partial_update(request, *args, **kwargs) # ───────────────────────────── diff --git a/apps/epg/models.py b/apps/epg/models.py index ffbc3d16..ed8f2708 100644 --- a/apps/epg/models.py +++ b/apps/epg/models.py @@ -15,12 +15,15 @@ class EPGSource(models.Model): STATUS_PARSING = 'parsing' STATUS_ERROR = 'error' STATUS_SUCCESS = 'success' + STATUS_DISABLED = 'disabled' + STATUS_CHOICES = [ (STATUS_IDLE, 'Idle'), (STATUS_FETCHING, 'Fetching'), (STATUS_PARSING, 'Parsing'), (STATUS_ERROR, 'Error'), (STATUS_SUCCESS, 'Success'), + (STATUS_DISABLED, 'Disabled'), ] name = models.CharField(max_length=255, unique=True) diff --git a/apps/epg/signals.py b/apps/epg/signals.py index 82db7fad..601ec68b 100644 --- a/apps/epg/signals.py +++ b/apps/epg/signals.py @@ -1,4 +1,4 @@ -from django.db.models.signals import post_save, post_delete +from django.db.models.signals import post_save, post_delete, pre_save from django.dispatch import receiver from .models import EPGSource from .tasks import refresh_epg_data @@ -54,3 +54,25 @@ def delete_refresh_task(sender, instance, **kwargs): """ if instance.refresh_task: instance.refresh_task.delete() + +@receiver(pre_save, sender=EPGSource) +def update_status_on_active_change(sender, instance, **kwargs): + """ + When an EPGSource's is_active field changes, update the status accordingly. + """ + if instance.pk: # Only for existing records, not new ones + try: + # Get the current record from the database + old_instance = EPGSource.objects.get(pk=instance.pk) + + # If is_active changed, update the status + if old_instance.is_active != instance.is_active: + if instance.is_active: + # When activating, set status to idle + instance.status = 'idle' + else: + # When deactivating, set status to disabled + instance.status = 'disabled' + except EPGSource.DoesNotExist: + # New record, will use default status + pass diff --git a/apps/m3u/api_views.py b/apps/m3u/api_views.py index 2cd7df28..6176a0ca 100644 --- a/apps/m3u/api_views.py +++ b/apps/m3u/api_views.py @@ -95,16 +95,18 @@ class M3UAccountViewSet(viewsets.ModelViewSet): return response def partial_update(self, request, *args, **kwargs): - """Handle PATCH requests with special handling for is_active toggle""" - # Check if this is just an is_active toggle - if len(request.data) == 1 and 'is_active' in request.data: - instance = self.get_object() - instance.is_active = request.data['is_active'] - instance.save(update_fields=['is_active']) - serializer = self.get_serializer(instance) - return Response(serializer.data) + """Handle partial updates with special logic for is_active field""" + instance = self.get_object() - # Otherwise, handle as normal update + # Check if we're toggling is_active + if 'is_active' in request.data and instance.is_active != request.data['is_active']: + # Set appropriate status based on new is_active value + if request.data['is_active']: + request.data['status'] = M3UAccount.Status.IDLE + else: + request.data['status'] = M3UAccount.Status.DISABLED + + # Continue with regular partial update return super().partial_update(request, *args, **kwargs) class M3UFilterViewSet(viewsets.ModelViewSet): diff --git a/apps/m3u/models.py b/apps/m3u/models.py index eef76527..503ac3da 100644 --- a/apps/m3u/models.py +++ b/apps/m3u/models.py @@ -21,6 +21,7 @@ class M3UAccount(models.Model): ERROR = "error", "Error" SUCCESS = "success", "Success" PENDING_SETUP = "pending_setup", "Pending Setup" + DISABLED = "disabled", "Disabled" """Represents an M3U Account for IPTV streams.""" name = models.CharField( diff --git a/apps/m3u/signals.py b/apps/m3u/signals.py index c78328cf..e5951ff2 100644 --- a/apps/m3u/signals.py +++ b/apps/m3u/signals.py @@ -1,5 +1,5 @@ # apps/m3u/signals.py -from django.db.models.signals import post_save, post_delete +from django.db.models.signals import post_save, post_delete, pre_save from django.dispatch import receiver from .models import M3UAccount from .tasks import refresh_single_m3u_account, refresh_m3u_groups @@ -68,3 +68,25 @@ def delete_refresh_task(sender, instance, **kwargs): if instance.refresh_task: instance.refresh_task.interval.delete() instance.refresh_task.delete() + +@receiver(pre_save, sender=M3UAccount) +def update_status_on_active_change(sender, instance, **kwargs): + """ + When an M3UAccount's is_active field changes, update the status accordingly. + """ + if instance.pk: # Only for existing records, not new ones + try: + # Get the current record from the database + old_instance = M3UAccount.objects.get(pk=instance.pk) + + # If is_active changed, update the status + if old_instance.is_active != instance.is_active: + if instance.is_active: + # When activating, set status to idle + instance.status = M3UAccount.Status.IDLE + else: + # When deactivating, set status to disabled + instance.status = M3UAccount.Status.DISABLED + except M3UAccount.DoesNotExist: + # New record, will use default status + pass