diff --git a/appengine/images/api/README.md b/appengine/images/api/README.md new file mode 100644 index 00000000000..1aa573887b9 --- /dev/null +++ b/appengine/images/api/README.md @@ -0,0 +1,13 @@ +## Images Guestbook Sample + +This is a sample app for Google App Engine that demonstrates the [Images Python +API](https://cloud.google.com/appengine/docs/python/images/usingimages). + + +These samples are used on the following documentation page: + +> https://cloud.google.com/appengine/docs/python/images/ + + + +Refer to the [App Engine Samples README](../../README.md) for information on how to run and deploy this sample. \ No newline at end of file diff --git a/appengine/images/api/app.yaml b/appengine/images/api/app.yaml new file mode 100644 index 00000000000..697c4e6d018 --- /dev/null +++ b/appengine/images/api/app.yaml @@ -0,0 +1,8 @@ +runtime: python27 +api_version: 1 +threadsafe: yes + +handlers: + +- url: .* + script: main.app diff --git a/appengine/images/favicon.ico b/appengine/images/api/favicon.ico similarity index 100% rename from appengine/images/favicon.ico rename to appengine/images/api/favicon.ico diff --git a/appengine/images/api/main.py b/appengine/images/api/main.py new file mode 100644 index 00000000000..fd9432addfa --- /dev/null +++ b/appengine/images/api/main.py @@ -0,0 +1,56 @@ +# Copyright 2015 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +Sample application that demonstrates how to use the App Engine Images API. + +For more information, see README.md. +""" + +# [START all] +# [START thumbnailer] +from google.appengine.api import images +from google.appengine.ext import ndb + +import webapp2 + + +class Photo(ndb.Model): + title = ndb.StringProperty() + full_size_image = ndb.BlobProperty() + + +class Thumbnailer(webapp2.RequestHandler): + def get(self): + if self.request.get("id"): + photo = Photo.get_by_id(int(self.request.get("id"))) + + if photo: + img = images.Image(photo.full_size_image) + img.resize(width=80, height=100) + img.im_feeling_lucky() + thumbnail = img.execute_transforms(output_encoding=images.JPEG) + + self.response.headers['Content-Type'] = 'image/jpeg' + self.response.out.write(thumbnail) + return + + # Either "id" wasn't provided, or there was no image with that ID + # in the datastore. + self.error(404) +# [END thumbnailer] + + +app = webapp2.WSGIApplication([('/img', Thumbnailer)], debug=True) +# [END all] diff --git a/appengine/images/api/main_test.py b/appengine/images/api/main_test.py new file mode 100644 index 00000000000..8eea85195df --- /dev/null +++ b/appengine/images/api/main_test.py @@ -0,0 +1,50 @@ +# Copyright 2015 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import main +import pytest +import mock +import webtest + + +@pytest.fixture +def app(testbed): + return webtest.TestApp(main.app) + + +def test_img(app): + with mock.patch('main.images') as mock_images: + mock_images.resize.return_value = 'asdf' + mock_images.im_feeling_lucky.return_value = 'gsdf' + photo = main.Photo( + id=234 + ) + photo.title='asdf' + photo.full_size_image=b'123' + photo.put() + print photo.key.id() + + response = app.get('/img?id=%s' % photo.key.id()) + + assert response.status_int == 200 + + +def test_img_missing(app): + # Bogus image id, should get error + app.get('/img?id=123', status=404) + + +def test_no_img_id(app): + # Bogus image id, should get error + app.get('/img', status=404) diff --git a/appengine/images/app.yaml b/appengine/images/app.yaml deleted file mode 100644 index 6033ebaf3c3..00000000000 --- a/appengine/images/app.yaml +++ /dev/null @@ -1,17 +0,0 @@ -# This file specifies your Python application's runtime configuration -# including URL routing, versions, static file uploads, etc. See -# https://developers.google.com/appengine/docs/python/config/appconfig -# for details. - -runtime: python27 -api_version: 1 -threadsafe: yes - -# Handlers define how to route requests to your application. -handlers: - -# This handler tells app engine how to route requests to a WSGI application. -# The script value is in the format . -# where is a WSGI application object. -- url: .* # This regex directs all routes to main.app - script: main.app diff --git a/appengine/images/README.md b/appengine/images/guestbook/README.md similarity index 76% rename from appengine/images/README.md rename to appengine/images/guestbook/README.md index f4164204c54..e5deab29873 100644 --- a/appengine/images/README.md +++ b/appengine/images/guestbook/README.md @@ -10,4 +10,4 @@ These samples are used on the following documentation page: -Refer to the [App Engine Samples README](../README.md) for information on how to run and deploy this sample. +Refer to the [App Engine Samples README](../../README.md) for information on how to run and deploy this sample. \ No newline at end of file diff --git a/appengine/images/guestbook/app.yaml b/appengine/images/guestbook/app.yaml new file mode 100644 index 00000000000..697c4e6d018 --- /dev/null +++ b/appengine/images/guestbook/app.yaml @@ -0,0 +1,8 @@ +runtime: python27 +api_version: 1 +threadsafe: yes + +handlers: + +- url: .* + script: main.app diff --git a/appengine/images/guestbook/favicon.ico b/appengine/images/guestbook/favicon.ico new file mode 100644 index 00000000000..23c553a2966 Binary files /dev/null and b/appengine/images/guestbook/favicon.ico differ diff --git a/appengine/images/index.yaml b/appengine/images/guestbook/index.yaml similarity index 100% rename from appengine/images/index.yaml rename to appengine/images/guestbook/index.yaml diff --git a/appengine/images/main.py b/appengine/images/guestbook/main.py similarity index 100% rename from appengine/images/main.py rename to appengine/images/guestbook/main.py diff --git a/appengine/images/main_test.py b/appengine/images/guestbook/main_test.py similarity index 100% rename from appengine/images/main_test.py rename to appengine/images/guestbook/main_test.py