Пример #1
0
    def dispatch(self, request, **resources):

        # Fix PUT and PATH methods in Django request
        request = fix_request(request)

        # Send ADREST started signal
        api_request_started.send(self, request=request)

        # Send current api started signal
        if self.api:
            self.api.request_started.send(self, request=request)

        # Current HTTP method
        method = request.method.upper()

        try:

            # Check request method
            self.check_method_allowed(method)

            # Authentificate
            self.authenticate(request)

            # Throttle check
            self.throttle_check()

            if request.method != 'OPTIONS' or not settings.ALLOW_OPTIONS:

                # Get required resources
                resources = self.get_resources(request,
                                               resource=self,
                                               **resources)

                # Check owners
                self.check_owners(**resources)

                # Check rights for resources with this method
                self.check_rights(resources, request=request)

                # Parse content
                request.data = self.parse(request)

            # Get the appropriate create/read/update/delete function
            view = getattr(self, self.callmap.get(method, None))

            # Get function data
            response = view(request, **resources)

            # Serialize response
            response = self.emit(response, request=request)
            response["Allow"] = ', '.join(self.allowed_methods)
            response["Vary"] = 'Authenticate, Accept'

        except HttpError, e:
            response = SerializedHttpResponse(e.content, status=e.status)
            response = self.emit(response, request=request, emitter=e.emitter)
Пример #2
0
    def dispatch(self, request, **resources):

        # Fix PUT and PATH methods in Django request
        request = fix_request(request)

        # Send ADREST started signal
        api_request_started.send(self, request=request)

        # Send current api started signal
        if self.api:
            self.api.request_started.send(self, request=request)

        # Current HTTP method
        method = request.method.upper()

        try:

            # Check request method
            self.check_method_allowed(method)

            # Authentificate
            self.authenticate(request)

            # Throttle check
            self.throttle_check()

            if request.method != 'OPTIONS' or not settings.ALLOW_OPTIONS:

                # Get required resources
                resources = self.get_resources(request, resource=self, **resources)

                # Check owners
                self.check_owners(**resources)

                # Check rights for resources with this method
                self.check_rights(resources, request=request)

                # Parse content
                request.data = self.parse(request)

            # Get the appropriate create/read/update/delete function
            view = getattr(self, self.callmap.get(method, None))

            # Get function data
            response = view(request, **resources)

            # Serialize response
            response = self.emit(response, request=request)
            response["Allow"] = ', '.join(self.allowed_methods)
            response["Vary"] = 'Authenticate, Accept'

        except HttpError, e:
            response = SerializedHttpResponse(e.content, status=e.status)
            response = self.emit(response, request=request, emitter=e.emitter)
Пример #3
0
    def dispatch(self, request, *args, **kwargs):

        # Save request for later use
        self.request = request

        # Send started signal
        api_request_started.send(self, request = request)

        # Current HTTP method
        method = request.method.upper()

        try:

            # Check request method
            self.check_method_allowed(method)

            # Authentificate
            # We do not restrict access for OPTIONS request.
            if method == 'OPTIONS' and not AUTHENTICATE_OPTIONS_REQUEST:
                self.identifier = 'anonymous'
            else:
                self.identifier = self.authenticate()

            # Throttle check
            self.throttle_check()

            # Get required resources
            resources = self.parse_resources(**kwargs)

            # Parse content
            if method in ('POST', 'PUT'):
                request.data = self.parse()
                if isinstance(request.data, basestring):
                    request.data = dict()

            # Get the appropriate create/read/update/delete function
            func = getattr(self, self.callmap.get(method, None))

            # Get function data
            content = func(request, *args, **resources)
            response = Response(content)

        except HttpError, e:
            response = Response(e.message, status=e.status)