예제 #1
0
파일: rest.py 프로젝트: joannataryma/pecan
    def _route(self, args):
        '''
        Routes a request to the appropriate controller and returns its result.

        Performs a bit of validation - refuses to route delete and put actions
        via a GET request).
        '''
        # convention uses "_method" to handle browser-unsupported methods
        if request.environ.get('pecan.validation_redirected', False) is True:
            #
            # If the request has been internally redirected due to a validation
            # exception, we want the request method to be enforced as GET, not
            # the `_method` param which may have been passed for REST support.
            #
            method = request.method.lower()
        else:
            method = request.params.get('_method', request.method).lower()

        # make sure DELETE/PUT requests don't use GET
        if request.method == 'GET' and method in ('delete', 'put'):
            abort(405)

        # check for nested controllers
        result = self._find_sub_controllers(args)
        if result:
            return result

        # handle the request
        handler = getattr(self, '_handle_%s' % method, self._handle_custom)
        result = handler(method, args)

        # return the result
        return result
예제 #2
0
파일: rest.py 프로젝트: keitheis/pecan
    def _handle_get(self, method, remainder):
        """
        Routes ``GET`` actions to the appropriate controller.
        """
        # route to a get_all or get if no additional parts are available
        if not remainder or remainder == [""]:
            controller = self._find_controller("get_all", "get")
            if controller:
                return controller, []
            abort(404)

        method_name = remainder[-1]
        # check for new/edit/delete GET requests
        if method_name in ("new", "edit", "delete"):
            if method_name == "delete":
                method_name = "get_delete"
            controller = self._find_controller(method_name)
            if controller:
                return controller, remainder[:-1]

        # check for custom GET requests
        if method.upper() in self._custom_actions.get(method_name, []):
            controller = self._find_controller("get_%s" % method_name, method_name)
            if controller:
                return controller, remainder[:-1]
        controller = getattr(self, remainder[0], None)
        if controller and not ismethod(controller):
            return lookup_controller(controller, remainder[1:])

        # finally, check for the regular get_one/get requests
        controller = self._find_controller("get_one", "get")
        if controller:
            return controller, remainder

        abort(404)
예제 #3
0
파일: rest.py 프로젝트: cajun-code/pecan
 def _handle_get(self, method, remainder):
     
     # route to a get_all or get if no additional parts are available
     if not remainder:
         controller = self._find_controller('get_all', 'get')
         if controller:
             return controller, []
         abort(404)
     
     # check for new/edit/delete GET requests
     method_name = remainder[-1]
     if method_name in ('new', 'edit', 'delete'):
         if method_name == 'delete':
             method_name = 'get_delete'
         controller = self._find_controller(method_name)
         if controller:
             return controller, remainder[:-1]
     
     # check for custom GET requests
     if method.upper() in self._custom_actions.get(method_name, []):
         controller = self._find_controller('get_%s' % method_name, method_name)
         if controller:
             return controller, remainder[:-1]
     controller = getattr(self, remainder[0], None)
     if controller and not ismethod(controller):
         return lookup_controller(controller, remainder[1:])
     
     # finally, check for the regular get_one/get requests
     controller = self._find_controller('get_one', 'get')
     if controller:
         return controller, remainder
     
     abort(404)
예제 #4
0
파일: rest.py 프로젝트: cajun-code/pecan
 def _handle_custom(self, method, remainder):
     
     # try finding a post_{custom} or {custom} method first
     controller = self._find_controller('post_%s' % method, method)
     if controller:
         return controller, remainder
     
     # if no controller exists, try routing to a sub-controller; note that 
     # since this isn't a safe GET verb, any local exposes are 405'd
     if remainder:
         if self._find_controller(remainder[0]):
             abort(405)
         sub_controller = getattr(self, remainder[0], None)
         if sub_controller:
             return lookup_controller(sub_controller, remainder[1:])
     
     abort(404)
예제 #5
0
파일: rest.py 프로젝트: cajun-code/pecan
 def _handle_delete(self, method, remainder):
     
     # check for post_delete/delete requests first
     controller = self._find_controller('post_delete', 'delete')
     if controller:
         return controller, remainder
     
     # if no controller exists, try routing to a sub-controller; note that 
     # since this is a DELETE verb, any local exposes are 405'd
     if remainder:
         if self._find_controller(remainder[0]):
             abort(405)
         sub_controller = getattr(self, remainder[0], None)
         if sub_controller:
             return lookup_controller(sub_controller, remainder[1:])
     
     abort(404)
예제 #6
0
파일: rest.py 프로젝트: keitheis/pecan
    def _handle_delete(self, method, remainder):
        """
        Routes ``DELETE`` actions to the appropriate controller.
        """
        # check for post_delete/delete requests first
        controller = self._find_controller("post_delete", "delete")
        if controller:
            return controller, remainder

        # if no controller exists, try routing to a sub-controller; note that
        # since this is a DELETE verb, any local exposes are 405'd
        if remainder:
            if self._find_controller(remainder[0]):
                abort(405)
            sub_controller = getattr(self, remainder[0], None)
            if sub_controller:
                return lookup_controller(sub_controller, remainder[1:])

        abort(404)
예제 #7
0
파일: rest.py 프로젝트: cajun-code/pecan
    def _handle_post(self, method, remainder):

        # check for custom POST/PUT requests
        if remainder:
            method_name = remainder[-1]
            if method.upper() in self._custom_actions.get(method_name, []):
                controller = self._find_controller('%s_%s' % (method, method_name), method_name)
                if controller:
                    return controller, remainder[:-1]
            controller = getattr(self, remainder[0], None)
            if controller and not ismethod(controller):
                return lookup_controller(controller, remainder[1:])
        
        # check for regular POST/PUT requests
        controller = self._find_controller(method)
        if controller:
            return controller, remainder
        
        abort(404)
예제 #8
0
파일: rest.py 프로젝트: cajun-code/pecan
 def _route(self, args):
     
     # convention uses "_method" to handle browser-unsupported methods
     method = request.params.get('_method', request.method).lower()
     
     # make sure DELETE/PUT requests don't use GET
     if request.method == 'GET' and method in ('delete', 'put'):
         abort(405)
     
     # check for nested controllers
     result = self._find_sub_controllers(args)
     if result:
         return result
     
     # handle the request
     handler = getattr(self, '_handle_%s' % method, self._handle_custom)
     result = handler(method, args)
     
     # return the result
     return result