[Zope-dev] The Application object

Shane Hathaway shane@digicool.com
Mon, 17 Jul 2000 12:50:27 -0400


Chris McDonough wrote:
> 
> > Sorry I wasn't clear enough. The Singleton is a design
> > pattern from the
> > Gang of Four book that fits this situation well. It is a
> > class that is
> > meant to control access to the one and only instance of a global
> > variable. I was just using that as a frame of reference,
> > though; Zope has
> > no such class for the Application object, or I wouldn't be
> > writing this
> > message.
> 
> Oh, ok, sorry, hard to tell intent from actual code.  :-)
> 
> >
> > I just need some way of getting at the one and only
> > Application object at
> > run-time. Something like:
> >
> >   from Globals import app
> >   myOb = app.Control_Panel.Products.MyProduct.MyZClass('foo')
> >   ...
> >
> > Given a reference to an object in the ZODB, I can do this via:
> >
> >   app = self.getPhysicalRoot()
> >   myOb = app.Control_Panel.Products.MyProduct.MyZClass('foo')
> >
> > But, this does not work from methods like __init__, or
> > __setstate__, where
> > we do not have a physical location in the ZODB, yet.
> 
> Ah, I see the problem.
> 
> I'm afraid I don't know the answer.
> 
> I generally use the manage_afterAdd and manage_beforeDelete methods to
> do things like this.
> 
> I suppose you could pass the root object into the constructor.
> __setstate__... well.  Err...
> 
> Anybody else?

The way to get the root application object is to open a connection to
the ZODB and get the 'Application' object from the list of root
objects.  There is an easy shortcut:

import Zope
app = Zope.app()

app now refers to a *copy* of the root Application object.  When you're
done modifying it and its descendants you have to commit or abort the
transaction.

get_transaction().commit()  (or abort())

This gives you all kinds of benefits like undo and elimination of
threading conflicts.  You *must* remember to close the connection:

app._p_jar.close()

Shane