Skip to content

Commit b3df05c

Browse files
author
Jon Wayne Parrott
committed
Moving blobstore sample
1 parent fbb6e6f commit b3df05c

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

appengine/blobstore/__init__.py

Whitespace-only changes.

appengine/blobstore/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+
- url: .*
7+
script: main.app
8+
login: required

appengine/blobstore/main.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
# [START all]
16+
from google.appengine.api import users
17+
from google.appengine.ext import blobstore
18+
from google.appengine.ext import ndb
19+
from google.appengine.ext.webapp import blobstore_handlers
20+
import webapp2
21+
22+
23+
# This datastore model keeps track of which users uploaded which photos.
24+
class UserPhoto(ndb.Model):
25+
user = ndb.StringProperty()
26+
blob_key = ndb.BlobKeyProperty()
27+
28+
29+
class PhotoUploadFormHandler(webapp2.RequestHandler):
30+
def get(self):
31+
# [START upload_url]
32+
upload_url = blobstore.create_upload_url('/upload_photo')
33+
# [END upload_url]
34+
# [START upload_form]
35+
# To upload files to the blobstore, the request method must be "POST"
36+
# and enctype must be set to "multipart/form-data".
37+
self.response.out.write("""
38+
<html><body>
39+
<form action="{0}" method="POST" enctype="multipart/form-data">
40+
Upload File: <input type="file" name="file"><br>
41+
<input type="submit" name="submit" value="Submit">
42+
</form>
43+
</body></html>""".format(upload_url))
44+
# [END upload_form]
45+
46+
47+
# [START upload_handler]
48+
class PhotoUploadHandler(blobstore_handlers.BlobstoreUploadHandler):
49+
def post(self):
50+
try:
51+
upload = self.get_uploads()[0]
52+
user_photo = UserPhoto(
53+
user=users.get_current_user().user_id(),
54+
blob_key=upload.key())
55+
user_photo.put()
56+
57+
self.redirect('/view_photo/%s' % upload.key())
58+
59+
except:
60+
self.error(500)
61+
# [END upload_handler]
62+
63+
64+
# [START download_handler]
65+
class ViewPhotoHandler(blobstore_handlers.BlobstoreDownloadHandler):
66+
def get(self, photo_key):
67+
if not blobstore.get(photo_key):
68+
self.error(404)
69+
else:
70+
self.send_blob(photo_key)
71+
# [END download_handler]
72+
73+
74+
app = webapp2.WSGIApplication([
75+
('/', PhotoUploadFormHandler),
76+
('/upload_photo', PhotoUploadHandler),
77+
('/view_photo/([^/]+)?', ViewPhotoHandler),
78+
], debug=True)
79+
# [END all]

appengine/blobstore/main_test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 tests
16+
import webtest
17+
18+
from . import main
19+
20+
21+
class TestBlobstoreSample(tests.AppEngineTestbedCase):
22+
23+
def setUp(self):
24+
super(TestBlobstoreSample, self).setUp()
25+
self.app = webtest.TestApp(main.app)
26+
27+
def test_form(self):
28+
self.loginUser()
29+
response = self.app.get('/')
30+
31+
self.assertTrue('/_ah/upload' in response)

tests/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ def setUp(self):
8888
self.testbed.init_memcache_stub()
8989

9090
# Setup remaining stubs.
91+
self.testbed.init_blobstore_stub()
9192
self.testbed.init_user_stub()
9293
self.testbed.init_taskqueue_stub(root_path='tests/resources')
9394
self.taskqueue_stub = self.testbed.get_stub(

0 commit comments

Comments
 (0)