diff --git a/website/db.sqlite3 b/website/db.sqlite3 index 6ebc817..3bbbde7 100644 Binary files a/website/db.sqlite3 and b/website/db.sqlite3 differ 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