From a5cfb428182e944a23aee7faabf5921c89458508 Mon Sep 17 00:00:00 2001 From: Pratyush Desai Date: Sat, 8 Jan 2022 07:18:32 +0530 Subject: [PATCH] set up viewsets with routers, note broken highlighting --- website/db.sqlite3 | Bin 147456 -> 147456 bytes website/snippets/urls.py | 30 +++++++-------------- website/snippets/views.py | 54 ++++++++++++++++---------------------- 3 files changed, 32 insertions(+), 52 deletions(-) diff --git a/website/db.sqlite3 b/website/db.sqlite3 index 6ebc817df826cd8f52b4248ea26acec31c3500bf..3bbbde7655f88949c5ea7965e5dd0002ecc93187 100644 GIT binary patch delta 375 zcmZo@;B08%oFL7pJ5k1&QFmj)5_utZ{sad8m;6`wck`F=ck|EUPuSS##V;np%dE>- zoR?WpkXlk44`nbnm+5aW(`W4az{SkZ&cMHhKboI?W8+W0`XFXb21i+Q4oOu`BLgEN zT?0d10}BNMLn~uTD^o)~BLfQ~3)76$oSb~Ug35xT%)Al}bud?MNwU|pt2+* zKQFzgv@9vVLN_I`DBGV2Xr?%JGmS0u42;bT%?!bY6x0)EVh|(Pm7JKaw6HR?&@(Z! sG&VIL(Un{b3=GVi{GS;3|L}k3|Fl`K;0Zq;3o|DpGl*bfX6EDs0Le>h1poj5 delta 56 zcmZo@;B08%oFL7pIZ?)$QFCL$5_usu{xSysm;6`wck`F=ck|EUuh`g_z~5Y^zr9SK MvG2oXmI;640rKJ#Z2$lO diff --git a/website/snippets/urls.py b/website/snippets/urls.py index dfc25de..ea39236 100644 --- a/website/snippets/urls.py +++ b/website/snippets/urls.py @@ -1,24 +1,12 @@ -from django.urls import path -from rest_framework.urlpatterns import format_suffix_patterns +from django.urls import path, include +from rest_framework.routers import DefaultRouter from . import views + +router = DefaultRouter() +router.register(r'snippets', views.SnippetViewSet) +router.register(r'users', views.UserViewSet) + urlpatterns = [ - path('', views.api_root), - path('snippets/', - views.SnippetList.as_view(), - name='snippet-list'), - path('snippets//', - views.SnippetDetail.as_view(), - name='snippet-detail'), - path('snippets//highlight/', - views.SnippetHighlight.as_view(), - name='snippet-highlight'), - path('users/', - views.UserList.as_view(), - name='user-list'), - path('users//', views.UserDetail.as_view(), - name='user-detail'), - -] - -urlpatterns = format_suffix_patterns(urlpatterns) \ No newline at end of file + path('', include(router.urls)), +] \ No newline at end of file diff --git a/website/snippets/views.py b/website/snippets/views.py index cb5e715..64ae859 100644 --- a/website/snippets/views.py +++ b/website/snippets/views.py @@ -3,44 +3,36 @@ from django.contrib.auth.models import User from .models import Snippet from .serializers import SnippetSerializer, UserSerializer from .permissions import IsOwnerOrReadOnly -from rest_framework import generics, permissions, renderers -from rest_framework.decorators import api_view +from rest_framework import permissions, renderers from rest_framework.response import Response 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']) -def api_root(request, format=None): - return Response({ - 'users': reverse('user-list', request=request, format=format), - 'snippets': reverse('snippet-list', request=request, format=format) - }) -class SnippetList(generics.ListCreateAPIView): +class SnippetViewSet(viewsets.ModelViewSet): + """ + This viewset automatically provides `list`, `create`, `retrieve`, + `update` and `destroy` actions. + + Additionally we also provide an extra `highlight` action. + """ queryset = Snippet.objects.all() serializer_class = SnippetSerializer - permission_classes = [permissions.IsAuthenticatedOrReadOnly] + permission_classes = [permissions.IsAuthenticatedOrReadOnly, + IsOwnerOrReadOnly] + + @action(detail=True, renderer_classes=[renderers.StaticHTMLRenderer]) + def highlight(self, request, *args, **kwargs): + snippet = self.get_object() + return Response(snippet.highlighted) def perform_create(self, serializer): serializer.save(owner=self.request.user) - - -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() - return Response(snippet.highlighted) - -class UserList(generics.ListAPIView): - queryset = User.objects.all() - serializer_class = UserSerializer - - -class UserDetail(generics.RetrieveAPIView): +class UserViewSet(viewsets.ReadOnlyModelViewSet): + """ + This viewset automatically provides `list` and `retrieve` actions. + """ queryset = User.objects.all() serializer_class = UserSerializer \ No newline at end of file