Florian Lindner 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?


Hi,

I'm creating a special add view of my application (). If you add a foo
it will be created with a LocalSiteManager and a catalog. Hope this
helps.

Any other ways of doing this ?

- Andreas

in your browser configure:

<configure
  xmlns="http://namespaces.zope.org/browser";>

  <addform
    label="Add foo with initialization"
    name="AddMyContent.html"
    class=".foo.AddFoo"
    schema="myapp.interfaces.IFoo"
    content_factory="myapp.foo.Foo"
    fields="name"
    permission="zope.ManageContent"
    />
...
</configure>

Now create the view class you defined in the browser configure.zcml inside foo.py:

from zope.app import zapi
from zope.proxy import removeAllProxies
from zope.app.component import site
from zope.app.catalog.catalog import Catalog
from zope.app.catalog.interfaces import ICatalog
from zope.app.event.objectevent import ObjectCreatedEvent
from zope.event import notify
from myapp.helper import addLocalUtility

class Foo(object):
   class AddFoo(object):
    """ Add a foo.
This special addform class is needed to add special initialization. (Catalog init)
    """

    def createAndAdd(self,data):
        content = super(AddFoo, self).createAndAdd(data)
        # get foo object, stripping off security proxy
        bare = removeAllProxies(content)
        # create a LocalSiteManager
        sm = site.LocalSiteManager(bare)
        # and assign it to Labor folder
        content.setSiteManager(sm)

        # Catalog setup

        cat = Catalog()
        addLocalUtility(bare,'fooCatalog',
                        ICatalog,
                        cat)
        notify(ObjectCreatedEvent(cat))

I found addLocalUtility somewhere in the zope mailing lists.

from zope.app import zapi
from zope.app.container.interfaces import INameChooser
from zope.app.component.interfaces.registration import ActiveStatus
from zope.app.component.interfaces import ISite
from zope.app.utility import UtilityRegistration

def addLocalUtility(site, name, iface, utility, package='default'):
    """Add a utility to a site

    The utility is added to the package and activated.
    This assumes the site has already a Utility Service.
    """

    # preconditions
    if not ISite.providedBy(site):
        raise TypeError('ISite required.')

    # get site manager and site management folder
    sitemanager = site.getSiteManager()
    default = sitemanager[package]

    # add utility to site management folder
    chooser = INameChooser(default)
    folder_name = chooser.chooseName(utility.__name__, utility)
    default[folder_name] = utility

    # create service registration
    path = zapi.getPath(utility)
    registration = UtilityRegistration(name, iface, utility)
    key = default.registrationManager.addRegistration(registration)
    zapi.traverse(default.registrationManager, key).status = ActiveStatus

    return zapi.traverse(sitemanager, path)

_______________________________________________
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users

Reply via email to