From 01035a2b45dcdfd081d6eff35fab247103976820 Mon Sep 17 00:00:00 2001 From: Pratyush Desai Date: Sat, 8 Jan 2022 04:54:32 +0530 Subject: [PATCH] Reqest Response --- .gitignore | 3 ++- website/db.sqlite3 | Bin 143360 -> 143360 bytes website/snippets/urls.py | 5 ++++- website/snippets/views.py | 36 +++++++++++++++++------------------- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index c60f1c7..5953f98 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ venv .vscode -__pycache__ \ No newline at end of file +__pycache__ +db.sqlite3 \ No newline at end of file diff --git a/website/db.sqlite3 b/website/db.sqlite3 index 3521cf061c56f6747b228e97c1f6cd27601fc7e0..ee2300a0975ea53363b033fa74e632c2a00936ea 100644 GIT binary patch delta 208 zcmZp8z|ru4V}dlJ+C&*=MzxIzOXP*v`4brUU-Dn!-_2jf-_1XZKVf5|7r&SYFS9OV zab9LYL25~HJe0xIT&BOhOrNpu0|yg-2Lt~R{*H}}HT)G;OdJf3yowx>vPK3*M!E)u zx(4P7M#fe~hE@iKdM2humS*M!MVWae8YZS@ngx|58Tol>MVYC2DLIu^j5zcfSeY2= R838pISVHs~8X2SM1pvzuJ2n6S delta 56 zcmV-80LTA;;0S==2#^~AB#|6L0VJ_tq#qIn4|V_#=?~NoyAO5`iw~L)c(H+C4}*3u Ow{|Z9j_|VtVEhjlOB3M$ diff --git a/website/snippets/urls.py b/website/snippets/urls.py index 45d7cf4..2d39cba 100644 --- a/website/snippets/urls.py +++ b/website/snippets/urls.py @@ -1,7 +1,10 @@ from django.urls import path +from rest_framework.urlpatterns import format_suffix_patterns from . import views urlpatterns = [ path('snippets/', views.snippet_list), path('snippets//', views.snippet_detail), -] \ No newline at end of file +] + +urlpatterns = format_suffix_patterns(urlpatterns) \ No newline at end of file diff --git a/website/snippets/views.py b/website/snippets/views.py index 5b9c8dc..51346d9 100644 --- a/website/snippets/views.py +++ b/website/snippets/views.py @@ -1,49 +1,47 @@ -from django.http import HttpResponse, JsonResponse -from django.views.decorators.csrf import csrf_exempt -from rest_framework.parsers import JSONParser +from rest_framework import status +from rest_framework.decorators import api_view +from rest_framework.response import Response from .models import Snippet from .serializers import SnippetSerializer -@csrf_exempt -def snippet_list(request): +@api_view(['GET', 'POST']) +def snippet_list(request, format=None): """ List all code snippets, or create a new snippet. """ if request.method == 'GET': snippets = Snippet.objects.all() serializer = SnippetSerializer(snippets, many=True) - return JsonResponse(serializer.data, safe=False) + return Response(serializer.data) elif request.method == 'POST': - data = JSONParser().parse(request) - serializer = SnippetSerializer(data=data) + serializer = SnippetSerializer(data=request.data) if serializer.is_valid(): serializer.save() - return JsonResponse(serializer.data, status=201) - return JsonResponse(serializer.errors, status=400) + return Response(serializer.data, status=status.HTTP_201_CREATED) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) -@csrf_exempt -def snippet_detail(request, pk): +@api_view(['GET', 'PUT', 'DELETE']) +def snippet_detail(request, pk, format=None): """ Retrieve, update or delete a code snippet. """ try: snippet = Snippet.objects.get(pk=pk) except Snippet.DoesNotExist: - return HttpResponse(status=404) + return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = SnippetSerializer(snippet) - return JsonResponse(serializer.data) + return Response(serializer.data) elif request.method == 'PUT': - data = JSONParser().parse(request) - serializer = SnippetSerializer(snippet, data=data) + serializer = SnippetSerializer(snippet, data=request.data) if serializer.is_valid(): serializer.save() - return JsonResponse(serializer.data) - return JsonResponse(serializer.errors, status=400) + return Response(serializer.data) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': snippet.delete() - return HttpResponse(status=204) \ No newline at end of file + return Response(status=status.HTTP_204_NO_CONTENT) \ No newline at end of file