patch to ZopePageTemplate.py
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 -
This patch makes sense (even if it can be made slightly shorter -- text=None is a valid argument to ZopePageTemplate). The best way to contribute it is to add a Collector issue at http://collector.zope.org/Zope with your patch attached. Thanks, Florent In article <JHEOKEOOLIGLDHCMAHMOIEEIECAA.mark@mceahern.com> you write:
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
-
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
-- Florent Guillaume, Nuxeo (Paris, France) +33 1 40 33 79 87 http://nuxeo.com mailto:fg@nuxeo.com
[Florent Guillaume [mailto:fg@nuxeo.com]]
This patch makes sense (even if it can be made slightly shorter -- text=None is a valid argument to ZopePageTemplate). The best way to contribute it is to add a Collector issue at http://collector.zope.org/Zope with your patch attached.
Thank you for the tip. I submitted the patch here: http://collector.zope.org/Zope/596 Cheers, // mark -
participants (2)
-
Florent Guillaume -
Mark McEahern