将现有 config.add_route、金字塔 1.4 升级到 1.10

upgrading existing config.add_route, pyramid 1.4 to 1.10

我正在处理的代码:

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)

这在金字塔 1.4 上完美运行,到目前为止,我已经查看了 1.4 源代码和 1.10 源代码,这是我发现的不同之处,但我似乎找不到修复它的方法。

参考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):

参考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
    ):

请注意金字塔 1.10 版本的 add_route 中没有参数 view,有什么建议吗?我正在查看更改日志,如果我碰巧早点找到它,这可能是一个有趣的解决方案,可以发布在平台上。

从更改说明到 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.

例如:

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

有关详细信息,请参阅 View Configuration 上的文档。我更喜欢使用函数装饰器来配置视图。