[Zope] creating instances of products "programmatically"
hans
hans@beehive.de
Fri, 22 Feb 2002 12:08:37 +0000
> Hi,
>
> I've got a Product question that I just haven't been able to answer
> using the documentation I can find.
>
> I've developed a Product called RDJM_JobImpl following the general
> procedure in the Developers Guide. The product class is defined so:
>
> class RDJM_JobImpl(Implicit,Persistent,RoleManager,ObjectManager)
>
> I've got enough stuff working so that I can add the product from the
> manage screen and the guts of the product itself work swimmingly.
>
> What I'd really like to be able to do is create instances of this
class
> programmatically (either through scripts or DTML methods or whatever)
> instead of through the management interface.
>
> This I cannot figure out how to do. Pointers to the appropriate
> documentation would be greatly appreciated.
###################################################################
well, i have:
############################################# File JumpTabs/Tabs.py
class Tabs(SimpleItem.SimpleItem):
#.... many lines omitted
#-----------------------------------------------------------
# product instantiation
############################################# File JumpTabs/Tabs.py
manage_addTabsForm = DTMLFile('manage_addTabsForm', globals())
# init instance Tabs(id, title) via webform
def manage_addTabs(dispatcher, id, title=default_title, render=0,
REQUEST=None):
"""Folder add(Tabs)"""
tabs = Tabs(id, title)
dispatcher.Destination()._setObject(id, tabs) # add tabs to destination
folder
if render:
tabs = dispatcher.Destination()._getOb(id) # in render absolute_url
needs this
dtml_text = tabs.render_show_dtml()
dtml_name = id+'_show_tabs' # avoids name clashes
dispatcher.manage_addDTMLMethod(id=dtml_name, file=dtml_text,
title=title)
if REQUEST is None: # alt: else: self.manage_main(self, REQUEST)
return
try:
u = dispatcher.DestinationURL()
except:
u = REQUEST['URL1']
REQUEST.RESPONSE.redirect(u + '/manage_main')
############################################# File __init__.py
import Tabs
def initialize(context):
"""init Tabs product. Makes Tabs appear in the add product list"""
context.registerClass(
Tabs.Tabs,
constructors = (
Tabs.manage_addTabsForm, # browser add Tabs product, hook into ZMI
Tabs.manage_addTabs, # program add Tabs product
),
icon = 'pics/Tabs.gif'
)
<dtml-comment>########### DTMLDoc manage_addTabsForm.dtml
</dtml-comment>
<snip>
<form name="form" action="<dtml-var URL1>"><br>
<tr>
<td><b>Id</b> (required):</td>
<td><input type="text" name="id:required" value="JT"
size="30"></td>
</tr>
<tr>
<td><b>Title</b>:</td>
<td><input type="text" name="title:string" size="30"></td>
</tr>
<tr>
<td><b>Render?</b>:
<input type="hidden" name="render:int:default" value="0" >
</td>
<td>
<input type="checkbox" name="render:int" checked value="1"
> render show_tabs.DTML in same folder?
</td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="submit" name="manage_addTabs:method" value=" Add
" />
</td>
</tr>
</form>
######################################################
as you might see, in manage_addTabsForm.dtml the form action is
(the method) manage_addTabs
calling manage_addTabs without keyword parameter REQUEST (ie REQEST is
None)
implies programatic call (not through the web)
and manage_addTabs just returns,
anything else leads to a RESPONSE (DTML/HTML whatever sent to the client
browser)
--------------------------------------------------------------
hans augustin (software developer) hans@beehive.de
beehive elektronische medien GmbH http://www.beehive.de
phone: +49 30 847-82 0 fax: +49 30 847-82 299