def create(self, request): req_body = json.loads(request.body.decode()) new_order = Order() new_order.customer_id = request.auth.user.customer.id new_order.save() new_orderproduct = OrderProduct() products = Product.objects.all() new_orderproduct.order_id = new_order.id new_orderproduct.product_id = req_body['product_id'] new_orderproduct.save() serializer = OrderSerializer(new_order, context={'request': request}) return Response(serializer.data)
def create(self, request): """Handle POST operations Returns: Response -- JSON serialized Order instance """ new_order = Order() # uncomment this line when AUTH is ready new_order.customer_id = request.auth.user.customer.id # use this line UTNIL AUTH is ready # new_order.customer_id = request.data["customer_id"] new_order.save() serializer = OrderSerializer(new_order, context={'request': request}) return Response(serializer.data)
def create(self, request): """ @api {POST} /cart POST new line items to cart @apiName AddLineItem @apiGroup ShoppingCart @apiSuccessExample {json} Success HTTP/1.1 204 No Content @apiParam {Number} product_id Id of product to add """ current_user = Customer.objects.get(user=request.auth.user) try: open_order = Order.objects.get( customer=current_user, payment_type__isnull=True) except Order.DoesNotExist as ex: open_order = Order() open_order.created_date = datetime.datetime.now() open_order.customer = current_user open_order.save() line_item = OrderProduct() line_item.product = Product.objects.get(pk=request.data["product_id"]) line_item.order = open_order line_item.save() return Response({}, status=status.HTTP_204_NO_CONTENT)
def create(self, request): """Handle POST operations Returns: Response -- JSON serialized ParkArea instance """ order_item = OrderProduct() order_item.product = Product.objects.get(pk=request.data["product_id"]) current_customer = Customer.objects.get(pk=request.user.id) order = Order.objects.filter(customer=current_customer, payment_type=None) if order.exists(): order_item.order = order[0] else: new_order = Order() new_order.customer = current_customer new_order.save() order_item.order = new_order order_item.save() return Response({}, status=status.HTTP_204_NO_CONTENT)
def create(self, request): """Handle POST operations Returns: Response -- JSON serialized ParkArea instance """ neworder = Order() neworder.created_date = request.data["created_date"] customer = Customer.objects.get(id=request.data["customer_id"]) neworder.customer = customer neworder.save() serializer = OrderSerializer(neworder, context={'request': request}) return Response(serializer.data)
def test_complete_order(self): """ Ensure that an order is completed by adding a payment type """ order = Order() order.customer_id = 1 order.created_date = datetime.date.today() order.payment_type = None order.save() data = {"payment_type": self.payment.id} url = f"/orders/{order.id}" self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) response = self.client.put(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) test_order = Order.objects.get(pk=order.id) test_payment = Payment.objects.get(pk=self.payment.id) self.assertEqual(test_order.payment_type, test_payment)
def create(self, request): """Handle POST operations Returns: Response -- JSON serialized product category instance """ new_order_product = OrderProduct() new_order_product.product = Product.objects.get( pk=request.data["product"]) # new_order_product.order = request.data["order_id"] # new_order_product.product = request.data["product_id"] new_order_product.quantity = request.data["quantity"] customer = Customer.objects.get(user=request.auth.user) try: neworder = Order.objects.get(customer=customer, payment_type__isnull=True) except Order.DoesNotExist: neworder = Order() neworder.created_date = datetime.date.today() neworder.customer = customer neworder.save() new_order_product.order = neworder new_order_product.save() # if order.payment_type is not "NULL": # ordered_items = order.invoiceline.all() # for oi in ordered_items: # ordered_products.add(oi.product) # products = list(ordered_products) # for p in products: # num_sold = p.item.filter(order=order).count() # p.quantity = p.new_inventory(num_sold) # p.save() serializer = OrderProductSerializer(new_order_product, context={'request': request}) return Response(serializer.data)
def cart(self, request): """Shopping cart manipulation""" current_user = Customer.objects.get(user=request.auth.user) if request.method == "DELETE": """ @api {DELETE} /profile/cart DELETE all line items in cart @apiName DeleteCart @apiGroup UserProfile @apiHeader {String} Authorization Auth token @apiHeaderExample {String} Authorization Token 9ba45f09651c5b0c404f37a2d2572c026c146611 @apiSuccessExample {json} Success HTTP/1.1 204 No Content @apiError (404) {String} message Not found message. """ try: open_order = Order.objects.get(customer=current_user, payment_type=None) line_items = OrderProduct.objects.filter(order=open_order) line_items.delete() open_order.delete() except Order.DoesNotExist as ex: return Response({'message': ex.args[0]}, status=status.HTTP_404_NOT_FOUND) return Response({}, status=status.HTTP_204_NO_CONTENT) if request.method == "GET": """ @api {GET} /profile/cart GET line items in cart @apiName GetCart @apiGroup UserProfile @apiHeader {String} Authorization Auth token @apiHeaderExample {String} Authorization Token 9ba45f09651c5b0c404f37a2d2572c026c146611 @apiSuccess (200) {Number} id Order cart @apiSuccess (200) {String} url URL of order @apiSuccess (200) {String} created_date Date created @apiSuccess (200) {Object} payment_type Payment Id used to complete order @apiSuccess (200) {String} customer URI for customer @apiSuccess (200) {Number} size Number of items in cart @apiSuccess (200) {Object[]} line_items Line items in cart @apiSuccess (200) {Number} line_items.id Line item id @apiSuccess (200) {Object} line_items.product Product in cart @apiSuccessExample {json} Success { "id": 2, "url": "http://localhost:8000/orders/2", "created_date": "2019-04-12", "payment_type": null, "customer": "http://localhost:8000/customers/7", "line_items": [ { "id": 4, "product": { "id": 52, "url": "http://localhost:8000/products/52", "name": "900", "price": 1296.98, "number_sold": 0, "description": "1987 Saab", "quantity": 2, "created_date": "2019-03-19", "location": "Vratsa", "image_path": null, "average_rating": 0, "category": { "url": "http://localhost:8000/productcategories/2", "name": "Auto" } } } ], "size": 1 } @apiError (404) {String} message Not found message """ try: open_order = Order.objects.get(customer=current_user, payment_type=None) line_items = OrderProduct.objects.filter(order=open_order) line_items = LineItemSerializer(line_items, many=True, context={'request': request}) cart = {} cart["order"] = OrderSerializer(open_order, many=False, context={'request': request}).data #cart["order"]["line_items"] = line_items.data cart["order"]["size"] = len(line_items.data) except Order.DoesNotExist as ex: return Response({'message': ex.args[0]}, status=status.HTTP_404_NOT_FOUND) return Response(cart["order"]) if request.method == "POST": """ @api {POST} /profile/cart POST new product to cart @apiName AddToCart @apiGroup UserProfile @apiHeader {String} Authorization Auth token @apiHeaderExample {String} Authorization Token 9ba45f09651c5b0c404f37a2d2572c026c146611 @apiSuccess (200) {Object} line_item Line items in cart @apiSuccess (200) {Number} line_item.id Line item id @apiSuccess (200) {Object} line_item.product Product in cart @apiSuccess (200) {Object} line_item.order Open order for cart @apiSuccessExample {json} Success { "id": 14, "product": { "url": "http://localhost:8000/products/52", "deleted": null, "name": "900", "price": 1296.98, "description": "1987 Saab", "quantity": 2, "created_date": "2019-03-19", "location": "Vratsa", "image_path": null, "customer": "http://localhost:8000/customers/7", "category": "http://localhost:8000/productcategories/2" }, "order": { "url": "http://localhost:8000/orders/2", "created_date": "2019-04-12", "customer": "http://localhost:8000/customers/7", "payment_type": null } } @apiError (404) {String} message Not found message """ try: open_order = Order.objects.get(customer=current_user, payment_type=None) print(open_order) except Order.DoesNotExist as ex: open_order = Order() open_order.created_date = datetime.datetime.now() open_order.customer = current_user open_order.save() line_item = OrderProduct() line_item.product = Product.objects.get( pk=request.data["product_id"]) line_item.order = open_order line_item.save() line_item_json = LineItemSerializer(line_item, many=False, context={'request': request}) return Response(line_item_json.data) if request.method == "PUT": """ { "id": 21, "url": "http://localhost:8000/orders/21", "created_date": "2021-02-16", "payment_type": "http://localhost:8000/paymenttypes/9", "customer": "http://localhost:8000/customers/7", "lineitems": [ { "id": 33, "product": { "id": 88, "name": "Element", "price": 1727.41, "number_sold": 12, "description": "2003 Honda", "quantity": 3, "created_date": "2019-05-28", "location": "Dukoh", "image_path": null, "average_rating": -1 } } ] } """ try: open_order = Order.objects.get(customer=current_user, payment_type=None) open_order.payment_type = Payment.objects.get(pk=request.data["payment_type"]) open_order.save() except Order.DoesNotExist as ex: return Response({'message': ex.args[0]}, status=status.HTTP_404_NOT_FOUND) open_order_json = OrderSerializer(open_order, many=False, context={'request': request}).data return Response(open_order_json, status=status.HTTP_204_NO_CONTENT) return Response({}, status=status.HTTP_405_METHOD_NOT_ALLOWED)