[z3-five] Re: Providing get_size() for the ZMI

Philipp von Weitershausen philipp at weitershausen.de
Sun Apr 3 23:40:49 MEST 2005


Andreas Jung wrote:
> How would I implement a get_size() method so that the ZMI can display
> the size for an object? I know that you have to provide an adapter
> implementing ISized in Z3 but how can I do this in Five (other than
> implementing get_size() as method of the content class)?

The ISized adapter is a typical case of a framework-compliance adapter. 
In Zope 3, there is the zope.app.size framework that has a notion of 
sizable things and objects can communicate with that framework by being 
adaptable to the framework's interface, ISize. The file representation 
framework is another good example of this.

As you can see, the problem lies in the framework querying the adapter, 
not in your content class. Zope 2 doesn't have the ISized framework, so 
there will be nothing that will be adapting your content object to ISized.


Fortunately, the Five world doesn't have to be painted all that black. 
In two places we already use structured monkey patching to do those 
things that Zope 2 uses methods for (for example manage_afterAdd) in 
Zope 3 style (in the aforementioned case we send an IObjectAddedEvent).

So, something worthwile adding to Five would be a structural monkey 
patch that adds to a certain class a get_size() method that would do this:

   def get_size(self):
       size = ISized(self, None)
       if size is not None:
           return size.getSizeForSorting()[1]

This get_size method could be turned on for classes through a ZCML 
directive, e.g.

   <five:sizable class="Products.MyProduct.MyContentClass" />

If you want to implement such a feature (not too hard), you should look 
at the implementation of <five:sendEvents /> which provides classes with 
a manage_afterAdd, manage_afterClone etc. that send Z3-style events.

Basically, you'd need to

   - write a dummy ExtensionClass with the above cited get_size() method

   - write a directive handler for the <five:sizable /> directive. It 
would take a class_ as an argument and monkey patch the new get_size 
method in. Use the implementation of the sendEvents handler as an example.

   - in meta.zcml, register the directive handler using a directive 
schema (you can use fivedirectives.ISendEvents for the schema, since the 
parameters are the same, it's just class_ which is a Python object with 
a dotted name).

Lemme know if you'd like to contribute this little piece of code (incl. 
a test, of course), then I'll get you an svn account... :)

Philipp



More information about the z3-five mailing list