Creating Zope Products - Inner Mechanisms (Page 3 of 4 )
Now that we've described our Product by building a class, we'll have to work on a few inner mechanisms required to actually use our Product. The first two mechanisms we add to our Product will be responsible for creating instances of that Product. The first one will accept information about the instance to be created, such as the id, title and nickname attributes we make define in __init__. This data will then be passed to the second mechanism. This second mechanism will be responsible for actually adding the instance to Zope based on the data we entered into the first mechanism.
All that's needed for the first mechanism is a simple form that requests the data we use in __init__. Note that this is not included in our Product class, but it does go into the same file.
...
def addForm(self):
"Accept basic information about the instance."
out = '<html><head><title>Person Product</title></head><body>'
out = out + '<form action="addProduct">'
out = out + '<b>ID:</b> <input type="text" name="id" /><br />'
out = out + '<b>Name:</b> <input type="text"
name="title" /><br />'
out = out + '<b>Nickname:</b> <input type="text"
name="nickname" /><br />'
out = out + '<input type="submit"></form></body></html>'
return out
As I said above, the addForm method is simply a form that accepts the information we will use to create our instance, id, title and nickname. Once we have this information, we can then use the information to actually create an instance of our Product. To do this, we use the _setObject method:
def addProduct(self, id, title, nickname):
"Add an instance."
self._setObject(id, Person(id, title, nickname))
return "Object created."
We pass the _setObject method two arguments. The first is the id of our instance, and the second is an actual instance of our Person class, to which we pass the information obtained in addForm. Finally, we return a message stating that our object has been created.
Next: Finishing Touches >>
More Zope Articles
More By Peyton McCullough