Example #1
0
    def route_url(self, route_name, *elements, **kw):
        """ See: pyramid.URLMethodsMixin.route_url
        """

        mapper = self.registry.getUtility(IRoutesMapper)
        route = mapper.get_route(route_name)

        if route is None:
            raise KeyError('No such route named %s' % route_name)

        if route.pregenerator is not None:
            elements, kw = route.pregenerator(self, elements, kw)

        app_url, scheme, host, port, qs, anchor = parse_url_overrides(kw)

        if app_url is None:
            if scheme is not None or host is not None or port is not None:
                app_url = self._partial_application_url(scheme, host, port)
            else:
                app_url = self.request.host_url
                application_path_url = (self.config.settings.get('application_path_url') or '').strip('/')
                if application_path_url:
                    app_url = string_join('/', (app_url.strip('/'), application_path_url))

        path = route.generate(kw)  # raises KeyError if generate fails

        if elements:
            suffix = _join_elements(elements)
            if not path.endswith('/'):
                suffix = '/' + suffix
        else:
            suffix = ''

        return app_url + path + suffix + qs + anchor
Example #2
0
    def static_url(self, path, **kw):
        if not isabs(path) and ':' not in path:
            package = caller_package()
            package_name = package.__name__
            path = '%s:%s' % (package_name, path)
        else:
            package_name = path.split(':', 1)[0]

        if package_name == self.package_name:
            registry = self.registry
        else:
            for application_name, config in APPLICATIONS.items():
                if config.package_name == package_name:
                    registry = config.registry
                    break
            else:
                registry = get_current_registry() # b/c

        info = registry.queryUtility(IStaticURLInfo)
        if info is None:
            raise ValueError('No static URL definition matching %s' % path)

        registrations = info._get_registrations(registry)
        api_route_url = getattr(self.applications, registry.application_name).route_url

        for (url, spec, route_name, cachebust) in registrations:
            if path.startswith(spec):
                subpath = path[len(spec):]
                if WIN: # pragma: no cover
                    subpath = subpath.replace('\\', '/') # windows
                if cachebust:
                    subpath, kw = cachebust(subpath, kw)
                if url is None:
                    kw['subpath'] = subpath
                    return api_route_url(route_name, **kw)
                else:
                    app_url, scheme, host, port, qs, anchor = parse_url_overrides(kw)
                    parsed = urlparse(url)
                    if not parsed.scheme:
                        url = urlunparse(parsed._replace(scheme=self.environ['wsgi.url_scheme']))
                    subpath = url_quote(subpath)
                    result = urljoin(url, subpath)
                    return result + qs + anchor

        raise ValueError('No static URL definition matching %s' % path)
Example #3
0
 def route_url(*a, **kw):
     # XXX: refactor DummyRequest to take advantage of `pyramid.testing`
     parts = parse_url_overrides(kw)
     return ''.join([p for p in parts if p])
Example #4
0
 def route_url(*a, **kw):
     # XXX: refactor DummyRequest to take advantage of `pyramid.testing`
     parts = parse_url_overrides(kw)
     return ''.join([p for p in parts if p])