Esempio n. 1
0
    def put_url_params_to_patch(self, request, **kwargs):
        # Only allow PUT
        if request.method != 'PUT':
            raise ImmediateHttpResponse(response=http.HttpNotImplemented())

        # Mimic a PATCH request
        request.method = 'PATCH'

        # Modify request body
        try:
            data = self.deserialize(request,
                                    request.body,
                                    format=request.META.get(
                                        'CONTENT_TYPE', 'application/json'))
        except:
            data = {}

        for k in kwargs.keys():
            if k not in [
                    'api_name', 'resource_name', self._meta.detail_uri_name
            ]:
                # Use pop() so as to remove the url params from kwargs filters
                data[k] = kwargs.pop(k)

        request._body = json.dumps(data)
        request.META['CONTENT_TYPE'] = 'application/json'

        # Call dispatch_detail as though it was originally a PATCH
        return self.dispatch_detail(request, **kwargs)
Esempio n. 2
0
    def dispatch_public(self, request_type, request, **kwargs):
        """
            Same as `tastypie.resources.Resource.dispatch` except that
            we don't check if the user is authenticated
        """
        allowed_methods = getattr(self._meta,
                                  "%s_allowed_methods" % request_type, None)

        if 'HTTP_X_HTTP_METHOD_OVERRIDE' in request.META:
            request.method = request.META['HTTP_X_HTTP_METHOD_OVERRIDE']

        request_method = self.method_check(request, allowed=allowed_methods)
        method = getattr(self, "%s_%s" % (request_method, request_type), None)

        if method is None:
            raise ImmediateHttpResponse(response=http.HttpNotImplemented())

        self.throttle_check(request)

        # All clear. Process the request.
        request = convert_post_to_put(request)
        response = method(request, **kwargs)

        # Add the throttled request.
        self.log_throttled_access(request)

        # If what comes back isn't a ``HttpResponse``, assume that the
        # request was accepted and that some action occurred. This also
        # prevents Django from freaking out.
        if not isinstance(response, HttpResponse):
            return http.HttpNoContent()

        return response
Esempio n. 3
0
    def dispatch(self, request_type, request, **kwargs):
        """
        Handles the common operations (allowed HTTP method, authentication,
        throttling, method lookup) surrounding most CRUD interactions.

        This version moves the authorization check to later in the pipeline
        """
        allowed_methods = getattr(self._meta, "%s_allowed_methods" % request_type, None)
        request_method = self.method_check(request, allowed=allowed_methods)
        method_name = "%s_%s" % (request_method, request_type)
        method = getattr(self, method_name, None)

        if method is None:
            raise ImmediateHttpResponse(response=http.HttpNotImplemented())

        self.is_authenticated(request)
        if method_name not in self._meta.delayed_authorization_methods:
            self.is_authorized(request, **kwargs)
        self.throttle_check(request)

        # All clear. Process the request.
        request = convert_post_to_put(request)
        response = method(request, **kwargs)

        # Add the throttled request.
        self.log_throttled_access(request)

        # If what comes back isn't a ``HttpResponse``, assume that the
        # request was accepted and that some action occurred. This also
        # prevents Django from freaking out.
        if not isinstance(response, HttpResponse):
            return http.HttpNoContent()

        return response
Esempio n. 4
0
    def nested_dispatch_list(self, request, **kwargs):
        """
        Handles the common operations (allowed HTTP method, authentication,
        throttling, method lookup) surrounding most CRUD interactions.
        """
        allowed_methods = self._meta.nested_allowed_methods

        if 'HTTP_X_HTTP_METHOD_OVERRIDE' in request.META:
            request.method = request.META['HTTP_X_HTTP_METHOD_OVERRIDE']

        request_method = self.method_check(request, allowed=allowed_methods)
        method = getattr(self, "%s_list" % request_method, None)

        if method is None:
            raise ImmediateHttpResponse(response=http.HttpNotImplemented())

        self.is_authenticated(request)
        self.throttle_check(request)

        # All clear. Process the request.
        request = convert_post_to_put(request)
        response = method(request, **kwargs)

        # Add the throttled request.
        self.log_throttled_access(request)

        # If what comes back isn't a ``HttpResponse``, assume that the
        # request was accepted and that some action occurred. This also
        # prevents Django from freaking out.
        if not isinstance(response, HttpResponse):
            return http.HttpNoContent()

        return response
