[Zope-dev] new FAQ entry: please review before I add it

Marius Gedminas mgedmin at b4net.lt
Fri Dec 14 15:48:17 EST 2007


On Fri, Dec 14, 2007 at 07:29:59PM +0100, Christophe Combelles wrote:
> how do I automatically create some needed object at application startup?
>
> Do it by subscribing to IDatabaseOpenedWithRootEvent (from 
> zope.app.appsetup)
>
> Example code:
>
> @adapter(IDatabaseOpenedWithRootEvent)
> def CreateMyContainer(event):
>     conn = event.database.open()
>     root = conn.root()['Application']

I would use ZopePublication.root_name instead of hardcoding
'Application' here.  Or maybe reuse getInformationFromEvent from
zope.app.appsetup.bootstrap, although it smells like an internal
function.

>     if 'mycontainer' not in root:
>         root['mycontainer'] = MyContainer()
>     transaction.commit()
>     conn.close()
>
> Then register this subscriber in your configure.zcml:
> <subscriber handler="myapp.CreateMyContainer" />

Marius Gedminas
-- 
<niemeyer> I'm wondering why we have defineChecker() defined twice with exactly
           the same implementation under zope.security.checker...
<niemeyer> Is it some kind of high-availability system? :-)
		-- #zope3-dev
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.zope.org/pipermail/zope-dev/attachments/20071214/3fbd85d8/attachment.bin


More information about the Zope-Dev mailing list