Ejemplo n.º 1
0
def item_list(request):
    if request.method == 'GET':
        items = Item.objects.all()
        serializer = ItemSerializer(items, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = ItemSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 2
0
def item_details(request, pk):
    try:
        item = Item.objects.get(pk=pk)
    except Item.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = ItemSerializer(item)
        return Response(serializer.data)

    elif request.method == 'PUT':
        serializer = ItemSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        item.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
Ejemplo n.º 3
0
    def post(self, request):
        try:
            inventory = json.loads(request.body)
            serializer = ItemSerializer(data=inventory)

            if serializer.is_valid():
                serializer.save()
                response_dict = {
                    'message': serializer.data,
                    'status_code': 200
                }
            else:
                response_dict = {
                    'message': serializer.errors,
                    'status_code': 401
                }
        except:
            print(traceback.format_exc())
            response_dict = {
                'message': 'Internal server error',
                'status_code': 500
            }
        return Response(response_dict, status=response_dict['status_code'])