Skip to content

Commit 9d37f16

Browse files
committed
moved image guestbook, added basic image example
1 parent fddf5e6 commit 9d37f16

File tree

12 files changed

+136
-18
lines changed

12 files changed

+136
-18
lines changed

appengine/images/api/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Images Guestbook Sample
2+
3+
This is a sample app for Google App Engine that demonstrates the [Images Python
4+
API](https://cloud.google.com/appengine/docs/python/images/usingimages).
5+
6+
<!-- auto-doc-link -->
7+
These samples are used on the following documentation page:
8+
9+
> https://cloud.google.com/appengine/docs/python/images/
10+
11+
<!-- end-auto-doc-link -->
12+
13+
Refer to the [App Engine Samples README](../../README.md) for information on how to run and deploy this sample.

appengine/images/api/app.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
runtime: python27
2+
api_version: 1
3+
threadsafe: yes
4+
5+
handlers:
6+
7+
- url: .*
8+
script: main.app
File renamed without changes.

appengine/images/api/main.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""
16+
Sample application that demonstrates how to use the App Engine Images API.
17+
18+
For more information, see README.md.
19+
"""
20+
21+
# [START all]
22+
# [START thumbnailer]
23+
from google.appengine.api import images
24+
from google.appengine.ext import ndb
25+
26+
import webapp2
27+
28+
29+
class Photo(ndb.Model):
30+
title = ndb.StringProperty()
31+
full_size_image = ndb.BlobProperty()
32+
33+
34+
class Thumbnailer(webapp2.RequestHandler):
35+
def get(self):
36+
if self.request.get("id"):
37+
photo = Photo.get_by_id(int(self.request.get("id")))
38+
39+
if photo:
40+
img = images.Image(photo.full_size_image)
41+
img.resize(width=80, height=100)
42+
img.im_feeling_lucky()
43+
thumbnail = img.execute_transforms(output_encoding=images.JPEG)
44+
45+
self.response.headers['Content-Type'] = 'image/jpeg'
46+
self.response.out.write(thumbnail)
47+
return
48+
49+
# Either "id" wasn't provided, or there was no image with that ID
50+
# in the datastore.
51+
self.error(404)
52+
# [END thumbnailer]
53+
54+
55+
app = webapp2.WSGIApplication([('/img', Thumbnailer)], debug=True)
56+
# [END all]

appengine/images/api/main_test.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import main
16+
import pytest
17+
import mock
18+
import webtest
19+
20+
21+
@pytest.fixture
22+
def app(testbed):
23+
return webtest.TestApp(main.app)
24+
25+
26+
def test_img(app):
27+
with mock.patch('main.images') as mock_images:
28+
mock_images.resize.return_value = 'asdf'
29+
mock_images.im_feeling_lucky.return_value = 'gsdf'
30+
photo = main.Photo(
31+
id=234
32+
)
33+
photo.title='asdf'
34+
photo.full_size_image=b'123'
35+
photo.put()
36+
print photo.key.id()
37+
38+
response = app.get('/img?id=%s' % photo.key.id())
39+
40+
assert response.status_int == 200
41+
42+
43+
def test_img_missing(app):
44+
# Bogus image id, should get error
45+
app.get('/img?id=123', status=404)
46+
47+
48+
def test_no_img_id(app):
49+
# Bogus image id, should get error
50+
app.get('/img', status=404)

appengine/images/app.yaml

Lines changed: 0 additions & 17 deletions
This file was deleted.

appengine/images/README.md renamed to appengine/images/guestbook/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ These samples are used on the following documentation page:
1010
1111
<!-- end-auto-doc-link -->
1212

13-
Refer to the [App Engine Samples README](../README.md) for information on how to run and deploy this sample.
13+
Refer to the [App Engine Samples README](../../README.md) for information on how to run and deploy this sample.

appengine/images/guestbook/app.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
runtime: python27
2+
api_version: 1
3+
threadsafe: yes
4+
5+
handlers:
6+
7+
- url: .*
8+
script: main.app
8.15 KB
Binary file not shown.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)