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)
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)
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)
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,)
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)
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)
def retrieve(self, request, pk=None): try: qs = ProductModel.objects.get(pid=pk) ps = ProductSerializer(qs) return Response(ps.data) except ProductModel.DoesNotExist: msg = {'data': 'Product Not Found'} return Response(msg)
def product_detail_ws(request, slug): try: product = Product.objects.filter(slug=slug).first() except Product.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) serializer = ProductSerializer(product) return Response(serializer.data)
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 get_new_products(request): prods = Product.objects.all().order_by('-id') if 'num' in request.GET: num = int(request.GET['num']) prods = prods[:num] serializer = ProductSerializer(prods, many=True) return Response(serializer.data)
def get_product(request, id): res = {} try: prod = Product.objects.get(id=id) except Product.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) serializer = ProductSerializer(prod) res.update(serializer.data) return Response(res)
def get(self, request): products = Product.objects.all() serializer = ProductSerializer(products, many=True) filter_backends = ( DjangoFilterBackend, filters.SearchFilter, ) filter_class = ProductManager return Response(serializer.data)
class MyOrderItemSerializer(serializers.ModelSerializer): product = ProductSerializer() class Meta: model = OrderItem fields = ( "product", "quantity", )
def products_in_category_ws(request, name): products = Product.objects.filter(available=True) try: category = Category.objects.get(slug=name) except Category.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) products = products.filter(category=category) serializer = ProductSerializer(products, many=True) return Response(serializer.data)
def products_by_category(request, category_id): try: category = Category.objects.get(id=category_id) serializer = CategorySerializer(category, many=True) except Category.DoesNotExist as e: return Response({'error': str(e)}) if request.method == 'GET': products = category.product_set.all() serializer = ProductSerializer(products, many=True) return Response(serializer.data)
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 get_top_products(request): res = [] purchs = Purchase.objects.values('product').annotate( c=Count('product')).order_by('-c') if 'num' in request.GET: num = int(request.GET['num']) purchs = purchs[:num] for p in purchs: res.append(Product.objects.get(pk=p.get('product'))) serializer = ProductSerializer(res, many=True) return Response(serializer.data)
def cart_detail_ws(request): cart = Cart(request) list = [] # quantities = [] for key, value in cart.cart.items(): data = {} p = get_object_or_404(Product, id=key) serializers = ProductSerializer(p) q = value['quantity'] totalPrice = float(serializers.data['price']) * q data['product'] = serializers.data data['quantity'] = q data['totalPrice'] = round(totalPrice,2) list.append(data) return Response(list)
def comments_by_product(request, product_id, category_id): try: categories = Category.objects.get(id=category_id) try: products = Product.objects.get(id=product_id) serializer = ProductSerializer(products, many=True) except Category.DoesNotExist as e: return Response("error", str(e)) except Product.DoesNotExist as e: return Response("error:", str(e)) if request.method == 'GET': comments = products.comment_set.all() serializer = CommentSerializer(comments, many=True) return Response(serializer.data)
def get_products(request): products = ProductFilter(request.GET, queryset=Product.objects.all()).qs order = request.GET.get('order') if order: if order == 'cost': products = products.order_by('price') elif order == '-cost': products = products.order_by('-price') elif order == 'rate': products = sorted(products, key=lambda p: p.stars) elif order == '-rate': products = sorted(products, key=lambda p: p.stars, reverse=True) else: products = products.order_by('price') serializer = ProductSerializer(products, many=True) return Response(serializer.data)
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)
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})
def list( self, request): #List method will bahave like get() Will read all values qs = ProductModel.objects.all() ps = ProductSerializer(qs, many=True) return Response(ps.data)
def products(request): if request.method == 'GET': get = Product.objects.all() serializer = ProductSerializer(get, many=True) return JsonResponse(serializer.data, safe=False)
def product_list_ws(request): products = Product.objects.filter(available=True) serializer = ProductSerializer(products, many=True) return Response(serializer.data)
def category_product(request, id): if request.method == 'GET': get = Product.objects.filter(category_id=id) serializer = ProductSerializer(get, many=True) return JsonResponse(serializer.data, safe=False)
def get(self, request, product_id): product = self.get_object(product_id) serializer = ProductSerializer(product) return Response(serializer.data)