[Zope] load_site.py and zpt files

Oliver Bleutgen myzope@gmx.net
Mon, 16 Sep 2002 18:32:54 +0200


jbr@FIZ-Karlsruhe.DE wrote:
> hello,
> 
> I am still trying to enhance load_site.py to create zope page template files
> for files with suffix .zpt .
> 
> Dieter Maurer has suggested to use Client.call     
> 
> 
>>From dieter@handshake.de Fri Sep 13 20:32 MET 2002
>>From: Dieter Maurer <dieter@handshake.de>
>>Subject: Re: [Zope] load_site.py and zpt files
>>
>>jbr@FIZ-Karlsruhe.DE writes:
>> > ...
>> >     call(object.manage_addProduct['PageTemplates'].manage_addPageTemplate( id=name, title='', text=f))
>> > ...
>> >     call(object.manage_addProduct['PageTemplates'].manage_addPageTemplate( id=name, title='', text=f))
>> > AttributeError: Function instance has no attribute '__getitem__'
>>"object.manage_addProduct" is a "Function" instance (defined in
>>"ZPublisher.Client"). It does not have a "__getitem__" instance.
>>Therefore, you cannot use subscription ("[...]").
>>
>>Use 'Client.call("%s/manage_addProduct/PageTemplates/manage_addPageTemplate" % URL_TO_OBJECT, id=name, title='', text=f)'
>>
>>
>>Dieter
>>
> 
> 
> therefore I changed function  upload_zpt:
> 
> def upload_zpt(object, f):
>     import ZPublisher.Client
> 
>     dir, name = os.path.split(f)
>     f=open(f)
> 
>     ZPublisher.Client.call("%s/manage_addProduct/PageTemplates/manage_addPageTemplate" % object.url, username=object.username, password=object.password, id=name, title='', text=f)
> 
>     if verbose: print 'in upload_zpt end of function'
> 
> 
> I tried to upload file: test35.zpt
> to URL:  http://a7dsid1:8040/Testordner
> where "Testordner" is a normal zope folder.
> 
>   object.url is: http://a7dsid1:8040/Testordner
>   id is: test35.zpt
> 
> But I have got some strange results:
> 1.)  a zope page template file http://a7dsid1:8040/Testordner/test35.zpt
>  was created, but a error was raised ( see trace below)

This is an easy one.
This error is due to the fact that you do a call like a webbrowser. 
Therefore manage_addTemplate gets a REQUEST variable passed, and most, 
if not all, "through the web callable" methods in zope contain something 
like:

if REQUEST is not None:
[return a redirect to another page]

Because it doesn't get a 200 response, ZPublisher.Client raises an 
exception. Look at the call() method of load_site to see how they handle 
this (they ignore exceptions caused by 3xy responses) and
just copy the code.


> 2.) the content of http://a7dsid1:8040/Testordner/test35.zpt is not the
>  the content of file test35.zpt (It seems that Parameter  text=f is ignored.)
> but a standard zope page template instead:

This is because manage_addPageTemplate handling of the uploaded file is 
slightly different from e.g. addDTMLMethod.
manage_addPageTemplate forces you to upload a File (in the sense of a 
<input type="File"...> form tag (maybe because it needs information 
about the content type?), while addDTMLMethod also works when text is 
just a string.
Look in the source of lib/python/Products/PageTemplates/ZopePageTemplate.py
for manage_addPageTemplate for more infos.

So the best might be to first add the zpt as you do, and afterwards call 
it's edit method to upload your text content, unfortunately I'm not 
experienced in ZPTs.
 From a glance at the source, maybe it suffices to call

ZPublisher.Client.call("%s/%s/pt_upload" % (object.url, name), 
username=object.username, password=object.password, file=f)

after you added the ZPT. Don't forget to catch any 3xy responses again.


HTH,
oliver