I've been checking the documentation example about how to manioulate
images in GAE, and I decided to translate the sample from webapp to
webpy  ( the sample is here: 
http://code.google.com/appengine/docs/images/usingimages.html
).

The problem is that I cannot find the way to display properly the
images stored in the datastore.
The weapp sample creates a handler for displaying the image as
follows:

class Image (webapp.RequestHandler):
  def get(self):
    greeting = db.get(self.request.get("img_id"))
    if greeting.avatar:
      self.response.headers['Content-Type'] = "image/png"
      self.response.out.write(greeting.avatar)
    else:
      self.error(404)

And then, in the main handler, the images are displayed as follows
(simplified):

...
for greeting in greetings:
      self.response.out.write("<div><img src='img?img_id=%s'></img>" %
                              greeting.key())
      self.response.out.write(' %s</div>' %
                              cgi.escape(greeting.content))
...

Problems:
webapp outputs resullts with "self.response.out.write" which is
equivalent to a "print" statement, while webpy does it with "return".
I tried adding strings to a list and then return all as
''.join(mylist), as usual, but the image is not displayed.
I also tried requesting directly the Image handler and it doesn't
work.
I also tried using "yield" instead, with same result.

I believe that it has to do with mixing headers ("text/html" in main
handler and "image/png" in image handler) but I'm not sure.

I also tried a simple output of the image with an "image/png" header
and it doesn't work...

My translated script is this (using webpy version 0.3):

import web
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.api import images
app = web.auto_application()

class Greeting(db.Model):
  author = db.UserProperty()
  content = db.StringProperty(multiline=True)
  avatar = db.BlobProperty()
  date = db.DateTimeProperty(auto_now_add=True)

class index(app.page):
  path = '/'
  def GET(self):
    web.header('Content-Type', "text/html")
    greetings = db.GqlQuery ("SELECT * FROM Greeting ORDER BY date
DESC LIMIT 10")

    s = []
    for greeting in greetings:
      if greeting.author:
        s.append('<b>%s</b> wrote:' % greeting.author.nickname())
      else:
        s.append('An anonymous person wrote:')
      s.append("<div><img src='img?img_id=%s'></img>" %greeting.key())
      s.append('%s</div>' %greeting.content)

    s.append("""
          <form action="/sign" enctype="multipart/form-data"
method="POST">
            <div><label>Message:</label></div>
            <div><textarea name="content" rows="3" cols="60"></
textarea></div>
            <div><label>Avatar:</label></div>
            <div><input type="file" name="img"/></div>
            <div><input type="submit" value="Sign Guestbook"></div>
          </form>
        </body>
      </html>""")
    return ''.join(s)

class img(app.page):
  def GET(self, img_id):
    greeting = db.get(img_id)
    if greeting.avatar:
      web.header('Content-Type', "image/png")
      return greeting.avatar
    else:
      return "No image"

class sign(app.page):
  def POST(self):
    i = web.input()
    greeting = Greeting()
    if users.get_current_user():
      greeting.author = users.get_current_user()
    greeting.content = i.content
    avatar = images.resize(i.img, 502, 502)
    greeting.avatar = db.Blob(avatar)
    greeting.put()
    web.seeother('/')


def main():
    app.cgirun()

if __name__=='__main__':
    main()


Any hint?
Luis
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to