[Zope] Calling ZClass constructors programmatically...

sean.upton@uniontrib.com sean.upton@uniontrib.com
Thu, 19 Apr 2001 11:18:16 -0700


Sorry to bring up this subject, as it seems to be a popular question on the
list, but I'm not sure if there is a complete answer to the question in the
list archives, HOWTOs, etc.  I'm sure I am not alone in trying to find an
answer to this...

I have been trying a bunch of different things to instantiate a ZClass
instance (in this case, it is a custom folder subclassed from
ZCatalog:CatalogAware, and OFS:Folder).  The ZClass constructor (which works
when called by direct traversal from the factory form) is another
PythonScript, and I am trying to call the constructor via a PythonScript
elsewhere in the ODB hierarchy; my code to do that looks like this:

#---------------------------------------------------------------------------
---
container.Control_Panel.Products.Classifieds.AdFolder_add(id=`item.id`,
title=item['category'], shortTitle=item['category'], locationDepth=1,
redir=0)
#---------------------------------------------------------------------------
---

The problem with this is that it creates the instance in the namespace of
the product folder where the AdFolder_add constructor resides.  I would use
container.manage_addProduct['Classifeds'].AdFolder_add() ... but this won't
work, because manage_addProduct doesn't understand what 'Classifieds' is
(though it does when called through the web and this also works fine
programmatically for OFS:Folder...).  

The code for my constructor is below; I am wondering if there is a way to
adjust the script that calls the constructor to have it (somehow) pass the
namespace of the calling folder (acquisition parent of the calling script)
to the constructor method?

Any help anyone might have would be greatly appreciated.  If I can find an
answer, I will try to piece together a howto...

Thanks,
Sean


## Script (Python) "AdDayFolder_add"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=id, title, shortTitle, redir
##title=
##
#---------------------------------------------------------------------------
---
#--- Trap for no id, this sucks, and we quit...
 if not id:
   return 'Doh!  Need an id for object constructor.  Try again...'

#--- Setup some defaults, in case params passed to this script aren't
there...
 if not title:
   title="Ads"
 if not shortTitle:
   shortTitle='Ads'
 if not locationDepth:
   locationDepth='1'
 if not redir:
   redir='1'   #default is to redirect to instance manager, see comments
below

#--- Set parameters passed to this script as REQUEST form params...
 container.REQUEST.set('title', title)
 container.REQUEST.set('shortTitle', shortTitle)
 container.REQUEST.set('locationDepth', locationDepth)

#--- First, add new instance of an AdFolder class object within the 
#--- current context / parent folder / aquisition parent
 
container.manage_addProduct['Classifieds'].AdFolder.createInObjectManager(id
, container.REQUEST)

#--- Secondly, sync the parameters passed to this method with the 
#--- Property Sheet of the AdFolder instance
 container[id].propertysheets.Basic.manage_editProperties(container.REQUEST)

#--- Now re-index with catalog...
 container[id].reindex_object()

#--- Now, by default, we assume this is a web-request initiated constructor
#--- method that is going to need to redirect the browser to some URL, so if
#--- redir=1 (default), then this is going to redirect to the management
#--- interface URL for the new object instance; there are 2 other options,
#--- each with it's own numerical code as possible (used) values of redir:
#--- 
#---      0 - No Redirect (redirect off)
#---      1 - Redirect default to instance management screen (redirect on)
#---      2 - Redirect to context/parent folder's management screen

 if (redir == '1'):
   return
container.REQUEST.RESPONSE.redirect(container[id].absolute_url()+'/manage_wo
rkspace')
 if (redir == '2'):
   return
container.REQUEST.RESPONSE.redirect(container.absolute_url()+'/manage_worksp
ace')
 else:
   return 'OK' #Default return statement, in the case of no redirect
#---------------------------------------------------------------------------
---

=========================
Sean Upton
Senior Programmer/Analyst
SignOnSanDiego.com
The San Diego Union-Tribune
619.718.5241
sean.upton@uniontrib.com
=========================