[Zope3-Users] Access to request in content object and object path in doctests

Marius Gedminas mgedmin at b4net.lt
Thu Jan 25 06:20:25 EST 2007

On Thu, Jan 25, 2007 at 08:44:43AM +0100, Maciej Wisniowski wrote:
> > Yes.  You're not supposed to do that.  Views work with requests, not
> > content objects.
> >
> > If you told us what you want to achieve, we could help you find a way to
> > do it that works with Zope 3 rather than against it.
> >   
> I'am creating form (CRUD) based on formlib, but this
> is supposed to work with relational database.
> Design is simple:
> content_object -> view(based on formlib.form)
> content_object is responsible for calling sql statements
> so it needs data from the request. Possibly I have to pass
> them (or the whole request) from the view?


Formlib conveniently gives you a dict of field names mapped to field
values.  You can pass that dict to your content object.  Or you can use
Python's ** syntaxt to convert that dict to keyword arguments, if your
schema field names match argument names:

    @form.action("Save changes")
    def handle_save(self, action, data):

I would advise against passing the request to the content object.
Formlib gives you validation and data type conversion; if you use the
request directly, you'll lose that.

On Thu, Jan 25, 2007 at 09:18:22AM +0100, Maciej Wisniowski wrote:
> > <InterfaceClass zope.traversing.interfaces.IPhysicallyLocatable>)
> >
> > Ehm...? Ideas?
> OK, I forgot about:
> setup.setUpTraversal()


> Now it works with:
> >>> from zope.app.folder import rootFolder 
> >>> from zope.traversing.api import getPath
> >>> root = rootFolder()
> >>> root['test_content'] = DBCrudContent()
> >>> test_content = root['test_content']
> >>> getPath(root['test_content'])
> /test_content
> but unfortuantelly when my object is trying to get
> path to itself like:
> class DBCrudContent():
> I get error:
>     TypeError: Not enough context to determine location root

Your class does not implement IContained, and therefore when you add it
to a folder, the folder does not set its __name__ and __parent__
attributes.  At the very least you should say that it implements
ILocation, if you want to call getPath on it.

If you decide that IContained is not what you want, you'll have to set
up the __name__ and __parent__ attributes by yourself

> >>> context = DBCrudContent()
> >>> context.__parent__ = rootFolder()
> >>> context.__name__ = 'mycontent'
> >>> context.my_location()
> '/mycontent'

Marius Gedminas
We did it for smallpox, we'll also win over on ISO 8859-1 ... ;-)
	-- Markus Kuhn after eradicating one more ISO 8859-1 file from his disk
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.zope.org/pipermail/zope3-users/attachments/20070125/ec2b9ac9/attachment.bin

More information about the Zope3-users mailing list