[Zope] Pluggable brains?
Martijn Pieters
mj@antraciet.nl
Mon, 05 Jul 1999 13:07:39 +0200
At 02:28 05/07/99 , Alexander Staubo wrote:
>Uh, so "pluggable brains" _is_ the ability for Zope to return records as
>instances of a user-definable class?
>
>It may be a vivid term, but descriptive it ain't. :-)
>
>I have a related question. I need to post-process a query result set
>using Python; I simply want to (a) go through each record and add (or
>set) a column, and (b) reorder the records. Can you give me a simple
>example of how to do this? As far as I can see, records returned by SQL
>Methods are instances of a special class "r" which descends from Record,
>Acquisition.Implicit and an optional user-defined class (the "brain", I
>guess). It seems that when such an instance is created, each column is
>set as an attribute on the instance. If so, I guess I can simply put the
>records into a list, add my column as a vanilla Python attribute, and
>reorder them as I wish, and then return them for use in DTML? Correct?
Indeed, just create the 'Brain' (Igor, is that lightning storm here yet?),
and have it set the attribute. The reordering is trickier. You could use
the sort attribute of the #in tag. If the order cannot be derived from any
existing attributes, you just add one to the brain object (Igor, tweezers
please), that, if sorted on, will create the right order.
Just put a module in the Extensions directory of your Zope installation,
let call it MyBrains.py. In it you define your class:
class SmartRecord:
static_attr = 'A Static Attribute'
def dyn_attr:
return 'A Dynamic Attribute'
And on the 'Advanced' tab of your Z SQL Method you specify this class and
the file it is in ('SmartRecord' and 'MyBrains', or 'MyBrains.py'). From
now on, you can refer to static_attr and dyn_attr as if they were columns
of the result set.
PS. Above code is untested, YMMV
--
Martijn Pieters, Web Developer
| Antraciet http://www.antraciet.nl
| Tel: +31-35-7502100 Fax: +31-35-7502111
| mailto:mj@antraciet.nl http://www.antraciet.nl/~mj
| PGP: http://wwwkeys.nl.pgp.net:11371/pks/lookup?op=get&search=0xA8A32149
------------------------------------------