Skip to content

Commit

Permalink
Merge pull request #274 from thesandlord/master
Browse files Browse the repository at this point in the history
moved image guestbook, added basic image example
  • Loading branch information
Jon Wayne Parrott committed Apr 21, 2016
2 parents 74d684f + 9d37f16 commit 77b9b97
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 18 deletions.
13 changes: 13 additions & 0 deletions appengine/images/api/README.md
Original file line number Diff line number Diff line change
@@ -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).

<!-- auto-doc-link -->
These samples are used on the following documentation page:

> https://cloud.google.com/appengine/docs/python/images/
<!-- end-auto-doc-link -->

Refer to the [App Engine Samples README](../../README.md) for information on how to run and deploy this sample.
8 changes: 8 additions & 0 deletions appengine/images/api/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
runtime: python27
api_version: 1
threadsafe: yes

handlers:

- url: .*
script: main.app
File renamed without changes.
56 changes: 56 additions & 0 deletions appengine/images/api/main.py
Original file line number Diff line number Diff line change
@@ -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]
50 changes: 50 additions & 0 deletions appengine/images/api/main_test.py
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 0 additions & 17 deletions appengine/images/app.yaml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ These samples are used on the following documentation page:
<!-- end-auto-doc-link -->

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.
8 changes: 8 additions & 0 deletions appengine/images/guestbook/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
runtime: python27
api_version: 1
threadsafe: yes

handlers:

- url: .*
script: main.app
Binary file added appengine/images/guestbook/favicon.ico
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 77b9b97

Please sign in to comment.