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

2007-01-26 Thread David Johnson
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  
object).


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@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users



___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


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

2007-01-25 Thread Maciej Wisniowski

 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():
def myfunction(self):
getPath(self)

 test_content.myfunction()

I get error:

File dbcrudbase.txt, line 86, in dbcrudbase.txt
Failed example:
test_content.myfunction()
Exception raised:
Traceback (most recent call last):
  File
/home/downloads/Zope/Zope-3.3.0/build/lib.linux-i686-2.4/zope/testing/doctest.py,
line 1256, in __run
compileflags, 1) in test.globs
  File doctest dbcrudbase.txt[17], line 1, in ?
test_content.myfunction()
  File doctest dbcrudbase.txt[7], line 18, in myfunction
getPath(self)
  File
/home/downloads/Zope/Zope-3.3.0/build/lib.linux-i686-2.4/zope/traversing/api.py,
line 62, in getPath
return IPhysicallyLocatable(obj).getPath()
  File
/home/downloads/Zope/Zope-3.3.0/build/lib.linux-i686-2.4/zope/location/traversing.py,
line 154, in getPath
raise TypeError(Not enough context to determine location root)
TypeError: Not enough context to determine location root

-- 
Maciej Wisniowski
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


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

2007-01-25 Thread Maciej Wisniowski

 line 154, in getPath
 raise TypeError(Not enough context to determine location root)
 TypeError: Not enough context to determine location root
I've added Contained as the base class of my content
and it works now... uff...

-- 
Maciej Wisniowski
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


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

2007-01-24 Thread Marius Gedminas
On Wed, Jan 24, 2007 at 11:50:21PM +0100, Maciej Wisniowski wrote:
 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?

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.

 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?

Lately I've been using this pattern in my tests:

#!/usr/bin/env python
import unittest

from zope.testing import doctest
from zope.app.testing import setup
from zope.app.folder import rootFolder
from zope.traversing.api import getPath

from mypackage import MyObject


def doctest_something():
Bla bla bla

 root = rootFolder()
 root['my_object'] = my_object = MyObject()
 getPath(my_object)
'/my_object'




def setUp(test):
setup.placelessSetUp()
setup.setUpTraversal()


def tearDown(test):
setup.placelessTearDown()


def test_suite():
return doctest.DocTestSuite(setUp=setUp, tearDown=tearDown)


if __name__ == '__main__':
unittest.main(defaultTest='test_suite'))


I avoid setup.placefulSetUp, and I avoid zapi/ztapi.

Cheers,
Marius Gedminas
-- 
System going down at 5 this afternoon to install scheduler bug.


signature.asc
Description: Digital signature
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users