It's a long weekend. Got nothing better to do, so I spent some time grokking Five.
To do that, I have to cross reference between many documentation:
- Zope 3 quickstart
- codespeak Five manual
- Five and plone tutorial
- Jeffry Shell's post (I have yet to get to read and understand this
- rocky's great introduction to Five
And what do I get after all this? I think I get what interfaces and Views are, and where it is in Five. I still can't get adapter, and how it fits. The plone and five tutorial has some mention about it, but I think I need to reread the references above.
If anybody has a real world example for a simpleton like me, then I thank you.
Here's my Five hello product. Tested on Zope 2.8.3+
Step 1:
Require the package. In this case, it's a directory in INSTANCE_HOME/Products/ola
Step 2:
Require the __init__.py :
import hello
def initialize(context):
context.registerClass(
hello.Hello,
constructors = (hello.manage_addHelloForm,
hello.manage_addHello),
)
Step 3:
Draft out the interfaces:
from zope.interface import Interface
class IHello(Interface):
def manage_editHello():
"""edit hello instance"""
def returnGreetz():
"""return greetings, accept nothing"""
Step 4:
have the main class, in this case, ola.py:
class Ola:
def __init__(self, greetz, subject):
"""init wih greetz and subject"""
self.greetz = greetz
self.title = self.subject = subject
def returnGreetz(self):
"""return the greetings"""
return "%s %s!" % (self.greetz, self.subject)
Step 5:
Now the zope wrapper, hello.py. Everything is pretty much the same as any Zope2 product. See the ZDG for more information:
from zope.interface import implements
#import persistent
from OFS.SimpleItem import SimpleItem
#from Products.PageTemplates.PageTemplateFile import PageTemplateFile
from interfaces import IHello
from Globals import DTMLFile, HTMLFile
from AccessControl.Role import RoleManager
import ola
class Hello(ola.Ola, SimpleItem):
"""my hello"""
implements(IHello)
meta_type = 'Hello'
manage_options=(
{'label' : 'Edit', 'action' : 'editHelloForm', 'help': ('KebasData
','edit.txt')
},
) + SimpleItem.manage_options
def __init__(self, id, greetz='hello', subject='world'):
"""the init"""
ola.Ola.__init__(self, greetz, subject)
self.id = id
#self.greetz = greetz
#self.subject = subject
#self.title = subject
def manage_editHello(self, greetz, subject, REQUEST=None):
"""edit hello item"""
self.greetz = greetz
self.subject = subject
if REQUEST is None:
return \
REQUEST.RESPONSE.redirect(REQUEST['URL1'] + '/manage_main')
manage_addHelloForm = DTMLFile(
"www/helloAdd", globals(),
__name__ = 'manage_addHelloForm')
def manage_addHello(self, id, greetz, subject, REQUEST=None):
"""the constructor"""
id = self._setObject(id, Hello(id, greetz, subject))
if REQUEST is None:
return
REQUEST.RESPONSE.redirect(REQUEST['URL1'] + '/manage_main')
Step 6:
Now the ZCML, configure.zcml. Get the Five directives at codespeak. I'm not quite sure whether I need to have OFS.Folder.Folder traversable. And I have only understand a few of the directives. We define the Views, permissions, and other stuff (what?) here or some other separate files.:
<configure xmlns="http://namespaces.zope.org/zope"
xmlns:browser="http://namespaces.zope.org/browser"
xmlns:five="http://namespaces.zope.org/five">
<five:traversable class="Products.ola.ola.Ola" />
<browser:page
for=".interfaces.IHello"
name="index_html"
template="www/index.zpt"
permission="zope2.View"
/>
<browser:page
for=".interfaces.IHello"
name="editHelloForm"
template="www/editForm.zpt"
permission="zope2.ViewManagementScreens"
/>
</configure>
This works, as you can see from this
I welcome comments and feedback, especially on adapters and browsers.py. Never too late to learn.
If this shows my lack of understanding, please correct me :))
Be a fool to be cool?
Trackback is http://myzope.kedai.com.my/blogs/kedai/60/tbping
Thanks, this was helpful. However the product as posted here is not complete. The forms for add and edit are missing as well as the index_html page template. I could get it to work (after fixing up the indentation because of the copy/paste mess up).
In the zcml file it seems to me that
five:traversable class="Products.ola.ola.Ola"
could be replaced by
five:traversable class=".hello.Hello"
but both versions seem to work.
was intentional :P
i could have supplied the tar ball though.
i'll scrape things up and put it in.
as for the five:traversable directive, i'm not sure why both ways work, but my gut feeling is that both classes are traversable?
anybody?
Thinking about it, it seems to me that if the Ola class is traversable then the Hello class is as well, just be cause of inheritance. After all an instance of the Hello class is an instance of Ola as well. The same isn't true the other way around: an Ola instance is not an Hello instance.
So I guess it just depends on what you are after. If you have multiple classes all inheriting from Ola and you want them all be traversable, then making Ola traversable is the proper way to go. Otherwise you have to be more specific and pick which derived classes need to be traversable.
and if you do grok the other five directives, do tell.
thanks
with better explanation, from a guy with more zen points :)
http://slinkp.com/~paul/pycon_2006/z2/slides.html
