[Zope3-Users] Creating objects in software space when making site

Jeff Shell eucci.group at gmail.com
Tue Mar 7 11:06:15 EST 2006


On 3/6/06, Florian Lindner <mailinglists at xgm.de> wrote:
> Hello,
> my content object depends on a number of utilities to be present. It is
> usually also used as a site. During development it happens often that I
> delete and recreate it.
> Can I install a hook or something so I can make that these utilies are being
> created when my objects becomes a site? How do I create utitlies in software
> space?

Check for when your root object gets added. I'm not sure if an event
is fired off when something 'becomes a site'. My pattern is to have an
Interface and Content Class for the site, and have the following in
'site.py':

from zope.app import zapi
from zope.interface import implements
from zope.app.component.interfaces import ISite
from zope.app.component.interfaces.registration import (
    ActiveStatus, InactiveStatus, IRegistered)
from zope.app.component.site import (LocalSiteManager, UtilityRegistration,
    setSite)
from zope.app.container.interfaces import IObjectAddedEvent
from zope.app.intid import IntIds
from zope.app.intid.interfaces import IIntIds


def setupSiteManagerSubscriber(event):
    """
    event is supposed to be an IObjectAddedEvent

    An event subscriber which calls setupSiteManager when a Site folder is
    added. This ensures that there's enough placeful context in place for the
    site configuration and utility registration to happen whenever a Site
    is added and an IObjectAddedEvent is fired off.
    """
    assert IObjectAddedEvent.providedBy(event)

    if IMyApplicationSite.providedBy(event.object):
        setupSiteManager(event.object)

def setupSiteManager(context):
    """
    Configures the application manager's local utilities.
    """
    if not ISite.providedBy(context):
        context.setSiteManager(LocalSiteManager(context))
    # Required for getUtility to work within this request
    setSite(context)

    # Get the default registration
    default = context.getSiteManager()['default']
    reg_manager = default.registrationManager

    if 'intid' not in default:
        # There's a shorter way to do this, but I can never remember what it is
        intid = IntIds()
        default['intid'] = intid
        intids_reg = UtilityRegistration('', IIntIds, intid)
        reg_manager.addRegistration(intids_reg)
        intids_reg.status = ActiveStatus

    # Set up the 'auth' utility
    if 'auth' not in default:
        # This is a custom function that wraps up all of the work involved
        # setting up the pluggable auth utility and its sources.
        from myapp.security.browser import establish
        establish.createAuthenticationUtility(
            default, realm='Killer App', registerAs=''
            )

    # etc...

And then in configure.zcml:
  <subscriber for="zope.app.container.interfaces.IObjectAddedEvent"
      handler=".site.setupSiteManagerSubscriber"/>


--
Jeff Shell


More information about the Zope3-users mailing list