set up viewsets with routers, note broken highlighting
This commit is contained in:
parent
91430d30a0
commit
a5cfb42818
Binary file not shown.
@ -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)
|
|
@ -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]
|
||||||
|
|
||||||
|
@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):
|
def perform_create(self, serializer):
|
||||||
serializer.save(owner=self.request.user)
|
serializer.save(owner=self.request.user)
|
||||||
|
class UserViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
|
"""
|
||||||
class SnippetDetail(generics.RetrieveUpdateDestroyAPIView):
|
This viewset automatically provides `list` and `retrieve` actions.
|
||||||
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):
|
|
||||||
queryset = User.objects.all()
|
queryset = User.objects.all()
|
||||||
serializer_class = UserSerializer
|
serializer_class = UserSerializer
|
Reference in New Issue
Block a user