list, create and delete views

This commit is contained in:
Corban-Lee Jones 2024-01-29 20:44:08 +00:00
parent 688520eaac
commit 7e0d9749c4
3 changed files with 23 additions and 12 deletions

View File

@ -2,8 +2,6 @@
import logging import logging
from django.apps import apps
from django.conf import settings
from rest_framework import serializers from rest_framework import serializers
from apps.home.models import RSSFeed, FeedChannel from apps.home.models import RSSFeed, FeedChannel
@ -11,7 +9,7 @@ from apps.home.models import RSSFeed, FeedChannel
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class DynamicModelSerializer(serializers.ModelSerializer): class DynamicModelSerializer(serializers.HyperlinkedModelSerializer):
"""For use with GET requests, to specify which fields to include or exclude """For use with GET requests, to specify which fields to include or exclude
Mimics some graphql functionality. Mimics some graphql functionality.
@ -107,6 +105,7 @@ class DynamicModelSerializer(serializers.ModelSerializer):
class RssFeedSerializer(DynamicModelSerializer): class RssFeedSerializer(DynamicModelSerializer):
image = serializers.ImageField()
class Meta: class Meta:
model = RSSFeed model = RSSFeed

View File

@ -10,6 +10,9 @@ urlpatterns = [
path("api-auth/", include("rest_framework.urls", namespace="rest_framework")), path("api-auth/", include("rest_framework.urls", namespace="rest_framework")),
path("api-token-auth/", obtain_auth_token), path("api-token-auth/", obtain_auth_token),
path("rssfeed/", views.RSSFeedListApiView.as_view(), name="rssfeed"), path("rssfeed/", include([
path("", views.RSSFeedList.as_view(), name="rssfeed"),
path("<str:pk>/", views.RSSFeedDetail.as_view(), name="rssfeed-detail")
])),
path("feedchannel/", views.FeedChannelListApiView.as_view(), name="feedchannel") path("feedchannel/", views.FeedChannelListApiView.as_view(), name="feedchannel")
] ]

View File

@ -1,6 +1,7 @@
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
import logging import logging
import base64
import httpx import httpx
from django.http import HttpResponse from django.http import HttpResponse
@ -15,6 +16,7 @@ from rest_framework.response import Response
from rest_framework import status, permissions, filters, generics from rest_framework import status, permissions, filters, generics
from rest_framework.pagination import PageNumberPagination from rest_framework.pagination import PageNumberPagination
from rest_framework.authentication import SessionAuthentication, TokenAuthentication from rest_framework.authentication import SessionAuthentication, TokenAuthentication
from rest_framework.parsers import MultiPartParser, FormParser
from asgiref.sync import async_to_sync from asgiref.sync import async_to_sync
from apps.home.models import RSSFeed, FeedChannel from apps.home.models import RSSFeed, FeedChannel
@ -23,12 +25,11 @@ from .serializers import RssFeedSerializer, FeedChannelSerializer
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class RSSFeedListApiView(generics.ListAPIView): class RSSFeedList(generics.ListAPIView, generics.CreateAPIView):
authentication_classes = [SessionAuthentication, TokenAuthentication] authentication_classes = [SessionAuthentication, TokenAuthentication]
permission_classes = [permissions.IsAuthenticated] permission_classes = [permissions.IsAuthenticated]
serializer_class = RssFeedSerializer serializer_class = RssFeedSerializer
queryset = RSSFeed.objects.all().order_by("-created_at") queryset = RSSFeed.objects.all().order_by("-created_at")
filter_backends = [filters.SearchFilter, rest_filters.DjangoFilterBackend, filters.OrderingFilter] filter_backends = [filters.SearchFilter, rest_filters.DjangoFilterBackend, filters.OrderingFilter]
@ -36,14 +37,22 @@ class RSSFeedListApiView(generics.ListAPIView):
search_fields = ["name"] search_fields = ["name"]
ordering_fields = ["created_at"] ordering_fields = ["created_at"]
def post(self, request):
serializer = self.serializer_class(data=request.data) class RSSFeedDetail(generics.RetrieveUpdateDestroyAPIView):
if serializer.is_valid(): authentication_classes = [SessionAuthentication, TokenAuthentication]
serializer.save() permission_classes = [permissions.IsAuthenticated]
return Response(serializer.data, status.HTTP_201_CREATED) parser_classes = [MultiPartParser, FormParser]
return Response(serializer.errors, status.HTTP_400_BAD_REQUEST) 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): class FeedChannelListApiView(generics.ListAPIView):