def add_to_cart(request, pk): current_user = request.user product = Products.objects.get(pk=pk) # add product to cart c = Cart(user=current_user, product_name=product, quantity=1) # save to disk c.save() # get number of items in cart for current user cart_dict = Cart.objects.filter(user=current_user).aggregate( Sum('quantity')) cart_list = [int(value) for value in cart_dict.values()] products = Products.objects.all() return render(request, "app/products.html", { "cart": cart_list[0], "products": products })
def register(request): if request.method == 'POST': body_unicode = request.body.decode('utf-8') body = json.loads(body_unicode) if body['password'] != body['password1']: return JsonResponse({'status': False, 'message': 'error passwords'}) try: User.objects.get(username=body['username']) return JsonResponse({'status': False}) except User.DoesNotExist: user = User(username=body['username'], password=body['password'], email=body['email']) user.save() cart = Cart(user=user) cart.save() return JsonResponse({'status': True})
def post(self, request): request_data = get_request_data(request, 'post') cart_id = request_data["cart"] if request_data.get("cart") else None buyer = Buyer.get_active_objects(id=request.user.id) if buyer.exists(): cart = Cart.get_active_objects(buyer_id=buyer[0].id) if str(cart_id) == str(cart[0].id): serializer = self.write_serializer_class(data=request_data) if serializer.is_valid(): order = serializer.save() data = self.detail_serializer_class(order).data return response(data, status.HTTP_201_CREATED) return inv_serializer_error_response(serializer, status.HTTP_400_BAD_REQUEST) return error_response(CONST_ERROR_MESSAGE_PERMISSION_DENIED_KEY, status.HTTP_400_BAD_REQUEST) return error_response(CONST_ERROR_MESSAGE_INVALID_USER_KEY, status.HTTP_400_BAD_REQUEST)
def cart_add(): if request.method == 'POST': username = request.json.get('username') product = request.json.get('product') register = Cart(username, product) db.session.add(register) db.session.commit() return jsonify({"Message": "new register added susessfully"}) #Handling the GET request query = db.session.query(Cart, User, Product).join(User, User.id == Cart.user_id).join( Product, Product.id == Cart.product_id).all() output = [(user.name, product.product_name) for cart, user, product in query] return jsonify({"Result": output})
def delete(self, request, pk): request_data = get_request_data(request, 'delete') queryset, serializer_class = self.get_queryset(pk) if queryset.exists(): timestamp = now() instance = queryset[0] cart_id = instance.cart_id buyer = Buyer.get_active_objects(id=request.user.id) if buyer.exists(): cart = Cart.get_active_objects(buyer_id=buyer[0].id) if str(cart_id) == str(cart[0].id): instance.is_deleted = True instance.deleted_at = timestamp instance.modified_at = timestamp instance.save() return response({'deleted':True}, status.HTTP_200_OK) return error_response(CONST_ERROR_MESSAGE_PERMISSION_DENIED_KEY, status.HTTP_400_BAD_REQUEST) return error_response(CONST_ERROR_MESSAGE_INVALID_USER_KEY, status.HTTP_400_BAD_REQUEST) return error_response(CONST_ERROR_MESSAGE_INVALID_ID_KEY,status.HTTP_400_BAD_REQUEST)
def create(self, validate_data): cart = Cart.get_or_create_object(validate_data["buyer"].id) return cart