The following is my main.py so far.
import cgi
import datetime
import logging
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import images
logging.getLogger().setLevel(logging.DEBUG)
class Greeting(db.Model):
author = db.UserProperty()
content = db.StringProperty(multiline=True)
imageblob = blobstore.BlobReferebceProperty()
date = db.DateTimeProperty(auto_now_add=True)
class MainPage(webapp.RequestHandler):
def get(self):
self.response.out.write('<html><body>')
query_str = "SELECT * FROM Greeting ORDER BY date DESC LIMIT 10"
greetings = db.GqlQuery (query_str)
for greeting in greetings:
if greeting.author:
self.response.out.write('<b>%s</b> wrote:' % greeting.author.nickname())
else:
self.response.out.write('An anonymous person wrote:')
self.response.out.write("<div><img src='img?img_id=%s'></img>" %
greeting.key())
self.response.out.write(' %s</div>' %
cgi.escape(greeting.content))
self.response.out.write("""
<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>""")
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.response.out.write("No image")
class Guestbook(webapp.RequestHandler):
def post(self):
greeting = Greeting()
if users.get_current_user():
greeting.author = users.get_current_user()
greeting.content = self.request.get("content")
avatar = get_serving_url(self.request.get("img"), size=None, crop=False)
#avatar = images.crop(self.request.get("img"), 0.0, 0.0,1.0,0.5)
greeting.avatar = db.Blob(avatar)
greeting.put()
self.redirect('/')
application = webapp.WSGIApplication([
('/', MainPage),
('/img', Image),
('/sign', Guestbook)
], debug=True)
def main():
run_wsgi_app(application)
if __name__ == '__main__':
main()
Like it says in the docs:
In your code you seem to be using
get_serving_url()
to construct aBlob
. This will not work, sinceget_serving_url()
returns a URL, not the image data.You also seem to be confusing a
blobstore.BlobReferenceProperty
and adb.BlobProperty
-- aBlobReferenceProperty
references an object uploaded and stored in the blobstore while aBlobProperty
stores blob data directly in the datastore.A better solution to your problem would be to store the image you get in the request (as a
BlobProperty
) in your model, then serve it at different sizes usingget_serving_url()
.Alternatively, if you expect to be storing large images, store the images in the blobstore and use the
BlobReferenceProperty
-- either way, you should only be usingget_serving_url()
to serve the image, not in storing it.