[Zope] load_site.py and zpt files
jbr@FIZ-Karlsruhe.DE
jbr@FIZ-Karlsruhe.DE
Mon, 16 Sep 2002 20:35:56 +0200 (MEST)
Hello,
with the help of Dieter Maurer and Oliver Bleutgen ,
(thank you very much for your detailed and helpfull explanations),
I was able to enhance load_site.py to upload files with extension ".zpt" as
zope page templates.
In a copy of load_site.py ( load_site_fiz.py) I added the two functions below:
def upload_zpt(object, f):
import ZPublisher.Client
# verbose = 4 # aktivate prints for test
dir, name = os.path.split(f)
myurl = "%s/manage_addProduct/PageTemplates/manage_addPageTemplate" % object.url
if verbose > 3:
print "myurl='%s'\n" % myurl
# create zope page template with standard content
call_client(myurl, username=object.username, password=object.password, id=name, title='', text=f)
# upload real content into zope page template just created
f = open(f, 'rb')
try: ZPublisher.Client.call("%s/%s/pt_upload" % (object.url, name),
username=object.username, password=object.password, file=f)
except ServerError, v:
if str(v)[:1] != '3':
raise sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]
def call_client(url, username=None, password=None, **kw):
import ZPublisher.Client
try: apply(ZPublisher.Client.Function(url,username=username, password=password), (), kw)
except ServerError, v:
if str(v)[:1] != '3':
raise sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]
With kind regards, Juergen Berger
> From myzope@gmx.net Mon Sep 16 18:35 MET 2002
> Subject: Re: [Zope] load_site.py and zpt files
>
> 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