Tuesday, June 12, 2007

A thought on methods in Plone

Note: Because of the limitations of blogger, I am replacing the '<' and '>' in XHTML statements with '[' and ']'.

We just did a sprint on our big project at work. My first step was to create a bunch of methods in the content types that would traverse between items via the reference catalog (yes Virginia, we are using the Relations product along with Association Classes get ArchGenXML).

Our first thought was to return a tuple of the title, description, and url of the referred item. Well, that turned out to be annoying because:
[div content="python:item[1]"]
is not as much fun as:
[div content="item/description"]
Also, for the graphic designers, having them start doing 'python:' everywhere just seemed like fighting a battle we didn't need to fight.

Then we decided to return a dictionary containing title, url, and description. And in some cases, acronym, image, and more. Now we have:
[div content="item/description"]
Hooray! Except that there are about 10+ and growing examples now of times when we missed something that needed to be returned.

So it hit me, why not just return the darn catalog brain? Sure, reference_catalog doesn't return a usable URL (you have to strip off the /at_reference/md5_hash), but I've already written an often reused python script that does this called cleanUrl. If we went this method, we get:
[div content="itemBrain/description"]
And if we hit another snag where we need to also return field x, y, and z, we have them do this:
[div define="item python:itemBrain.getObject()" content="item/funnyQuote"]

Monday, June 4, 2007

Plone Introspection

So you have a Plone content type with some fields. What are the properties of that content type? Well, you can look it up by finding in your file system the product that content type comes from, then examining the schema and finding out your information. Of course, if the product extends another product, then you have to look up that other product.


Introspection is what we really want. Python has it in spades. On pretty much any object in Python I can wrap it in dir() or help(), or check the __dict__ attribute. But you can't do this easily in Plone. Cooking up a universal instrospection view is something that ought to be done for my CMS of choice. I know there are lots of various barely documented utility methods I can find. I can wrap it up in a product and just use it by having a special introspection view that lays out some pretty HTML.


My coworker Reed pointed out that Zope 3, and the Five stuff in Zope 2 uses Interfaces that give some neat tricks you can do for introspection. I think I'll give that method a try. I think thats the right way to go because:
  1. The Zope 3 specification handles introspection much more gracefully than Zope 2. And the methods for doing it seem better documented.
  2. The technology is going that way anyhow.