Example #1
0
def retailer_list(request):
    if request.method == 'GET':
        retailer = Retailer.objects.all()
        retailer_serializer = RetailerSerializer(retailer, many=True)
        return Response(retailer_serializer.data)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        retailer_serializer = RetailerSerializer(data=data)
        if retailer_serializer.is_valid():
            retailer_serializer.save()
            return Response(retailer_serializer.data, status=status.HTTP_201_CREATED)
        return Response(retailer_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #2
0
def retailer_details(request, id):

    try:
        retailer = Retailer.objects.get(pk=id)
    except retailer.DoesNotExist:
        return HttpResponse(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        retailer_serializer = RetailerSerializer(retailer)
        return Response(retailer_serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        retailer_serializer = RetailerSerializer(retailer, data=data)
        if retailer_serializer.is_valid():
            retailer_serializer.save()
            return Response(retailer_serializer.data)
        return Response(retailer_serializer.errors, status=status.HTTP_404_NOT_FOUND)

    elif request.method == 'DELETE':
        retailer.delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT)