highlight view

This commit is contained in:
Pratyush Desai 2022-01-08 06:49:47 +05:30
parent eb24f56b7c
commit d2909e5ce9
Signed by: pratyush
GPG Key ID: DBA5BB7505946FAD
2 changed files with 20 additions and 1 deletions

View File

@ -3,10 +3,12 @@ from rest_framework.urlpatterns import format_suffix_patterns
from . import views from . import views
urlpatterns = [ urlpatterns = [
path('', views.api_root),
path('snippets/', views.SnippetList.as_view()), path('snippets/', views.SnippetList.as_view()),
path('snippets/<int:pk>/', views.SnippetDetail.as_view()), path('snippets/<int:pk>/', views.SnippetDetail.as_view()),
path('users/', views.UserList.as_view()), path('users/', views.UserList.as_view()),
path('users/<int:pk>/', views.UserDetail.as_view()), path('users/<int:pk>/', views.UserDetail.as_view()),
path('snippets/<int:pk>/highlight/', views.SnippetHighlight.as_view()),
] ]
urlpatterns = format_suffix_patterns(urlpatterns) urlpatterns = format_suffix_patterns(urlpatterns)

View File

@ -3,8 +3,17 @@ 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 from rest_framework import generics, permissions, renderers
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.reverse import reverse
@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 SnippetList(generics.ListCreateAPIView):
queryset = Snippet.objects.all() queryset = Snippet.objects.all()
serializer_class = SnippetSerializer serializer_class = SnippetSerializer
@ -19,6 +28,14 @@ class SnippetDetail(generics.RetrieveUpdateDestroyAPIView):
serializer_class = SnippetSerializer serializer_class = SnippetSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly] 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): class UserList(generics.ListAPIView):
queryset = User.objects.all() queryset = User.objects.all()
serializer_class = UserSerializer serializer_class = UserSerializer