Exemplo n.º 1
0
    def _find_sub_controllers(self, remainder):
        """
        Identifies the correct controller to route to by analyzing the
        request URI.
        """
        # need either a get_one or get to parse args
        method = None
        for name in ("get_one", "get"):
            if hasattr(self, name):
                method = name
                break
        if not method:
            return

        # get the args to figure out how much to chop off
        args = getargspec(getattr(self, method))
        fixed_args = len(args[0][1:]) - len(request.pecan.get("routing_args", []))
        var_args = args[1]

        # attempt to locate a sub-controller
        if var_args:
            for i, item in enumerate(remainder):
                controller = getattr(self, item, None)
                if controller and not ismethod(controller):
                    self._set_routing_args(remainder[:i])
                    return lookup_controller(controller, remainder[i + 1 :])
        elif fixed_args < len(remainder) and hasattr(self, remainder[fixed_args]):
            controller = getattr(self, remainder[fixed_args])
            if not ismethod(controller):
                self._set_routing_args(remainder[:fixed_args])
                return lookup_controller(controller, remainder[fixed_args + 1 :])
Exemplo n.º 2
0
    def route(self, node, path):
        '''
        Looks up a controller from a node based upon the specified path.

        :param node: The node, such as a root controller object.
        :param path: The path to look up on this node.
        '''

        path = path.split('/')[1:]
        try:
            node, remainder = lookup_controller(node, path)
            return node, remainder
        except NonCanonicalPath, e:
            if self.force_canonical and \
                    not _cfg(e.controller).get('accept_noncanonical', False):
                if request.method == 'POST':
                    raise RuntimeError(
                        "You have POSTed to a URL '%s' which "
                        "requires a slash. Most browsers will not maintain "
                        "POST data when redirected. Please update your code "
                        "to POST to '%s/' or set force_canonical to False" %
                        (request.pecan['routing_path'],
                            request.pecan['routing_path'])
                    )
                redirect(code=302, add_slash=True)
            return e.controller, e.remainder
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
 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)
Exemplo n.º 5
0
 def get_controller(self, state):
     '''
     Retrieves the actual controller name from the application
     Specific to Pecan (not available in the request object)
     '''
     path = state.request.pecan['routing_path'].split('/')[1:]
     controller, reminder = lookup_controller(state.app.root, path)
     return controller.__str__().split()[2]
Exemplo n.º 6
0
    def _handle_delete(self, method, remainder):
        '''
        Routes ``DELETE`` actions to the appropriate controller.
        '''
        if remainder:
            controller = getattr(self, remainder[0], None)
            if controller and not ismethod(controller):
                return lookup_controller(controller, remainder[1:])

        # 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)
Exemplo n.º 7
0
 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)
Exemplo n.º 8
0
    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)