0

How can I get object from ZODB database in Zope3 project by url 'http://ecample.com/folder1/object1'?

obj1 = someMethod('http://ecample.com/folder1/object1')

Is there any tools of methods for this? Like absoluteUrl() but opposite? Or I must parse url and manually get object from db root?...Thanks

Gleb
  • 731
  • 1
  • 8
  • 14
  • How are you using the ZODB? In your own application, or do you have a HTTP publisher (Zope, Bluebream, Grok, whatever)? The ZODB itself knows *nothing* about HTTP. – Martijn Pieters Jan 31 '13 at 14:52
  • I'm using Zope 3 for HTTP publishing – Gleb Jan 31 '13 at 14:57
  • The Zope 3 publisher takes care of object publishing. What is your specific problem? – Martijn Pieters Jan 31 '13 at 14:58
  • I have some url, and in python code I want to "take" that object by that url for further logic. – Gleb Jan 31 '13 at 15:01
  • From outside of the Zope3 app or from inside? You need to perform the same traverse as the publisher executes, Zope 3 has interfaces for that. Note that this has *nothing* to do with the ZODB; it would be the same for other objects published by Zope3. – Martijn Pieters Jan 31 '13 at 15:04
  • from inside of my Zope3 app) – Gleb Jan 31 '13 at 15:05
  • Note that the term Zope 3 these days refers to the component-architecture-based family of libraries, and the original Zope 3 server has been renamed to Bluebream. I suspect you are using the original Zope 3 server here, so I'll answer accordingly. – Martijn Pieters Jan 31 '13 at 15:23
  • So, are you saying that there are no such magic function like 'absoluteUrl'?... And can you say more about that interfaces, or give some links please. And you can post your answer in answers section) Yes, project is quite old, so it's zope3) – Gleb Jan 31 '13 at 15:23
  • Of course there are, but I need to be certain about *what* API to tell you about, because 'Zope 3' can refer to different things in different contexts. I am also pointing out that it doesn't matter that your objects are stored in the ZODB. – Martijn Pieters Jan 31 '13 at 15:26

1 Answers1

2

You can turn a path into an object by using the traversing API:

from zope.traversing.api import traverse

obj = traverse(context, path)

You'll need a context to traverse from; use the site root for URL paths for example. If all you have is a URL, you'd need to parse out the path from it:

from urlparse import urlparse

path = urlparse(url).path
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343