attempting to get profiles to work properly

This commit is contained in:
kappa118 2025-02-28 09:47:40 -05:00
parent b8b9ed0fe0
commit 04080a5f2b
4 changed files with 43 additions and 15 deletions

View file

@ -13,8 +13,16 @@ router.register(r'profiles', M3UAccountViewSet, basename='m3u-account-profiles')
urlpatterns = [
path('refresh/', RefreshM3UAPIView.as_view(), name='m3u_refresh'),
path('refresh/<int:account_id>/', RefreshSingleM3UAPIView.as_view(), name='m3u_refresh_single'),
path('accounts/<int:m3u_account_id>/profiles/', M3UAccountProfileViewSet.as_view({'get': 'list', 'post': 'create'})),
path('accounts/<int:m3u_account_id>/profiles/<int:pk>/', M3UAccountProfileViewSet.as_view({'put': 'update', 'delete': 'destroy'})),
path('m3u/accounts/<int:account_id>/profiles/', M3UAccountProfileViewSet.as_view({
'get': 'list',
'post': 'create'
}), name='m3uaccountprofile-list'),
path('m3u/accounts/<int:account_id>/profiles/<int:pk>/', M3UAccountProfileViewSet.as_view({
'get': 'retrieve',
'put': 'update',
'patch': 'partial_update',
'delete': 'destroy'
}), name='m3uaccountprofile-detail'),
]
urlpatterns += router.urls

View file

@ -70,15 +70,18 @@ class UserAgentViewSet(viewsets.ModelViewSet):
permission_classes = [IsAuthenticated]
class M3UAccountProfileViewSet(viewsets.ModelViewSet):
queryset = M3UAccountProfile.objects.all()
serializer_class = M3UAccountProfileSerializer
permission_classes = [IsAuthenticated]
def get_queryset(self):
account_id = self.kwargs['account_id']
return M3UAccountProfile.objects.filter(account_id=account_id)
m3u_account_id = self.kwargs['account_id']
return M3UAccountProfile.objects.filter(m3u_account_id=m3u_account_id)
def perform_create(self, serializer):
account_id = self.kwargs['account_id']
account = M3UAccount.objects.get(id=account_id)
serializer.save(account=account)
m3u_account_id = self.kwargs['account_id']
try:
m3u_account = M3UAccount.objects.get(id=m3u_account_id)
except M3UAccount.DoesNotExist:
raise NotFound(f'M3UAccount with id {m3u_account_id} not found.')
serializer.save(m3u_account=m3u_account)

View file

@ -149,14 +149,20 @@ class ServerGroup(models.Model):
def __str__(self):
return self.name
from django.db import models
class M3UAccountProfile(models.Model):
"""Represents a profile associated with an M3U Account."""
m3u_account = models.ForeignKey(
'M3UAccount',
on_delete=models.CASCADE,
related_name='profiles',
help_text="The M3U account this profile belongs to."
)
name = models.CharField(
max_length=255,
help_text="Name for the M3U account profile"
)
m3u_account_id = models.ForeignKey(M3UAccount,
on_delete=models.CASCADE
)
max_streams = models.PositiveIntegerField(
default=0,
help_text="Maximum number of concurrent streams (0 for unlimited)"
@ -170,5 +176,8 @@ class M3UAccountProfile(models.Model):
class Meta:
constraints = [
models.UniqueConstraint(fields=['m3u_account_id', 'name'], name='unique_account_name')
models.UniqueConstraint(fields=['m3u_account', 'name'], name='unique_account_profile_name')
]
def __str__(self):
return f"{self.name} ({self.m3u_account.name})"

View file

@ -9,12 +9,20 @@ class M3UFilterSerializer(serializers.ModelSerializer):
model = M3UFilter
fields = ['id', 'filter_type', 'regex_pattern', 'exclude']
class M3UAccountProfileSerializer(serializers.ModelSerializer):
"""Serializer for M3U Account Profiles"""
from rest_framework import serializers
from .models import M3UAccountProfile
class M3UAccountProfileSerializer(serializers.ModelSerializer):
class Meta:
model = M3UAccountProfile
fields = ['id', 'name', 'm3u_account_id', 'search_pattern', 'replace_pattern']
fields = ['id', 'name', 'max_streams', 'search_pattern', 'replace_pattern']
read_only_fields = ['id']
def create(self, validated_data):
# Get the account from the context and assign it directly
m3u_account = self.context['m3u_account']
return M3UAccountProfile.objects.create(m3u_account=m3u_account, **validated_data)
class M3UAccountSerializer(serializers.ModelSerializer):
"""Serializer for M3U Account"""