Esempio n. 5
0
        def inner(self, request, **kwargs):
            """
            Handle ``{method}`` request.

            Implement ``async_{method} to add custom request, returning
            celery.result.AsyncResult. If it returns None, HttpBadRequest
            is returned from this method. If you don't implement the
            custom method, it will return HttpNotImplemented.
            """
            try:
                result = getattr(self, 'async_' + method)(
                    request, **self.remove_api_resource_names(kwargs))
                if result is None:
                    return http.HttpBadRequest()
            except NotImplementedError:
                return http.HttpNotImplemented()

            if isinstance(result, AsyncResult):
                if isinstance(result, EagerResult):
                    EAGER_RESULTS[result.id] = result
                response = http.HttpAccepted()
                response['Location'] = self._build_reverse_url(
                    'api_async_state',
                    kwargs={
                        'api_name': self._meta.api_name,
                        'resource_name': self._meta.resource_name,
                        'task_id': result.id
                    })
                return response
            else:
                return result
Esempio n. 6
0
 def dehydrate_response(self, bundle):
     ## see what the context request is
     context_request = bundle.request.GET['request']
     
     if context_request == EnvrionmentContextResource.PEOPLE_COUNT:
         environment = bundle.obj
         environment_people_count = UserContext.objects.filter(currentEnvironment = environment).count()
         
         return environment_people_count
     else:
         raise ImmediateHttpResponse(response=http.HttpNotImplemented())
Esempio n. 7
0
    def post_detail(self, request, **kwargs):
        """
        Emulate put_detail in a very specific case

        The Tastypie docs describe the semantics of this method as
        "Creates a new subcollection of the resource under a resource"

        This isn't what we want, but we need to implement this
        method in order to support replacing an image asset's image
        in older browsers such as IE9. For the older browsers, we have to
        send the request via a form in a hidden IFRAME. We can use the
        PUT method with forms, so we have to use POST.
        """
        # Only support POST to the detail endpoint if we're making the
        # request from inside a hidden iframe 
        if not self.iframed_request(request):
            return http.HttpNotImplemented()

        # In the case of a POST from inside a hidden iframe, delegate
        # to put_detail
        return self.put_detail(request, **kwargs)
    def dispatch(self, request_type, request, **kwargs):
        """
        Same as the usual dispatch, but knows if its being called from a nested
        resource.
        """
        allowed_methods = getattr(self._meta,
                                  "%s_allowed_methods" % request_type, None)
        request_method = self.method_check(request, allowed=allowed_methods)

        method = getattr(self, "%s_%s" % (request_method, request_type), None)

        if method is None:
            raise ImmediateHttpResponse(response=http.HttpNotImplemented())

        self.is_authenticated(request)
        self.throttle_check(request)

        parent_resource = kwargs.get('parent_resource', None)
        if parent_resource is None:
            self.is_authorized(request)
        else:
            self.is_authorized_nested(request, kwargs['nested_name'],
                                      parent_resource, kwargs['parent_object'])

        # All clear. Process the request.
        request = convert_post_to_put(request)
        response = method(request, **kwargs)

        # Add the throttled request.
        self.log_throttled_access(request)

        # If what comes back isn't a ``HttpResponse``, assume that the
        # request was accepted and that some action occurred. This also
        # prevents Django from freaking out.
        if not isinstance(response, HttpResponse):
            return http.HttpNoContent()

        return response
Esempio n. 9
0
    def dispatch(self, request_type, request, **kwargs):
        if not waffle.switch_is_active('stats-api'):
            raise ImmediateHttpResponse(response=http.HttpNotImplemented())

        return super(GlobalStatsResource,
                     self).dispatch(request_type, request, **kwargs)
Esempio n. 10
0
 def wait_detail(self, request, **kwargs):
     # TODO: Implement
     return http.HttpNotImplemented()