[ZPT] Problems using PageTempateFile from a disk product

Kevin Gill kevin@focussed.com
Wed, 6 Feb 2002 16:12:57 +0000


Hi,

I am trying to use PageTemplateFile in an disk product. My development is on
Zope 2.5.

It is a catch22 problem. I can do things two ways, one gives me a storage
error, the other gives me an attribute error.

Problem 1: Storage Error
-----------------

If I create the PageTemplateFile object during the initialisation of the
class, I get an exception when I restart Zope and view the page again.

 Error Type: StorageError
 Error Value: Instance of AntiPersistent class PageTemplateFile cannot be
        stored.

Storage error is demonstrated in the enclosed code (View3).

Problem 2: Attribute Error
------------------

If I create the PageTemplateFile in the method which is displaying the class,
then the object is not created properly. I get teh following error:

 Error Type: AttributeError
 Error Value: other

AttributeError is demonstrated in the enclosed code (View2)

This leaves me with no mechanism for using PageTemplateFile from a python
disk product.

I would appreciate it anyone could give me pointers.

I enclose some demonstration code. The following files comprise a simple
diskproduct. If you create the disk-product it provides 3 View Options, View1
should work, View2 should demonstrate the AttributeError problem, and View3
should demonstrate the StorageError problem (After restarting).

Thanks

Kevin

------------------------- Demonstration Code ----------------

Files:
	MyClass/__init__.py - Package Initialisation
	MyClass/MyClass.py - Logic of MyClass class
	MyClass/html/manage_addMyClassForm.zpt - manage_add form
	MyClass/viewMyClass.zpt - a view form


MyClass/__init__.py
---------------


__doc__="""Demonstration of ZPT problem"""

import MyClass

Products = [MyClass]

def initialize(context):
        for Product in Products:
                Product.ProductInitialise(context)


MyClass/MyClass.py
---------------

"""
        Trivial Disk Product, to demonstrate problem with PageTemplate.
"""

from OFS import SimpleItem, ObjectManager
from Interface import Base
from Products.PageTemplates.PageTemplateFile import PageTemplateFile

class IMyClass(Base):
        """ This is a basic interface for a basic task """
        def View1(self):
                " Display the task "
        def View2(self):
                " Display the task "
        def View3(self):
                " Display the task "


class MyClass(SimpleItem.SimpleItem, ObjectManager.ObjectManager):
        """Simple class for tasks"""

        __implements__ = (IMyClass,)
        meta_type="MyClass"
        manage_options = ObjectManager.ObjectManager.manage_options + (
                {'label': 'View1', 'action': 'View1'},
                {'label': 'View2', 'action': 'View2'},
                {'label': 'View3', 'action': 'View3'},
        ) + SimpleItem.SimpleItem.manage_options


        def __init__(self, id, title=None):
                """Initialise the product"""
                self.id = id
                if title == None: self.title = id
                else:  self.title = title
                self.description = ""
                self.viewFormDefault = PageTemplateFile('html/viewMyClass',
		globals())
                self.View1 = PageTemplateFile('html/viewMyClass', globals())

        def View2(self):
                """Action of the product"""
                viewFormDefault = PageTemplateFile('html/viewMyClass',
		globals())
                return viewFormDefault()

        def View3(self):
                """Action of the product"""
                return self.viewFormDefault()

def manage_addMyClass(dispatcher, id='MyClass', title='MyClass',
       RESPONSE=None):
        """Add a MyClass Class """
        NewObject = MyClass(id, title)
        dispatcher._setObject(id, NewObject)
        if RESPONSE:
                RESPONSE.redirect("manage_main")

#  Create the form for creating a MyClass system
manage_addMyClassForm = PageTemplateFile('html/manage_addMyClassForm',
        globals(), __name__='manage_addMyClassForm')

Constructors = [manage_addMyClassForm, manage_addMyClass]

def ProductInitialise(context):
     context.registerClass(instance_class=MyClass,
          constructors=Constructors)

MyClass/html/manage_addMyClassForm.zpt
-------------------------------

<h1 tal:replace="structure here/manage_page_header">Header</h1>

<h2 tal:define="form_title string:Add MyClass"
    tal:replace="structure here/manage_form_title">Form Title</h2>

<hr>
<b>This form is used to add a new MyClass.</b>
<hr>

<form name=addForm action="manage_addMyClass" method="post"
        enctype="multipart/form-data">
<table>
<tr>
        <td>ID:</td><td><input name=id type=text></td>
</tr><tr>
        <td>TITLE:</td><td><input name=title type=text></td>
</tr><tr>
        <td><input type=submit value=add></td><td></td>
</tr>
</form>

<h1 tal:replace="structure here/manage_page_footer">Footer</h1>


MyClass/viewMyClass.zpt
-------------------
hello, world

<p>
This is the view form.

-------------------------------------------------------