79 lines
2.9 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 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 RSSFeedList(generics.ListAPIView, generics.CreateAPIView):
authentication_classes = [SessionAuthentication, TokenAuthentication]
permission_classes = [permissions.IsAuthenticated]
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", "created_at"]
search_fields = ["name"]
ordering_fields = ["created_at"]
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")
# def delete(self, request, uuid: str):
# try:
# rss_feed = RSSFeed.objects.get(uuid=uuid)
# rss_feed.delete()
# return Response(status=status.HTTP_204_NO_CONTENT)
# except RSSFeed.DoesNotExist:
# return Response({"detail": "RSSFeed Not Found"}, status.HTTP_404_NOT_FOUND)
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", "channel_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)