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

David Johnson djohnson at jsatech.com
Fri Jan 26 07:35:20 EST 2007

I do lots of database stuff, like what you're describing.   I got  
lost in the threads here, but my general advice is the following:

1. Do not mix content with views, this destroys the scalability  
aspects of Zope and will hurt you later.  When ever you find Zope  
frustrating it often because you're trying to do something that is  
not in your best interests.

2. Generally you will want to create content with methods that can  
perform the database operations you would like.  For example, I  
recently created a Container/Folder that contains object from a  
database.  The container is persistent.  The container however loads  
the content from an SQL database. I can then edit and view the  
objects from the SQL database like ZODB objects.  I have some code  
that I can share here.

In another example, which may suite your needs, I created a Zope site  
in which customers can lookup orders by an order id.  My content  
object is effectively a class "Order", which has a method,  
"getOrderById", and that takes an order id as the parameter.   
getOrderById() queries the database and returns the order as a  
dictionary.  My view is a page that simply calls  
self.context.getOrderById() with the orderid as the first parameter.   
The advantage of this approach is that you can de-couple the database  
from the view and content objects.  You can the ability to do lots of  
other cool stuff as well, like create XML-RPC views without having to  
do much extra coding (because all the code is hidden in your content  

3. There are times when your content and views will need to mesh a  
bit more tightly.  In this case, you can create views on your context  
object.  I'm not very good with zope terminology at times, but this  
view is a different zope:view than the browser:view.  For example, I  
have a shopping cart application and the cart of course is different  
for each client.  The cart also resides in an SQL database.  So I  
have a content type: Cart, which has a view, that accepts a request,  
and can effectively remold the Cart content type into a specific cart  
for a specific client.  The client will after all need to manipulate  
the cart to add, delete, and edit items.

On Jan 24, 2007, at 11:50 PM, Maciej Wisniowski wrote:

> Hi
> Two questions...
> Is this possible to get
> request object in content class. In Zope2 this
> was possible with simple self.REQUEST. In Zope3
> I tried self.request but I only get errors.
> Maybe this is a feature, and I'm not supposed
> to access request object from content class?
> Another question. I'm trying to write tests
> for my content object.
> Because I want some caching facilities
> I'm using getLocationForCache from
> zope.app.cache.caching.
> and because of this: zapi.getPath(obj)
> In short it is like:
> class MyClass(Persistent):
>     (...)
>     def myfunction(self):
>         location = zapi.getPath(self)
>         return location
> In the tests I always get location == None.
> I'm using doctests like:
>>>> test_content = MyClass()
>>>> test_content.myfunction()
> placefulsetup adds some objects like root etc.
> but I'm not sure how I should add my object to this.
> Any clues?
> -- 
> Maciej Wisniowski
> _______________________________________________
> Zope3-users mailing list
> Zope3-users at zope.org
> http://mail.zope.org/mailman/listinfo/zope3-users

More information about the Zope3-users mailing list