Skip to content

Commit 6718a7e

Browse files
committed
Merge branch 'master' into modules
2 parents 467a64e + 5fd8683 commit 6718a7e

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-0
lines changed

appengine/ndb/entities/snippets.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,12 @@ def construct_keys_from_range_of_reserved_ids(first, last):
283283
def reserve_model_ids_up_to(N):
284284
first, last = MyModel.allocate_ids(max=N)
285285
return first, last
286+
287+
288+
class ModelWithUser(ndb.Model):
289+
user_id = ndb.StringProperty()
290+
color = ndb.StringProperty()
291+
292+
@classmethod
293+
def get_by_user(cls, user):
294+
return cls.query().filter(cls.user_id == user.user_id()).get()

appengine/ndb/entities/snippets_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import inspect
1616

17+
from google.appengine.api import users
1718
from google.appengine.ext import ndb
1819
from google.appengine.ext.ndb.google_imports import datastore_errors
1920
import pytest
@@ -227,3 +228,10 @@ def test_construct_keys_from_range_of_reserved_ids(client):
227228
def test_reserve_model_ids_up_to(client):
228229
first, last = snippets.reserve_model_ids_up_to(5)
229230
assert last - first >= 4
231+
232+
233+
def test_model_with_user(client):
234+
user = users.User(email='[email protected]', _user_id='123')
235+
item = snippets.ModelWithUser(user_id=user.user_id())
236+
item.put()
237+
assert snippets.ModelWithUser.get_by_user(user) == item

appengine/users/app.yaml

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

appengine/users/main.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright 2016 Google Inc.
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 Google App Engine application that demonstrates using the Users API
17+
18+
For more information about App Engine, see README.md under /appengine.
19+
"""
20+
21+
# [START all]
22+
23+
from google.appengine.api import users
24+
import webapp2
25+
26+
27+
class MainPage(webapp2.RequestHandler):
28+
def get(self):
29+
user = users.get_current_user()
30+
if user:
31+
nickname = user.nickname()
32+
logout_url = users.create_logout_url('/')
33+
greeting = 'Welcome, {}! (<a href="{}">sign out</a>)'.format(
34+
nickname, logout_url)
35+
else:
36+
login_url = users.create_login_url('/')
37+
greeting = '<a href="{}">Sign in</a>'.format(login_url)
38+
39+
self.response.write(
40+
'<html><body>{}</body></html>'.format(greeting))
41+
42+
43+
class AdminPage(webapp2.RequestHandler):
44+
def get(self):
45+
user = users.get_current_user()
46+
if user:
47+
if users.is_current_user_admin():
48+
self.response.write('You are an administrator.')
49+
else:
50+
self.response.write('You are not an administrator.')
51+
else:
52+
self.response.write('You are not logged in.')
53+
54+
55+
app = webapp2.WSGIApplication([
56+
('/', MainPage),
57+
('/admin', AdminPage)
58+
], debug=True)
59+
60+
# [END all]

appengine/users/main_test.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2016 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 webtest
17+
18+
19+
def test_index(testbed, login):
20+
app = webtest.TestApp(main.app)
21+
22+
response = app.get('/')
23+
assert 'Login' in response.body
24+
25+
login()
26+
response = app.get('/')
27+
assert 'Logout' in response.body
28+
assert '[email protected]' in response.body
29+
30+
31+
def test_admin(testbed, login):
32+
app = webtest.TestApp(main.app)
33+
34+
response = app.get('/admin')
35+
assert 'You are not logged in' in response.body
36+
37+
login()
38+
response = app.get('/admin')
39+
assert 'You are not an administrator' in response.body
40+
41+
login(is_admin=True)
42+
response = app.get('/admin')
43+
assert 'You are an administrator' in response.body

0 commit comments

Comments
 (0)