Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added blobstore image example #291

Merged
merged 1 commit into from
Apr 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions appengine/images/api/blobstore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 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 blobstore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blobstore should be above images, lint will fail otherwise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lint passes for me?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm dumb and mis-read ext and api. This is fine.


import webapp2


class Thumbnailer(webapp2.RequestHandler):
def get(self):
blob_key = self.request.get("blob_key")
if blob_key:
blob_info = blobstore.get(blob_key)

if blob_info:
img = images.Image(blob_key=blob_key)
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 "blob_key" wasn't provided, or there was no value with that ID
# in the Blobstore.
self.error(404)
# [END thumbnailer]


app = webapp2.WSGIApplication([('/img', Thumbnailer)], debug=True)
# [END all]
45 changes: 45 additions & 0 deletions appengine/images/api/blobstore_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 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 blobstore
import mock
import pytest
import webtest


@pytest.fixture
def app(testbed):
return webtest.TestApp(blobstore.app)


def test_img(app):
with mock.patch('blobstore.images') as mock_images:
with mock.patch('blobstore.blobstore') as mock_blobstore:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make these decorators? less nesting so a little cleaner imo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do this after closing the bug

mock_blobstore.get.return_value = b'123'
mock_images.resize.return_value = 'asdf'
mock_images.im_feeling_lucky.return_value = 'gsdf'

response = app.get('/img?blob_key=123')

assert response.status_int == 200


def test_img_missing(app):
# Bogus blob_key, should get error
app.get('/img?blob_key=123', status=404)


def test_no_img_id(app):
# No blob_key, should get error
app.get('/img', status=404)
9 changes: 4 additions & 5 deletions appengine/images/api/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# limitations under the License.

import main
import pytest
import mock
import pytest
import webtest


Expand All @@ -30,10 +30,9 @@ def test_img(app):
photo = main.Photo(
id=234
)
photo.title='asdf'
photo.full_size_image=b'123'
photo.title = 'asdf'
photo.full_size_image = b'123'
photo.put()
print photo.key.id()

response = app.get('/img?id=%s' % photo.key.id())

Expand All @@ -46,5 +45,5 @@ def test_img_missing(app):


def test_no_img_id(app):
# Bogus image id, should get error
# No image id, should get error
app.get('/img', status=404)