Search code examples
python-2.7viewupgradepyramid

upgrading existing config.add_route, pyramid 1.4 to 1.10


Code I'm working on:

def add(self, path, view, method='GET'):
        url = _join_url_paths(self.path_prefix, path)
        id_ = hashlib.md5('%s-%s' % (url, method)).hexdigest()
        view = _join_view_paths(self.view_prefix, view)
        if id_ in _routes and env.name == 'dev':
            mlog.debug('Detected duplicate route: %s, %s' % (url, view))
        _routes[id_] = Route(id_, url, view, method, self.model, self.parent)
        if self.model and path == '/' and self.model not in _model_routes:
            _model_routes[self.model] = _routes[id_]
        self.config.add_route(id_, url, view=view, request_method=method)

This works perfectly on pyramid 1.4 and so far I've taken a look into 1.4 source code and 1.10 source code here's the difference I found But I can't seem to find a way to fix it.

Reference 1.4

class RoutesConfiguratorMixin(object):
    @action_method
    def add_route(self,
                  name,
                  pattern=None,
                  view=None,
                  view_for=None,
                  permission=None,
                  factory=None,
                  for_=None,
                  header=None,
                  xhr=None,
                  accept=None,
                  path_info=None,
                  request_method=None,
                  request_param=None,
                  traverse=None,
                  custom_predicates=(),
                  view_permission=None,
                  renderer=None,
                  view_renderer=None,
                  view_context=None,
                  view_attr=None,
                  use_global_views=False,
                  path=None,
                  pregenerator=None,
                  static=False,
                  **predicates):

Reference 1.10

class RoutesConfiguratorMixin(object):
    @action_method
    def add_route(
        self,
        name,
        pattern=None,
        factory=None,
        for_=None,
        header=None,
        xhr=None,
        accept=None,
        path_info=None,
        request_method=None,
        request_param=None,
        traverse=None,
        custom_predicates=(),
        use_global_views=False,
        path=None,
        pregenerator=None,
        static=False,
        **predicates
    ):

Notice that there's no parameter view in add_route in version 1.10 of the pyramid, any suggestion? I'm taking a look into the changelogs, If I happen to find it earlier this could be an interesting bit of solution to be posted on the platform.


Solution

  • From the change notes to 1.5a2 (2013-09-22), Backwards Incompatibilities:

    Removed the ability to pass the following arguments to pyramid.config.Configurator.add_route: view, view_context, view_for, view_permission, view_renderer, and view_attr. Using these arguments had been deprecated since Pyramid 1.1. Instead of passing view-related arguments to add_route, use a separate call to pyramid.config.Configurator.add_view to associate a view with a route using its route_name argument. Note that this impacts the pyramid.config.Configurator.add_static_view function too, because it delegates to add_route.

    For example:

    self.config.add_route(id_, url)
    self.config.add_view(view=my_view_callable, route_name=id_, request_method=method)
    

    See the docs on View Configuration for more information. I prefer using function decorators for view configuration.