def post(self, request, user=None): """ api for returning an order :param request: http request for the view method allowed: POST http request should be authorised by the jwt token of the user :param user: User object of the requesting user :returns Response object with message and 200 status if no error message and corresponding status if error """ order = Order.objects.get(id=request.GET['order_id']) if user != order.user: return Response({'message': messages.RESTRICTED}, status=status.HTTP_403_FORBIDDEN) if order.status == Order.STATUS_CHOICES.RETURNED.value: return Response({'message': messages.ORDER_ALREADY_RETURNED}, status=status.HTTP_400_BAD_REQUEST) elif order.status == Order.STATUS_CHOICES.RETURN_PENDING.value: return Response({'message': messages.ORDER_RETURNED_PENDING}, status=status.HTTP_400_BAD_REQUEST) qr_code_url = request.build_absolute_uri( reverse("shop-return-qr-code")) order.status = Order.STATUS_CHOICES.RETURN_PENDING.value order.save() serializer = OrderSerializer(order) return Response( { 'message': messages.ORDER_RETURNING, 'qr_code_url': qr_code_url, 'order': serializer.data }, status=status.HTTP_200_OK)
def get(self, request, user=None): """ returns all the order of the particular user :param request: http request for the view method allowed: GET http request should be authorised by the jwt token of the user :param user: User object of the requesting user :returns Response object with list of order object and 200 status if no error message and corresponding status if error """ orders = Order.objects.filter(user=user) serializer = OrderSerializer(orders, many=True) response_data = dict(orders=serializer.data) return Response(response_data, status=status.HTTP_200_OK)
def get(self, request, order_id=None, user=None): """ order view for fetching a particular order :param request: http request for the view method allowed: GET http request should be authorised by the jwt token of the user :param order_id: order_id of the order referring to\ :param user: User object of the requesting user :returns Response object with order object and 200 status if no error message and corresponding status if error """ order = Order.objects.get(id=order_id) if user != order.user: return Response({'message': messages.RESTRICTED}, status=status.HTTP_403_FORBIDDEN) serializer = OrderSerializer(order) response_data = dict(orders=serializer.data) return Response(response_data, status=status.HTTP_200_OK)
def put(self, request, order_id=None, user=None): """ order view for updating a particular order :param request: http request for the view method allowed: PUT http request should be authorised by the jwt token of the user mandatory fields for POST and PUT http methods: ['product_id', 'quantity'] :param order_id: order_id of the order referring to mandatory for GET and PUT http methods :param user: User object of the requesting user :returns Response object with order object and 200 status if no error message and corresponding status if error """ request_data = request.data order = Order.objects.get(id=order_id) if user != order.user: return Response({'message': messages.RESTRICTED}, status=status.HTTP_403_FORBIDDEN) if 'quantity' in request_data: order.quantity = request_data['quantity'] if 'status' in request_data and not Order.STATUS_CHOICES.has_value( request_data['status']): return Response({'message': messages.INVALID_STATUS}, status=status.HTTP_400_BAD_REQUEST) user_details = UserDetails.objects.get(user=order.user) if 'status' in request_data and request_data['status'] != order.status: order.status = request_data['status'] if request_data['status'] == Order.STATUS_CHOICES.RETURNED.value: user_details.available_credit += float(order.quantity * order.product.price) user_details.save() order.save() serializer = OrderSerializer(order) response_data = dict(orders=serializer.data) return Response(response_data, status=status.HTTP_200_OK)