Example #1
0
def IMG_list(request):
    """
    List all code snippets, or create a new snippet.
    """
    if request.method == 'GET':
        IMGs = Imaged.objects.latest('Created')
        serializer = IMGSerializer(IMGs, many=True)
        return JSONResponse(serializer.data)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = IMGSerializer(data=data["items"][0]["form"])
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data, status=201)
        return JSONResponse(serializer.errors, status=400)
Example #2
0
def IMG_detail(request, pk):
    """
    Retrieve, update or delete a code snippet.
    """
    try:
        img = Imaged.objects.get(pk=pk)
    except Imaged.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = IMGSerializer(img)
        return JSONResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = IMGSerializer(img, data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data)
        return JSONResponse(serializer.errors, status=400)

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