Change status to disabled when epg or m3u disabled. Change to idle when enabled.

This commit is contained in:
SergeantPanda 2025-05-07 13:50:10 -05:00
parent b84d1cc61a
commit e63a66bf57
6 changed files with 72 additions and 20 deletions

View file

@ -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)
# ─────────────────────────────

View file

@ -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)

View file

@ -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

View file

@ -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):

View file

@ -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(

View file

@ -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