From d2909e5ce931f1362a61dfbcf11ce96e0b968cfa Mon Sep 17 00:00:00 2001 From: Pratyush Desai Date: Sat, 8 Jan 2022 06:49:47 +0530 Subject: [PATCH] highlight view --- website/snippets/urls.py | 2 ++ website/snippets/views.py | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/website/snippets/urls.py b/website/snippets/urls.py index ceca15b..52cd7b8 100644 --- a/website/snippets/urls.py +++ b/website/snippets/urls.py @@ -3,10 +3,12 @@ from rest_framework.urlpatterns import format_suffix_patterns from . import views urlpatterns = [ + path('', views.api_root), path('snippets/', views.SnippetList.as_view()), path('snippets//', views.SnippetDetail.as_view()), path('users/', views.UserList.as_view()), path('users//', views.UserDetail.as_view()), + path('snippets//highlight/', views.SnippetHighlight.as_view()), ] urlpatterns = format_suffix_patterns(urlpatterns) \ No newline at end of file diff --git a/website/snippets/views.py b/website/snippets/views.py index a04cc15..cb5e715 100644 --- a/website/snippets/views.py +++ b/website/snippets/views.py @@ -3,8 +3,17 @@ 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 +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): queryset = Snippet.objects.all() serializer_class = SnippetSerializer @@ -19,6 +28,14 @@ class SnippetDetail(generics.RetrieveUpdateDestroyAPIView): 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