From 47259b07de8109b04be9fce4eb7f6761cf52d37b Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Tue, 19 Apr 2016 13:31:30 -0700 Subject: [PATCH] Moving app engine users example Change-Id: Ia6e58ab5418ce701f403db8cded023411db70936 --- appengine/ndb/entities/snippets.py | 9 ++++ appengine/ndb/entities/snippets_test.py | 8 ++++ appengine/users/app.yaml | 7 +++ appengine/users/main.py | 60 +++++++++++++++++++++++++ appengine/users/main_test.py | 43 ++++++++++++++++++ 5 files changed, 127 insertions(+) create mode 100644 appengine/users/app.yaml create mode 100644 appengine/users/main.py create mode 100644 appengine/users/main_test.py diff --git a/appengine/ndb/entities/snippets.py b/appengine/ndb/entities/snippets.py index 6eb3a38ddd61..4ad8b6084bfd 100644 --- a/appengine/ndb/entities/snippets.py +++ b/appengine/ndb/entities/snippets.py @@ -283,3 +283,12 @@ def construct_keys_from_range_of_reserved_ids(first, last): def reserve_model_ids_up_to(N): first, last = MyModel.allocate_ids(max=N) return first, last + + +class ModelWithUser(ndb.Model): + user_id = ndb.StringProperty() + color = ndb.StringProperty() + + @classmethod + def get_by_user(cls, user): + return cls.query().filter(cls.user_id == user.user_id()).get() diff --git a/appengine/ndb/entities/snippets_test.py b/appengine/ndb/entities/snippets_test.py index 4886b772aab5..0b1eb6cf2171 100644 --- a/appengine/ndb/entities/snippets_test.py +++ b/appengine/ndb/entities/snippets_test.py @@ -14,6 +14,7 @@ import inspect +from google.appengine.api import users from google.appengine.ext import ndb from google.appengine.ext.ndb.google_imports import datastore_errors import pytest @@ -227,3 +228,10 @@ def test_construct_keys_from_range_of_reserved_ids(client): def test_reserve_model_ids_up_to(client): first, last = snippets.reserve_model_ids_up_to(5) assert last - first >= 4 + + +def test_model_with_user(client): + user = users.User(email='user@example.com', _user_id='123') + item = snippets.ModelWithUser(user_id=user.user_id()) + item.put() + assert snippets.ModelWithUser.get_by_user(user) == item diff --git a/appengine/users/app.yaml b/appengine/users/app.yaml new file mode 100644 index 000000000000..42ad35ed2a84 --- /dev/null +++ b/appengine/users/app.yaml @@ -0,0 +1,7 @@ +runtime: python27 +threadsafe: yes +api_version: 1 + +handlers: +- url: .* + script: main.app diff --git a/appengine/users/main.py b/appengine/users/main.py new file mode 100644 index 000000000000..7904a20c4428 --- /dev/null +++ b/appengine/users/main.py @@ -0,0 +1,60 @@ +# Copyright 2016 Google Inc. +# +# 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 Google App Engine application that demonstrates using the Users API + +For more information about App Engine, see README.md under /appengine. +""" + +# [START all] + +from google.appengine.api import users +import webapp2 + + +class MainPage(webapp2.RequestHandler): + def get(self): + user = users.get_current_user() + if user: + nickname = user.nickname() + logout_url = users.create_logout_url('/') + greeting = 'Welcome, {}! (sign out)'.format( + nickname, logout_url) + else: + login_url = users.create_login_url('/') + greeting = 'Sign in'.format(login_url) + + self.response.write( + '{}'.format(greeting)) + + +class AdminPage(webapp2.RequestHandler): + def get(self): + user = users.get_current_user() + if user: + if users.is_current_user_admin(): + self.response.write('You are an administrator.') + else: + self.response.write('You are not an administrator.') + else: + self.response.write('You are not logged in.') + + +app = webapp2.WSGIApplication([ + ('/', MainPage), + ('/admin', AdminPage) +], debug=True) + +# [END all] diff --git a/appengine/users/main_test.py b/appengine/users/main_test.py new file mode 100644 index 000000000000..d695faa4e7fe --- /dev/null +++ b/appengine/users/main_test.py @@ -0,0 +1,43 @@ +# Copyright 2016 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 webtest + + +def test_index(testbed, login): + app = webtest.TestApp(main.app) + + response = app.get('/') + assert 'Login' in response.body + + login() + response = app.get('/') + assert 'Logout' in response.body + assert 'user@example.com' in response.body + + +def test_admin(testbed, login): + app = webtest.TestApp(main.app) + + response = app.get('/admin') + assert 'You are not logged in' in response.body + + login() + response = app.get('/admin') + assert 'You are not an administrator' in response.body + + login(is_admin=True) + response = app.get('/admin') + assert 'You are an administrator' in response.body