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 3521cf0..ee2300a 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 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