Previous Chapter | Next Chapter | Up | Next Section | Contents

Simple Example: Publishing Objects with External Methods


To publish an object, it must be part of the object hierarchy so that Zope can traverse it. One way to insert an object into the Zope object hierarchy is to use External Methods.

The first thing we'll do is create a module to hold our publishable classes. Let's create our own package inside the Shared package. Create a directory inside lib/python/Shared named PeeWee (or use your own name). Next create an empty file __init__.py inside the PeeWee directory. This lets Python know that PeeWee is a package. Now, let's create a Python file, PlayHouse.py inside the PeeWee directory.

class PlayHouse:

"Play house"

 

def index_html(self,game='tag'):

"publish the playhouse"

return "In the play house we play %s." % game

 

OK, now we have a publishable class that is Zope accessible. Now we need to get an instance of that class into the object hierarchy. Let's use an External Method. Create a Python file in Extensions named peewee_utils.py from:

Shared.PeeWee.PlayHouse import PlayHouse

def addPlayHouse(self,id):

"Add a playhouse to the current folder"

if not hasattr(self,id):

setattr(self,id,PlayHouse())

return "playhouse added with id %s" % id

else:

return "id %s already in use" % id

Now create an External Method object to give us access to the addPlayHouse method. Suppose we give our External Method the id addPlayHouse , and tell it to use the addPlayHouse method of the peewee_utils module.

We can now call our method and install a publishable object in the Zope object hierarchy.

myFolder/addPlayHouse?id=myHouse

This should return "playhouse added with id mouse." What the method did was create a PlayHouse instance and make it an attribute of the myFolder object. Now we can access the PlayHouse like this:

myFolder/myHouse?game=hide+and+seek

Which should return "In the play house we play hide and seek."

Note that you will not see any PlayHouse object in the management screen. This is because it is not a Zope Product. However, you can indirectly sense that it is there, if you try to create a DTML Method with id myHouse you will be told that myHouse is already in use.

Previous Chapter | Next Chapter | Up | Next Section | Contents