30.01.2006
Installation von Zope3-Utilities, die Kontext benötigen
Um ein Utility, welches Kontext benötigt (beispielsweise ein Index eines Katalogs) beim Zope-Start oder programmatisch in einer Site zu installieren sind einige Kniffe nötig.
by
Michael Howitz
—
posted at
2006-01-30 10:29
last modified
2006-01-30 11:31
Zur Installation von lokalen Utilities beim Zope-Start siehe Installation von Utilities beim Zope-Start.
Hier nur der nötige Programmcode, um den Kontext zu erhalten.
import transaction
from zope.app.intid.interfaces import IIntIds
from zope.app.catalog.interfaces import ICatalog
from zope.app.component import hooks
from zope.app import zapi
from zope.app.appsetup.bootstrap import ensureUtility, getInformationFromEvent
from zope.app.intid import IntIds
from zope.app.catalog.catalog import Catalog
from zope.app.catalog.field import FieldIndex
from exampleapp.interfaces import IColor
def bootStrapSubscriber(event):
"""Subscriber to the IDataBaseOpenedEvent
Create local utilities if not yet present
"""
db, connection, root, root_folder = getInformationFromEvent(event)
ensureUtility(root_folder, IIntIds, '', IntIds, copy_to_zlog=False)
ensureUtility(root_folder, ICatalog, '', Catalog, copy_to_zlog=False)
catalog = zapi.getUtility(ICatalog, context=root_folder)
if not catalog.get('interfaces'):
try:
# Die Installation eines Index erwartet Kontext, den Zope in
# einer thread-globalen Variablen vorhält. Diese muss jetzt
# gesetzt werden, weil die Site ja gerade erst erstellt wird.
old_site = hooks.getSite()
hooks.setSite(root_folder)
# add index
catalog['color'] = FieldIndex('color', IColor, field_callable=False)
finally:
# Die alte Site wieder herstellen
hooks.setSite(old_site)
transaction.commit()
connection.close()
- Category(s)
- Zope 3
- The URL to Trackback this entry is:
- http://blog.gocept.com/installation-von-zope3-utilities-die-kontext-benoetigen/tbping