set up viewsets with routers, note broken highlighting

This commit is contained in:
Pratyush Desai 2022-01-08 07:18:32 +05:30
parent 91430d30a0
commit a5cfb42818
Signed by: pratyush
GPG Key ID: DBA5BB7505946FAD
3 changed files with 32 additions and 52 deletions

Binary file not shown.

View File

@ -1,24 +1,12 @@
from django.urls import path from django.urls import path, include
from rest_framework.urlpatterns import format_suffix_patterns from rest_framework.routers import DefaultRouter
from . import views from . import views
router = DefaultRouter()
router.register(r'snippets', views.SnippetViewSet)
router.register(r'users', views.UserViewSet)
urlpatterns = [ urlpatterns = [
path('', views.api_root), path('', include(router.urls)),
path('snippets/',
views.SnippetList.as_view(),
name='snippet-list'),
path('snippets/<int:pk>/',
views.SnippetDetail.as_view(),
name='snippet-detail'),
path('snippets/<int:pk>/highlight/',
views.SnippetHighlight.as_view(),
name='snippet-highlight'),
path('users/',
views.UserList.as_view(),
name='user-list'),
path('users/<int:pk>/', views.UserDetail.as_view(),
name='user-detail'),
] ]
urlpatterns = format_suffix_patterns(urlpatterns)

View File

@ -3,44 +3,36 @@ from django.contrib.auth.models import User
from .models import Snippet from .models import Snippet
from .serializers import SnippetSerializer, UserSerializer from .serializers import SnippetSerializer, UserSerializer
from .permissions import IsOwnerOrReadOnly from .permissions import IsOwnerOrReadOnly
from rest_framework import generics, permissions, renderers from rest_framework import permissions, renderers
from rest_framework.decorators import api_view
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.reverse import reverse from rest_framework.reverse import reverse
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework import permissions
@api_view(['GET']) class SnippetViewSet(viewsets.ModelViewSet):
def api_root(request, format=None): """
return Response({ This viewset automatically provides `list`, `create`, `retrieve`,
'users': reverse('user-list', request=request, format=format), `update` and `destroy` actions.
'snippets': reverse('snippet-list', request=request, format=format)
}) Additionally we also provide an extra `highlight` action.
class SnippetList(generics.ListCreateAPIView): """
queryset = Snippet.objects.all() queryset = Snippet.objects.all()
serializer_class = SnippetSerializer serializer_class = SnippetSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly] permission_classes = [permissions.IsAuthenticatedOrReadOnly,
IsOwnerOrReadOnly]
def perform_create(self, serializer): @action(detail=True, renderer_classes=[renderers.StaticHTMLRenderer])
serializer.save(owner=self.request.user) def highlight(self, request, *args, **kwargs):
class SnippetDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly]
class SnippetHighlight(generics.GenericAPIView):
queryset = Snippet.objects.all()
renderer_classes = [renderers.StaticHTMLRenderer]
def get(self, request, *args, **kwargs):
snippet = self.get_object() snippet = self.get_object()
return Response(snippet.highlighted) return Response(snippet.highlighted)
class UserList(generics.ListAPIView): def perform_create(self, serializer):
queryset = User.objects.all() serializer.save(owner=self.request.user)
serializer_class = UserSerializer class UserViewSet(viewsets.ReadOnlyModelViewSet):
"""
This viewset automatically provides `list` and `retrieve` actions.
class UserDetail(generics.RetrieveAPIView): """
queryset = User.objects.all() queryset = User.objects.all()
serializer_class = UserSerializer serializer_class = UserSerializer