Esempio n. 1
0
 def post(self, request):
     serializer = ProductSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response({"error": serializer.errors},
                     status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Esempio n. 2
0
def product_detail(request, pk):
    """
    Retrieve, update or delete a product instance.
    """

    try:
        product = Product.objects.get(pk=pk)
    except Product.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = ProductSerializer(product)
        return Response(serializer.data)

    elif request.method == 'PUT':
        if product.owner != request.user:
            return Response(status=status.HTTP_403_FORBIDDEN)
        else:
            serializer = ProductSerializer(product, 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':
        if product.owner != request.user:
            return Response(status=status.HTTP_403_FORBIDDEN)
        else:
            product.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
 def create(self, request):  #will bahave as post
     res = request.data
     ps = ProductSerializer(data=res)
     if ps.is_valid():
         ps.save()
         msg = {'data': 'Data is saved'}
     else:
         msg = {'error': ps.errors}
     return Response(msg)
Esempio n. 4
0
	def patch(self, request, product_id):
		product = self.get_object(product_id)
		if product.is_added == False:
			product.is_added = True
		else:
			product.is_added = False
		serializer = ProductSerializer(instance = product, data = request.data, partial=True)
		if serializer.is_valid():
			serializer.save()
			return Response(serializer.data)
		return Response({'error': serializer.errors})
		permission_classes = (IsAuthenticated,)
Esempio n. 5
0
def create_product(request):
    user = check_request_user(request)
    if user == "Admin":
        serializer = ProductSerializer(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)

    return Response(
        {'error_message': "You're not allowed to do this Request!"},
        status=status.HTTP_403_FORBIDDEN)
Esempio n. 6
0
def product_list(request):
    if request.method == "GET":
        products = Product.objects.all()
        serializer = ProductSerializer(products, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)
    elif request.method == 'POST':
        serializer = ProductSerializer(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_500_INTERNAL_SERVER_ERROR)
 def update(self, request, pk=None):
     qs = ProductModel.objects.get(pid=pk)
     try:
         ps = ProductSerializer(qs, request.data, partial=True)
         if ps.is_valid():
             ps.save()
             msg = {'data': 'Product is Saved'}
         else:
             msg = {'error': 'Error while Saving Product'}
         return Response(msg)
     except ProductModel.DoesNotExist:
         msg = {'error': 'Product ID: Not Found'}
         return Response(msg)
Esempio n. 8
0
def update_product(request, id):
    user = check_request_user(request)
    if user == "Admin":
        try:
            prod = Product.objects.get(id=id)
        except Product.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        serializer = ProductSerializer(prod, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    return Response(
        {'error_message': "You're not allowed to do this Request!"},
        status=status.HTTP_403_FORBIDDEN)
Esempio n. 9
0
def product_detail(request, id):
    try:
        product = Product.objects.get(id=id)
    except Product.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = ProductSerializer(product)
        return Response(serializer.data)
    elif request.method == 'PUT':
        serializer = ProductSerializer(instance=product, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors)
    elif request.method == 'DELETE':
        product.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
Esempio n. 10
0
def product(request, id):
    try:
        p = Product.objects.get(id=id)
    except Exception as e:
        return JsonResponse({"!ERROR": str(e)}, safe=False)

    if request.method == 'GET':
        serializer = ProductSerializer(p)
        return JsonResponse(serializer.data)

    elif request.method == 'PUT':
        serializer = ProductSerializer(instance=p, data=request.body)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse({'error': serializer.errors})

    elif request.method == 'DELETE':
        p.delete()
        return JsonResponse({'deleted': True})