from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer

# Create your views here.

@csrf_exempt
def snippet_list(request):
    """
    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)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = SnippetSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)


@csrf_exempt
def snippet_detail(request, pk):
    """
    Retrieve, update or delete a code snippet.
    """
    try:
        snippet = Snippet.objects.get(pk=pk)
    except Snippet.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = SnippetSerializer(snippet)
        return JsonResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = SnippetSerializer(snippet, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        snippet.delete()
        return HttpResponse(status=204)


@@ -2,142 +2,36 @@
 rom 
-django.shortcuts import render%0Afrom django.http import HttpResponse, JsonResponse%0Afrom django.views.decorators.csrf import csrf_exempt
+rest_framework import status
 %0Afro
@@ -51,15 +51,16 @@
 ork.
-rendere
+decorato
 rs i
@@ -69,20 +69,16 @@
 ort 
-JSONRenderer
+api_view
 %0Afro
@@ -98,23 +98,24 @@
 ork.
-parsers
+response
  import 
 JSON
@@ -110,26 +110,24 @@
  import 
-JSONPar
+Respon
 se
-r
 %0Afrom sn
@@ -211,47 +211,35 @@
 er%0A%0A
-# Create your views here.%0A%0A@csrf_exempt
+%0A@api_view(%5B'GET', 'POST'%5D)
 %0Adef
@@ -283,21 +283,16 @@
 ist all 
-code 
 snippets
@@ -466,36 +466,32 @@
 %0A        return 
-Json
 Response(seriali
@@ -502,20 +502,8 @@
 data
-, safe=False
 )%0A%0A 
@@ -540,51 +540,8 @@
 T':%0A
-        data = JSONParser().parse(request)%0A
     
@@ -576,24 +576,32 @@
 alizer(data=
+request.
 data)%0A      
@@ -669,36 +669,32 @@
          return 
-Json
 Response(seriali
@@ -714,11 +714,31 @@
 tus=
-201
+status.HTTP_201_CREATED
 )%0A  
@@ -742,36 +742,32 @@
 %0A        return 
-Json
 Response(seriali
@@ -789,27 +789,73 @@
 tus=
-400)%0A%0A%0A@csrf_exempt
+status.HTTP_400_BAD_REQUEST)%0A%0A@api_view(%5B'GET', 'PUT', 'DELETE'%5D)
 %0Adef
@@ -929,20 +929,24 @@
 e a 
-code 
 snippet
+ instance
 .%0A  
@@ -1045,36 +1045,32 @@
 %0A        return 
-Http
 Response(status=
@@ -1073,11 +1073,33 @@
 tus=
-404
+status.HTTP_404_NOT_FOUND
 )%0A%0A 
@@ -1184,36 +1184,32 @@
 %0A        return 
-Json
 Response(seriali
@@ -1257,51 +1257,8 @@
 T':%0A
-        data = JSONParser().parse(request)%0A
     
@@ -1306,16 +1306,24 @@
 t, data=
+request.
 data)%0A  
@@ -1395,36 +1395,32 @@
          return 
-Json
 Response(seriali
@@ -1448,12 +1448,8 @@
 urn 
-Json
 Resp
@@ -1479,19 +1479,43 @@
  status=
-400
+status.HTTP_400_BAD_REQUEST
 )%0A%0A    e
@@ -1590,12 +1590,8 @@
 urn 
-Http
 Resp
@@ -1606,10 +1606,33 @@
 tus=
-204
+status.HTTP_204_NO_CONTENT
 )%0A%0A

use more of rest_framework