[Zope] patch to ZopePageTemplate.py

Mark McEahern mark@mceahern.com
Sat, 28 Sep 2002 11:14:53 -0500


I patched /zope/lib/python/Products/PageTemplates/ZopePageTemplate.py to
allow adding a Page Template to Zope via ZPublisher.Client.  The reason I
had to patch it is that when you add a Page Template via ZPublisher.Client,
the text and title arguments are ignored and so the resulting Page Template
has the default content rather than the content you specified.

I've appended my patch as well as a script that shows how I was using
ZPublisher.Client to add a Page Template below.  I'd be very interested to
hear any feedback on this approach.  If the patch makes sense, how do I
contribute it to Zope (if merely posting it here isn't sufficient)?

Thanks,

// mark

Here's the patch:

*** ZopePageTemplate.py.orig	Sat Sep 28 09:31:44 2002
--- ZopePageTemplate.py.fixed	Sat Sep 28 09:32:09 2002
***************
*** 316,326 ****
          file = REQUEST.form.get('file')
          headers = getattr(file, 'headers', None)
          if headers is None or not file.filename:
!             zpt = ZopePageTemplate(id)
          else:
              zpt = ZopePageTemplate(id, file, headers.get('content_type'))

          self._setObject(id, zpt)

          try: u = self.DestinationURL()
          except: u = REQUEST['URL1']
--- 316,338 ----
          file = REQUEST.form.get('file')
          headers = getattr(file, 'headers', None)
          if headers is None or not file.filename:
!             # 20020928 MMcEahern
!             # Patch to allow adding PageTemplate content via
!             # ZPublisher.Client interface.
!             if text is None:
!                 zpt = ZopePageTemplate(id)
!             else:
!                 zpt = ZopePageTemplate(id, text)
          else:
              zpt = ZopePageTemplate(id, file, headers.get('content_type'))

          self._setObject(id, zpt)
+         # 20020928 MMcEahern
+         # Patch to allow adding PageTemplate title via
+         # ZPublisher.Client interface.
+         if title:
+             ob = getattr(self, id)
+             ob.pt_setTitle(title)

          try: u = self.DestinationURL()
          except: u = REQUEST['URL1']


************ End of patch **************

Here's how I'm adding a Page Template:

#!/usr/local/zope/bin/python

__doc__ = \
"""
This is an experimental script for adding Page Templates.  You may need to
adjust the shebang line above to match your Zope--or, of course, just call
this explicitly with your Zope's python.

Other hard-coded stuff you may want to adjust is noted.
"""

# --------------------------------------------------------------------------
---
# Standard library imports
# --------------------------------------------------------------------------
---
import sys
import time
import os

# --------------------------------------------------------------------------
---
# BEGIN: Hard-coded stuff you may need to adjust.
# --------------------------------------------------------------------------
---
python_path = '/usr/local/zope/lib/python'
username = None
password = None
base_url = "http://www.foobar.com/somefolder"
# --------------------------------------------------------------------------
---
# END: Hard-coded stuff you may need to adjust.
# --------------------------------------------------------------------------
---

# --------------------------------------------------------------------------
---
# Zope imports
# --------------------------------------------------------------------------
---
# We can't import this until we modify sys.path.
sys.path.insert(0, python_path)
import ZPublisher.Client

# --------------------------------------------------------------------------
---
# Add a page template
# --------------------------------------------------------------------------
---
pt_url = "manage_addProduct/PageTemplates"

# 2-step, get the manage_addProduct object for PT, then add it.
url = os.path.join(base_url, pt_url)
z = ZPublisher.Client.Object(url, username=username, password=password)
try:
    # Use time.time() as a quick and dirty way to get a unique id.
    id = str(time.time())
    # Distinguish title from id.
    title = "%s - title" % id
    text = "<div class='content'></div>"
    z.manage_addPageTemplate(id=id, title=title, text=text)
except ZPublisher.Client.ServerError, e:
    redirect_prefix = "3"
    if not str(e).startswith(redirect_prefix):
        raise

-