RandomFolder for Zope 3

I’ve restarted my research about Zope 3 in the last few days. This time, I went a little further. I’ve written a small product so that I was forced to debug some of the internals of Zope 3 and learn something from it.

The product

While going through the Zope 3 API, I’ve wondered how I would implement something close to the RandomQuote Moin macro, including my hack to render quotes, like images. After thinking a little bit about it, I decided to implement a RandomFolder; something which would render one of the contained items randomly.

How it was implemented

After some study, the RandomFolder was implemented like this:

  1. Defined a dummy interface IRandomFolder, inheriting from IFolder.
  2. Defined a dummy type RandomFolder, inheriting from Folder and implementing IRandomFolder.
  3. Defined RandomFolderTraverser, inheriting from ContainerTraverser, and defining a custom browserDefault() method. This method does the real trick: instead of returning in the view name something like “@@index.html”, as usual, it will return a subpage name, randomly.
  4. Created a configure.zcml file, configuring the new type. The tricky part was to include a view adapter from IRandomFolder to IBrowserPresentation with the RandomFolderTraverser.

Result

Using the given scheme, the RandomFolder may be managed exactly as a normal Folder when including items and viewing the management interface. Contained items may also be accessed individually without any side effects. Even index.html may be used to list the folder content. OTOH, when the folder is accessed without any subpath following it, it will select one of its children randomly, and render it.

Random thoughts about Zope 3

I’m really amazed by the Component Architecture used in Zope 3. While the learning curve seems to be the same due to the many details of the framework, these details look more consistent now, reusing similar algorithms all over the place. Also, I’m happy to see a product being developed the way Zope 3 is being. It looks like everything is done the right way, no matter how much time it takes. Of course, this will probably hurt the schedule; but given the inherent complexity of these systems, I’d rather wait a little bit than getting into an evolutive cycle faster than necessary.

This entry was posted in Python. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *