Hi! Up to now I server some files in my Python Product with a URL like .../download?file=foo.doc. It would be much better if the URL would be .../foo.doc Does someone know how this could be done? The files are from the filesystem of the server. They are not in the ZODB. thomas -- Thomas Guettler <guettli@thomas-guettler.de> http://www.thomas-guettler.de
Thomas Guettler writes:
Up to now I server some files in my Python Product with a URL like .../download?file=foo.doc.
It would be much better if the URL would be .../foo.doc
Does someone know how this could be done?
The files are from the filesystem of the server. They are not in the ZODB. You give your product a "__getitem__(self,key)" method. It is called by ZPublisher when an attribute lookup failed, "key" will be the next URL segment, "foo.doc" in your example.
Dieter
On Sat, Nov 23, 2002 at 09:07:32PM +0100, Dieter Maurer wrote:
Thomas Guettler writes:
Up to now I server some files in my Python Product with a URL like .../download?file=foo.doc.
It would be much better if the URL would be .../foo.doc
Does someone know how this could be done?
The files are from the filesystem of the server. They are not in the ZODB. You give your product a "__getitem__(self,key)" method. It is called by ZPublisher when an attribute lookup failed, "key" will be the next URL segment, "foo.doc" in your example.
Thank you for this hint! It works 95%. Since no REQUEST is given to __getitem__ I get it like this: if hasattr(self, 'REQUEST'): request=self.REQUEST This or something differnt seems to change the result of getSecurityManager().getUser(): I always get the anonymous user. Does someone know why getUser() in __getitem__ is diffent than in other methods? The next thing is working, but it is not nice: the call to __getitem__() returns the content of a file to the browser. But Zope misses the docstring of the string. I solved by adding the content to the request object and returning a function which returns this, but a nicer solution would be better: request._return_file=self.download(key, REQUEST=request) def return_wrapper_with_docstring(REQUEST): "docstring" return REQUEST._return_file return return_wrapper_with_docstring -- Thomas Guettler <guettli@thomas-guettler.de> http://www.thomas-guettler.de
Thomas Guettler writes:
... calling "__getitem__" during URL traversal ... It works 95%. Since no REQUEST is given to __getitem__ I get it like this:
if hasattr(self, 'REQUEST'): request=self.REQUEST
This or something differnt No, it does not -- see below. seems to change the result of getSecurityManager().getUser(): I always get the anonymous user. This is because authentication is done at the end of traversal.
When you use "__getitem__" during traversal, the user is not yet determined.
Does someone know why getUser() in __getitem__ is diffent than in other methods? It is not. Any method used during traversal will behave identically in this respect.
The next thing is working, but it is not nice: the call to __getitem__() returns the content of a file to the browser. But Zope misses the docstring of the string. I solved by adding the content to the request object and returning a function which returns this, but a nicer solution would be better: You can solve both problems by returning a wrapper object for your file.
Ensure, that your wrapper class inherits from "Acquisition.Explicit/Implicit" and that you wrap it in the objects context. Give its "__call__" method a docstring and let it render the file content. Protect it as you find useful. This way Zopes security mechanism ensures authorized access only. You can also replace "__getitem__" by "__bobo_traverse__" (when you like). Dieter
The next thing is working, but it is not nice: the call to __getitem__() returns the content of a file to the browser. But Zope misses the docstring of the string. I solved by adding the content to the request object and returning a function which returns this, but a nicer solution would be better:
request._return_file=self.download(key, REQUEST=request) def return_wrapper_with_docstring(REQUEST): "docstring" return REQUEST._return_file return return_wrapper_with_docstring
I found a better solution, maybe someone likes urls like this "..../foo.myextension", too:
def __getitem__(self, key): print "Document.__getitem__: key=%s" % ( key) if hasattr(self, 'REQUEST'): request=self.REQUEST else: request=None ... request["file"]=key return self.download
def download(self, file, REQUEST=None): <<< This way /foo.txt will be wrapped to /download?file=foo.txt thomas -- Thomas Guettler <guettli@thomas-guettler.de> http://www.thomas-guettler.de
participants (2)
-
Dieter Maurer -
Thomas Guettler