Example #1
0
def stweet_detail(request, stweet_id):
    """
    Retrieve, update or delete a code snippet.
    """
    try:
        stweet = STweet.objects.get(id=stweet_id)
    except STweet.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = STweetSerializer(stweet)
        return JSONResponse(serializer.data)

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

    elif request.method == 'DELETE':
        stweet.delete()
        return HttpResponse(status=204)
Example #2
0
def stweet_list(request):
    """
    List all code snippets, or create a new snippet.
    """
    if request.method == 'GET':
        stweets = STweet.objects.all()
        serializer = STweetSerializer(stweets, many=True)
        return JSONResponse(serializer.data)

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