Search:
 

get Object using URL

Submitted by: runyaga
Last Edited: 2001-06-30

Category: DTML

Average rating is: 0.0 out of 5 (0 ratings)

Description:
sometimes you have a url and want to get the object. and if you have an object how do you get the url?

Source (Text):
<dtml-call "REQUEST.set('obj', REQUEST.resolve_url('http://localhost:8080/QuickStart/Outline'))">

url->obj.objectValues() <dtml-var "obj.objectValues()">

or

<dtml-with "restrictedTraverse('/Control_Panel', _.None)">
  <dtml-var objectIds>
</dtml-with>

<dtml-with "restrictedTraverse('/Control_Pane1', _.getitem('standard_html_header'))">
  <dtml-var raw>
</dtml-with>

obj->url <dtml-var "obj.absolute_url()">

URL -> Physical Path - <dtml-var "obj.getPhysicalPath()">

Explanation:
resolve_url() is a method on REQUEST that takes a fully-qualified URL and resolves it to an object.

NOTE: this will traverse using virtual hosts if you are using them.

restrictedTraverse is probably the best way of getting to an object.
* 1st - calls restrictedTraverse on the Control Panel and sends a default value of None
* 2nd - intentionally calls _Pane1 and says 'return (_.getitem) the standard_html_header' object
and returns the unrendered (raw) contents of the DTML Method.

absolute_url() is a method on REQUEST to give you an object's URL.

NOTE: it will take in consideration virtual hosts.

getPhysicalPath() does not take in consideration virtual hosts and
will give you the 'physical path' into the ZODB.


src can be found in
./lib/python/ZPublisher/HTTPRequest.py
./lib/python/OFSP/Traversable.py



Comments:

dont use REQUEST.resolve_url by runyaga - 2001-06-21
Don't use REQUEST.resolve_url. It's depricated and it was pretty dumb to
begin with. Use restrictedTraverse, which is documented in the online
help system under the 'ObjectManagerItem' API.

-Michel

got this from mailing list


loop getattrs by runyaga - 2001-06-30
from string import split
uri = 'folder/subfolder/method'
obj = context
for i in split(uri, '/'): obj=getattr(obj, i)
return obj.getId() #this would be method's id