From 9d37f1603a0b32622c478ad523fef03ea2dd244e Mon Sep 17 00:00:00 2001 From: Sandeep Dinesh Date: Thu, 21 Apr 2016 14:58:51 -0700 Subject: [PATCH] moved image guestbook, added basic image example --- appengine/images/api/README.md | 13 ++++ appengine/images/api/app.yaml | 8 +++ appengine/images/{ => api}/favicon.ico | Bin appengine/images/api/main.py | 56 ++++++++++++++++++ appengine/images/api/main_test.py | 50 ++++++++++++++++ appengine/images/app.yaml | 17 ------ appengine/images/{ => guestbook}/README.md | 2 +- appengine/images/guestbook/app.yaml | 8 +++ appengine/images/guestbook/favicon.ico | Bin 0 -> 8348 bytes appengine/images/{ => guestbook}/index.yaml | 0 appengine/images/{ => guestbook}/main.py | 0 appengine/images/{ => guestbook}/main_test.py | 0 12 files changed, 136 insertions(+), 18 deletions(-) create mode 100644 appengine/images/api/README.md create mode 100644 appengine/images/api/app.yaml rename appengine/images/{ => api}/favicon.ico (100%) create mode 100644 appengine/images/api/main.py create mode 100644 appengine/images/api/main_test.py delete mode 100644 appengine/images/app.yaml rename appengine/images/{ => guestbook}/README.md (76%) create mode 100644 appengine/images/guestbook/app.yaml create mode 100644 appengine/images/guestbook/favicon.ico rename appengine/images/{ => guestbook}/index.yaml (100%) rename appengine/images/{ => guestbook}/main.py (100%) rename appengine/images/{ => guestbook}/main_test.py (100%) 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 0000000000000000000000000000000000000000..23c553a2966ca4cecf146093f33d8114b4f1e368 GIT binary patch literal 8348 zcmeI0du&tJ8Ng3Vwv|ewX4Hu@my&|vCD%DN@CLnxyptBT_%HjatoYr7%N3zGPed#@ckvA><;HWecDU4&2}*2(h%gm&WgCftWj zkQfW;&mXr@0S&(csd+cjaImbXSy=NC<10}z(efGwgi=;B$dt=HkKYCvp-#3KX+qIu zxx$>zcwk+N=c%1^J9)sO44lmS0##~wzU(43f>wW-p_YXwj>s>3{gHI*^oo1lmJ!X*bw{3UBAKRut zUh5Uy4_94IIkdxC^v{_g%FjuEI+f&;9KQ3i+nK7pU_RivFjP_DTl-&gP_qr$nD=M{ z@z;C3#y5Jsh77svGQ4u$ZX)t}=e52{#Xvk;4qIxNh86kR$OhCAie%&e`U{r{ACQZ@ z-)1#sItef{2L za&U;nm+_lo@lbR7vat&^EcECsH)uSnhlht@etsV4LE5B`j#GW*iur~}xpYlTtc;FC{nl|LK^miZ0y3WU>MIM zmc}94VzEFx9?#&?4l+h;ggzvOeCI$ob=`tBp*&EtirH2xU z4a#KB)IMa{_YdYrnxq>-DrrQ>rfpYe8@|Nc-oHnG*L(HR$}d4Eo2&X@o1~9l@%@W) zU{%rv$`tBAOHJH+?zcv7`!T~x;=<#7@4R5T^5$-%Pz-NAYt+5{d;>R+9W24y&`;ZkZqFGQ6_)<35Z_$5V#ga#=N9985-M0KR*fl@h4M0BxWvbYQr6t zULtYBAMJ%iU>x|?U8z_Zyv1jg_VcZ^kO)pd_)jk`_~2MHZmybbloW?l)lnMrb~TAX zV&%#e+VLvM4!%js+%B6}N!=udFlN5Jv;yQm0sdW({6ny+{{$_b`%pI&q3%#p3R?q( z%3@zpafPo4-GA}ErIfU@j?gpfdgeyI);;S-otz(GefQbXvG3J6hbwD_0*YOuqb1V8 zpQm{(oT|g$!tcw;Ck6l>=mo$z0J@0f0^U0{x?+mD8}QB{9yV7qQ;&$5^%*fb@qUB& zX(O<{D-aZ493K;1xH%!}9+}vt8Sshs4os9)d132glTa=lIJv}MJyU_Y!ZFl62j9@l zggg2y{y~c&Vm0cMa@}r@bbn?HR4Sa|66p;nlMl_6D%_%EjCNRq)NBvx!R!t`@zUoW zKV#O#EhZy4>~?VU+rfg@_W_4Kt~zS<|3JjVMcZ#exy;pDUypq|xjpD&0#H{BHgrvM zI=z9nnTN~NCX?mf@DOU;!82-#gXGsw5CSSf22lJ>u8duE&igGuZq4lVZf*LH2%QqOqkv@O{w`Y}q~yWm0EP zeSP~Hau49ZBU@&hWwH5YG0dnGVN1^iztQFh>rIvj5$iQ;q^nyuSuXo`U~{E8DpuIS zA{kR5oC9o=_>jhfRX@QTs1KRm{@F31 zFKP1!w?51@R^I~kA%H*B0W^sKnyVIsv`_2;=zbUGR9pOTqUhV{{^UH=RQ2@S?`uaQ zEy`)Gsd}1IEedW&4lAe0Sg5h`nQXqaZ}RyEzX{FT?hmF3>0_QQT1V~jI$wc&1@aZh ZS0G=3d706zJ{{SaIO|Jj| literal 0 HcmV?d00001 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