91 lines
3.4 KiB
Python
91 lines
3.4 KiB
Python
# -*- encoding: utf-8 -*-
|
|
|
|
import logging
|
|
import base64
|
|
|
|
import httpx
|
|
from django.http import HttpResponse
|
|
from django_filters import rest_framework as rest_filters
|
|
from django.views.decorators.cache import cache_page
|
|
from django.utils.decorators import method_decorator
|
|
from django.core.files import File
|
|
from django.core.files.base import ContentFile
|
|
from django.core.files.temp import NamedTemporaryFile
|
|
from django.db.utils import IntegrityError
|
|
from rest_framework.views import APIView
|
|
from rest_framework.response import Response
|
|
from rest_framework import status, permissions, filters, generics
|
|
from rest_framework.pagination import PageNumberPagination
|
|
from rest_framework.authentication import SessionAuthentication, TokenAuthentication
|
|
from rest_framework.parsers import MultiPartParser, FormParser
|
|
from asgiref.sync import async_to_sync
|
|
|
|
from apps.home.models import RSSFeed, FeedChannel
|
|
from .serializers import RssFeedSerializer, FeedChannelSerializer
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
class RSSFeedPagination(PageNumberPagination):
|
|
page_size = 10
|
|
page_size_query_param = "page_size"
|
|
max_page_size = 25
|
|
|
|
|
|
class RSSFeedList(generics.ListAPIView, generics.CreateAPIView):
|
|
authentication_classes = [SessionAuthentication, TokenAuthentication]
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
pagination_class = RSSFeedPagination
|
|
serializer_class = RssFeedSerializer
|
|
queryset = RSSFeed.objects.all().order_by("created_at")
|
|
|
|
filter_backends = [filters.SearchFilter, rest_filters.DjangoFilterBackend, filters.OrderingFilter]
|
|
filterset_fields = ["uuid", "name", "url", "discord_server_id", "created_at"]
|
|
search_fields = ["name"]
|
|
ordering_fields = ["created_at"]
|
|
|
|
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": "RSS Feed 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 RSSFeedDetail(generics.RetrieveUpdateDestroyAPIView):
|
|
authentication_classes = [SessionAuthentication, TokenAuthentication]
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
parser_classes = [MultiPartParser, FormParser]
|
|
|
|
serializer_class = RssFeedSerializer
|
|
queryset = RSSFeed.objects.all().order_by("-created_at")
|
|
|
|
|
|
class FeedChannelListApiView(generics.ListAPIView):
|
|
authentication_classes = [SessionAuthentication, TokenAuthentication]
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
serializer_class = FeedChannelSerializer
|
|
|
|
queryset = FeedChannel.objects.all().order_by("-created_at")
|
|
|
|
filter_backends = [filters.SearchFilter, rest_filters.DjangoFilterBackend, filters.OrderingFilter]
|
|
filterset_fields = ["uuid", "discord_channel_id", "discord_server_id", "feeds", "created_at"]
|
|
search_fields = ["feeds__name"]
|
|
ordering_fields = ["created_at"]
|
|
|
|
def post(self, request):
|
|
|
|
serializer = self.serializer_class(data=request.data)
|
|
if serializer.is_valid():
|
|
serializer.save()
|
|
return Response(serializer.data, status.HTTP_201_CREATED)
|
|
|
|
return Response(serializer.errors, status.HTTP_400_BAD_REQUEST)
|