def dispatch(self, request, *args, **kwargs): ''' Check if this is a POST and has the X-RPC-Action header. If so, dispatch the request to the method, if it exists and is publishable. Otherwise behaves as normal. ''' method = request.META.get('HTTP_X_RPC_ACTION', None) if request.method != 'POST' or method is None: return super(RPCMixin, self).dispatch(request, *args, **kwargs) func = getattr(self, method, None) if not is_rpc_method(func): return http.PreconditionFailed() try: data = self.get_request_data({}) # Ensure data is valid for passing as **kwargs (lambda **kwargs: None)(**data) except (ValueError, TypeError): return http.BadRequest() resp = self.execute(func, data) return http.JsonResponse(resp)
def post(self, request): # If there's an existing order, cancel its items order_id = request.session.get('order_id') if order_id: models.OrderItem.objects.filter(order__pk=order_id).update( status=models.OrderItem.STATE.CANCELLED) cart = Cart.from_session(self.request.session) if not cart: return http.BadRequest() order = models.Order.from_cart(request.user, cart) request.session['order_id'] = order.pk return self.single_response(object=order)
def dispatch_rpc(self, request, function_name): func = getattr(self, function_name, None) if not is_rpc_method(func): return http.PreconditionFailed() data = self.get_request_data({}) sig = inspect.signature(func) try: sig.bind(**data) except TypeError as e: return http.BadRequest(e.args[0]) resp = self.execute(func, data) return http.JsonResponse(resp)
def dispatch(self, request, *args, **kwargs): method = request.META.get('HTTP_X_RPC_ACTION', None) if request.method != 'POST' or method is None: return super(RPCMixin, self).dispatch(request, *args, **kwargs) func = getattr(self, method, None) if not is_rpc_method(func): return http.PreconditionFailed() try: data = self.get_request_data({}) # Ensure data is valid for passing as **kwargs (lambda **kwargs: None)(**data) except (ValueError, TypeError): return http.BadRequest() resp = self.execute(func, data) return http.JsonResponse(resp)