[Zope-CMF] Validation during object creation
Geoff Davis
geoff@geoffdavis.net
Mon, 8 Jul 2002 20:33:50 -0400
Thanks for the feedback, Dieter.
You're right, the basic code is pretty straightforward. The main problem I
have been having is in figuring out a good way to use a python method to add
an instance of a class.
Having to add an ExternalMethod factory to accompany every new type I create
doesn't seem very elegant. I think a better way to do this might be to
create a factory object that sits at the portal level -- call it
'addProduct' or something. Every new product would have to register with
this factory. Then to create a new product, you'd invoke something like
'myportal/some/path/here/addProduct/myProduct' or if I were lazier,
'myportal/some/path/here/addProduct?type=myProduct'
Thoughts? Is this worth doing? Or is it a big waste of time? Should I
just add the external method for every product?
The second headache (since overcome) is that I have discovered through some
experimentation that one cannot add an instance of an object with a __call__
method as an ExternalMethod -- rather, you have to have an actual function.
For instance, if you have
class foo:
def __init__(self):
pass
def __call__(self, client):
pass
x = foo()
you can't add x as an external method. The reason is that the object x
lacks the attributes func_closure, func_code, func_defaults, func_dict,
func_globals, and func_name. That's kind of lame, but I assume (hope!) it's
something that is fixed in Python 2.2.
If instead, you just do
def x():
pass
everything works as expected. An easy workaround is to do
_x = foo()
def x(self):
_x(self)
Geoff Davis
http://www.geoffdavis.net
----- Original Message -----
From: "Dieter Maurer" <dieter@handshake.de>
To: "Geoff Davis"
Cc: "Zope CMF" <zope-cmf@zope.org>
Sent: Monday, July 08, 2002 2:23 PM
Subject: Re: [Zope-CMF] Validation during object creation
> Geoff Davis writes:
> > ...
> > I have a class of objects called MyObject that I want to manage. To
create
> > such a MyObject, one invokes a python method called addMyObject.
> > addMyObject initially sets some values and passes them to a ZPT called
> > addMyObjectForm. The form submits to addMyObject. addMyObject then
> > validates the parameters -- if they are invalid, it passes the user's
> > responses back to addMyObjectForm; if they are valid, it makes the
> > appropriate call to invokeFactory and then relocates to some other
page.
> >
> > A few questions:
> >
> > 1) Before I reinvent a wheel, is there already an easy way to do this
in
> > Zope? I haven't seen anything yet
> CMFFormulator may help you with the validation.
>
> Otherwise, your approach seems to be very easy, only a few lines
> of Python code. Do not expect complex solutions for simple problems...
>
> > ...
> > I have been trying to add addMyObject as an external method, but have
been
> > encountering weird errors that I think are arising from
> > ValidatedForm.__call__ not having the right function signature. Given
that
> > ExternalMethod seems to expect a method with the signature foo(self),
I'm
> > not sure how to write an appropriate __call__ signature (__call__(self,
> > self)??). Any ideas? The specific error I'm getting is on line 143 of
> > ExternalMethod.py (in Zope 2.5.1): ff doesn't have an attribute
> > func_defaults.
> External Methods do not have a fixed signature.
>
> There is some magic, if you have a first parameter called "self".
> Under some circumstances, e.g. when called via the Web,
> the context object (in the sense of Python Scripts) is passed
automatically
> via this "self" parameter. If you want to read more, look at
> the Zope book or
>
> <http://www.dieter.handshake.de/pyprojects/zope/book/chap3.html>
>
> The error above seems to indicate, that what you think is a function
> is something different.
>
>
> Dieter
>