223 lines
7.7 KiB
Python
223 lines
7.7 KiB
Python
# -*- encoding: utf-8 -*-
|
|
|
|
import logging
|
|
|
|
from django.db.utils import IntegrityError
|
|
from django_filters import rest_framework as rest_filters
|
|
from rest_framework import status, permissions, filters, generics
|
|
from rest_framework.response import Response
|
|
from rest_framework.pagination import PageNumberPagination
|
|
from rest_framework.authentication import SessionAuthentication, TokenAuthentication
|
|
from rest_framework.parsers import MultiPartParser, FormParser
|
|
|
|
from apps.home.models import Subscription, SubscriptionChannel, TrackedContent
|
|
from .serializers import (
|
|
SubscriptionSerializer,
|
|
SubscriptionChannelSerializerGET,
|
|
SubscriptionChannelSerializerPOST,
|
|
TrackedContentSerializer
|
|
)
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
class DefaultPagination(PageNumberPagination):
|
|
"""Default class for pagination in API views."""
|
|
|
|
page_size = 10
|
|
page_size_query_param = "page_size"
|
|
max_page_size = 25
|
|
|
|
|
|
# =================================================================================================
|
|
# Subscription Views
|
|
|
|
class Subscription_ListView(generics.ListCreateAPIView):
|
|
"""
|
|
View to provide a list of Subscription model instances.
|
|
Can also be used to create a new instance.
|
|
|
|
Supports: GET, POST
|
|
"""
|
|
|
|
authentication_classes = [SessionAuthentication, TokenAuthentication]
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
pagination_class = DefaultPagination
|
|
serializer_class = SubscriptionSerializer
|
|
queryset = Subscription.objects.all().order_by("-creation_datetime")
|
|
|
|
filter_backends = [filters.SearchFilter, rest_filters.DjangoFilterBackend, filters.OrderingFilter]
|
|
filterset_fields = ["uuid", "name", "rss_url", "server", "creation_datetime"]
|
|
search_fields = ["name"]
|
|
ordering_fields = ["creation_datetime"]
|
|
|
|
def post(self, request):
|
|
serializer = self.get_serializer(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
try:
|
|
self.perform_create(serializer)
|
|
except IntegrityError:
|
|
return Response(
|
|
{"detail": "Subscription name must be unique"},
|
|
status=status.HTTP_409_CONFLICT,
|
|
exception=True
|
|
)
|
|
|
|
headers = self.get_success_headers(serializer.data)
|
|
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|
|
|
|
|
|
class Subscription_DetailView(generics.RetrieveUpdateDestroyAPIView):
|
|
"""
|
|
View to provide details on a particular Subscription model instances.
|
|
|
|
Supports: GET, DELETE
|
|
"""
|
|
|
|
authentication_classes = [SessionAuthentication, TokenAuthentication]
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
parser_classes = [MultiPartParser, FormParser]
|
|
|
|
serializer_class = SubscriptionSerializer
|
|
queryset = Subscription.objects.all().order_by("-creation_datetime")
|
|
|
|
|
|
# =================================================================================================
|
|
# SubscriptionChannel Views
|
|
|
|
class SubscriptionChannel_ListView(generics.ListCreateAPIView):
|
|
"""
|
|
View to provide a list of SubscriptionChannel model instances.
|
|
Can also be used to create a new instance.
|
|
|
|
Supports: GET, POST
|
|
"""
|
|
|
|
authentication_classes = [SessionAuthentication, TokenAuthentication]
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
pagination_class = DefaultPagination
|
|
# serializer_class = SubscriptionChannelSerializer
|
|
queryset = SubscriptionChannel.objects.all().order_by("-creation_datetime")
|
|
|
|
filter_backends = [rest_filters.DjangoFilterBackend, filters.OrderingFilter]
|
|
filterset_fields = ["uuid", "id", "subscription", "subscription__server"]
|
|
ordering_fields = ["creation_datetime"]
|
|
|
|
def get_serializer(self, *args, **kwargs):
|
|
if self.request.method == "POST":
|
|
return SubscriptionChannelSerializerPOST(*args, **kwargs)
|
|
else:
|
|
return SubscriptionChannelSerializerGET(*args, **kwargs)
|
|
|
|
def post(self, request):
|
|
serializer = self.get_serializer(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
try:
|
|
self.perform_create(serializer)
|
|
except IntegrityError as exc:
|
|
log.error(exc)
|
|
return Response(
|
|
{"detail": "Duplicates not allowed"},
|
|
status=status.HTTP_409_CONFLICT,
|
|
exception=True
|
|
)
|
|
|
|
# Switch to the GET serializer for return data, as it contains more data on the subscription.
|
|
# We can't modify the POST serializer to contain this data, otherwise it expects it in POST.
|
|
self.request.method = "GET"
|
|
serializer = self.get_serializer(serializer.instance)
|
|
|
|
headers = self.get_success_headers(serializer.data)
|
|
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|
|
|
|
|
|
class SubscriptionChannel_DetailView(generics.RetrieveDestroyAPIView):
|
|
"""
|
|
View to provide details on a particular SubscriptionChannel model instances.
|
|
|
|
Supports: GET, DELETE
|
|
"""
|
|
|
|
authentication_classes = [SessionAuthentication, TokenAuthentication]
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
parser_classes = [MultiPartParser, FormParser]
|
|
|
|
serializer_class = SubscriptionChannelSerializerGET
|
|
queryset = SubscriptionChannel.objects.all().order_by("-creation_datetime")
|
|
|
|
def post(self, request):
|
|
serializer = self.get_serializer(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
try:
|
|
self.perform_create(serializer)
|
|
except IntegrityError:
|
|
return Response(
|
|
{"detail": "Channel must be unique"},
|
|
status=status.HTTP_409_CONFLICT,
|
|
exception=True
|
|
)
|
|
|
|
headers = self.get_success_headers(serializer.data)
|
|
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|
|
|
|
|
|
# =================================================================================================
|
|
# Tracked Content Views
|
|
|
|
class TrackedContent_ListView(generics.ListCreateAPIView):
|
|
"""
|
|
View to provide a list of TrackedContent model instances.
|
|
Can also be used to create a new instance.
|
|
|
|
Supports: GET, POST
|
|
"""
|
|
|
|
authentication_classes = [SessionAuthentication, TokenAuthentication]
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
pagination_class = DefaultPagination
|
|
serializer_class = TrackedContentSerializer
|
|
queryset = TrackedContent.objects.all().order_by("-creation_datetime")
|
|
|
|
filter_backends = [filters.SearchFilter, rest_filters.DjangoFilterBackend, filters.OrderingFilter]
|
|
filterset_fields = ["uuid", "subscription", "content_url", "creation_datetime"]
|
|
search_fields = ["name"]
|
|
ordering_fields = ["creation_datetime"]
|
|
|
|
def post(self, request):
|
|
serializer = self.get_serializer(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
try:
|
|
self.perform_create(serializer)
|
|
except IntegrityError:
|
|
return Response(
|
|
{"detail": "Tracked content must be unique"},
|
|
status=status.HTTP_409_CONFLICT,
|
|
exception=True
|
|
)
|
|
|
|
headers = self.get_success_headers(serializer.data)
|
|
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|
|
|
|
|
|
class TrackedContent_DetailView(generics.RetrieveDestroyAPIView):
|
|
"""
|
|
View to provide details on a particular TrackedContent model instances.
|
|
|
|
Supports: GET, DELETE
|
|
"""
|
|
|
|
authentication_classes = [SessionAuthentication, TokenAuthentication]
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
parser_classes = [MultiPartParser, FormParser]
|
|
|
|
serializer_class = TrackedContentSerializer
|
|
queryset = TrackedContent.objects.all().order_by("-creation_datetime")
|