Example #1
0
    def _url(self, path, preview=False, relative=False, host=None):
        plugin = self.request.get_plugin(IVirtualHosting)
        if not IForestHosting.providedBy(plugin):
            return super(ContentURL, self)._url(path, preview, relative, host)

        virtual_host = plugin.host
        if host is not None:
            virtual_host = plugin.service.query(url2tuple(host))
        if virtual_host is None:
            return super(ContentURL, self)._url(path, preview, relative, host)

        rule, index = virtual_host.by_path.get(path[1:], fallback=True)
        if rule is None:
            raise BadRequest(
                u"No virtual host is defined for %s" % self.context)
        path = list(path[index + 1:])

        if preview is True:
            preview_position = max(
                len(self.context.get_root().getPhysicalPath()) - (index + 1), 0)
            path.insert(preview_position, '++preview++')

        if relative:
            return '/' + '/'.join(rule.server_script + path)
        return '/'.join([rule.url,] + path)
Example #2
0
    def __init__(self, root, url, rewrites):
        self.url = url
        self.key = utils.url2tuple(url)
        self.by_url = utils.TupleMap()
        self.by_path = utils.TupleMap()

        base = self.key[3:]
        for rewrite in rewrites:
            rule = RewriteRule(root, url, rewrite)
            try:
                self.by_url.add(
                    base + utils.path2tuple(rewrite.original),
                    rule)
            except KeyError:
                raise ValueError(
                    u"Duplicate url entry for %s in %s" % (
                        rewrite.original, url))
            try:
                self.by_path.add(rule.path, rule)
            except KeyError:
                raise ValueError(
                    u"Duplicate path entry for %s in %s" % (
                        rewrite.rewrite, url))
Example #3
0
    def _url(self, path, preview=False, relative=False, host=None):
        plugin = self.request.get_plugin(IVirtualHosting)
        if not IForestHosting.providedBy(plugin):
            return super(SimpleURL, self)._url(path, preview, relative, host)

        virtual_host = plugin.host
        if host is not None:
            virtual_host = plugin.service.query(url2tuple(host))
        if virtual_host is None:
            return super(SimpleURL, self)._url(path, preview, relative, host)

        rule, index = virtual_host.by_path.get(path[1:], fallback=True)
        if rule is None:
            logger.error(
                u"No virtual host defined to compute URL for path %s.",
                '/'.join(path))
            # Broken fallback that will generate a relative URL to
            # prevent breaking the ZMI.
            return '/'.join(path)

        path = list(path[index + 1:])
        if relative:
            return '/' + '/'.join(rule.server_script + path)
        return '/'.join([rule.url,] + path)