[Zope] Re: Returning Image Data from a Product
Terry Hancock
hancock@anansispaceworks.com
Tue, 26 Feb 2002 15:35:52 -0800
Terry Hancock wrote:
> * I want to return the PNG Image (i.e. a block of data *
> * with mime type 'image/png'), NOT an HTML image tag *
> * containing a reference to result_image . *
> Is it possible that it matters whether I'm using __getattr__ or
> a python method to return this?
OKAY. This is the critical difference -- I just found
that out by testing.
However, I still think it would be very edifying to hear
someone explain why. :) I can see that there *is* a
distinction here between the return value for the __getattr__
method and a fixed Python method, but I don't really
understand it.
To recap and (hopefully) clarify, the problem:
class MyTestClass(OFS.SimpleItem.SimpleItem):
def __getattr__(self, keystr):
"Access my data by catching the attribute lookup"
# Decode keystr and catch a URL:
try:
if keystr != 'Foo' : raise AttribError
# omitted PIL code to read and convert an image
# which is now stored in self.result_image
return self.result_image
except:
OFS.SimpleItem.SimpleItem.__getattr__(keystr)
def Foo2(self):
"Direct access by Python method"
# omitted PIL code to read and convert an image
# which is now stored in self.result_image
# (i.e. same as above)
return self.result_image
If I do this, pointing my browser at an instance, called, say
"TestObject":
http://myzope/TestObject/Foo
returns a PNG image (i.e. a block of data with type 'image/png'
which is rendered by the browser), while:
http://myzope/TestObject/Foo2
returns an HTML-coded image tag, containing
src="http://myzope/TestObject/result_image". This is
not rendered by the browser, unless I wrap it in
<html><body>...</body></html> tags.
So why is that?
Why do I care? Well, mainly because the browser will try
to cache "result_image" if it's always the same URL, whereas
the __getattr__ approach has a unique URL (in the example
it doesn't matter, but actually I want to use some coded
URL, like "s50" = "scaled to 50x50 pixels".
Thanks!
Terry
--
------------------------------------------------------
Terry Hancock
hancock@anansispaceworks.com
Anansi Spaceworks
http://www.anansispaceworks.com
P.O. Box 60583
Pasadena, CA 91116-6583
------------------------------------------------------