示例#1
0
 def root_url(self, name=None):
     obj = self.context
     while obj is not None:
         if IRootFolder.providedBy(obj):
             return self.url(obj, name)
         obj = obj.__parent__
     raise ValueError("No application nor root element found.")
示例#2
0
 def test_notify(self):
     for setup in (lambda: None), self.createRootFolder, self.createRFAndSM:
         setup()
     bootStrapSubscriber(EventStub(self.db))
     cx = self.db.open()
     root = cx.root()
     root_folder = root.get(ZopePublication.root_name, None)
     self.assertTrue(IRootFolder.providedBy(root_folder))
     package_name = '/++etc++site/default'
     traverse(root_folder, package_name)
     cx.close()
示例#3
0
def get_root_folder():
    """Get the Root Folder of a Zope instance."""
    site = grok.getSite()
    if IApplication.providedBy(site):
        return site.__parent__
    else:
        # another sub-site is within the application, so
        # walk up the tree until we get to the root folder
        parent = site.__parent__
        while not IRootFolder.providedBy(parent):
            parent = parent.__parent__
        return parent
示例#4
0
 def accessSessionOnRootTraverse(self, event):
     if IRootFolder.providedBy(event.object):
         ISession(event.request)
示例#5
0
def threadSiteSubscriber(new_site, event):
    """
    Set the current ``zope.component.hooks`` site to
    the ``new_site`` object found during traversal,
    being careful to maintain any previously installed host (site-name)
    configurations as lower priority than the new site.

    Sites encountered during traversal are expected to have the
    main application site (e.g., ``nti.dataserver``) in their base chain
    so we have access to its configuration and persistent utilities.
    This implies that sites encountered during traversal are either
    synthetic (generated by a traversal adapter to use some particular
    ``IComponents``)  or themselves persistent.

    Because of this, when we encounter the root or dataserver folders
    as sites, we take no action.

    We expect that something else takes care of clearing the site.
    """

    if IMainApplicationFolder.providedBy(new_site) or IRootFolder.providedBy(new_site):
        # TODO: Since we get these events, we could
        # actually replace nti.appserver.tweens.zope_site_tween
        # with this. That's probably the longterm answer.
        return

    current_site = getSite()
    if current_site is None:
        # Nothing to do
        setSite(new_site)
    elif current_site is new_site:
        # This is typically the case when we traverse directly
        # into utilities registered with the site, for example
        #   /dataserver2/++etc++hostsites/janux.ou.edu/++etc++site/SOMEUTILITY/...
        # with the current host being janux.ou.edu.
        pass
    elif IHostPolicyFolder.providedBy(current_site) and \
         IHostPolicyFolder.providedBy(new_site):
        # This is typically the case when we traverse directly
        # into utilities registered with the site, for example
        #   /dataserver2/++etc++hostsites/janux.ou.edu/++etc++site/SOMEUTILITY/...
        # with the current host NOT being janux.ou.edu.
        # We do not want to switch host configurations here, but we do
        # want to allow traversal, so we take no action.
        # TODO: We might want to only allow this if there is some
        # inheritance relationship between the two sites?
        pass
    elif hasattr(current_site.getSiteManager(), 'host_components'):
        # A site synthesized by get_site_for_site_names
        # OR one previously synthesized by this function. In either case,
        # we always want to proxy, putting the preserved host components
        # at the end of the new proxy RO.
        host_components = current_site.getSiteManager().host_components
        # We need to keep host_components in the bases
        # for the new site. Where to put it is tricky
        # if we want to support multiple layers of overriding
        # of host registrations. Fortunately, the zope.interface.ro
        # machinery does exactly the right thing if we tack host
        # components (which are probably not already in the list)
        # on to the end. If they are in the list already, they
        # stay where they were.
        new_bases = new_site.getSiteManager().__bases__ + (host_components,)
        # TODO: We don't need to proxy the site manager, right?
        # it's almost never special by itself...
        new_site_manager = BasedSiteManager(new_site.__parent__,
                                            new_site.__name__,
                                            new_bases)
        new_site_manager.host_components = host_components
        new_fake_site = _ProxyTraversedSite(new_site,
                                            new_site_manager)

        setSite(new_fake_site)
    else:
        # Cancel traversal using a LocationError. This typically
        # will get surfaced as a 404.
        raise LocationError("Unknown kind of site", new_site, current_site)