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

Moving app engine users example #265

Merged
merged 1 commit into from
Apr 19, 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
9 changes: 9 additions & 0 deletions appengine/ndb/entities/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
8 changes: 8 additions & 0 deletions appengine/ndb/entities/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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='[email protected]', _user_id='123')
item = snippets.ModelWithUser(user_id=user.user_id())
item.put()
assert snippets.ModelWithUser.get_by_user(user) == item
7 changes: 7 additions & 0 deletions appengine/users/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
runtime: python27
threadsafe: yes
api_version: 1

handlers:
- url: .*
script: main.app
60 changes: 60 additions & 0 deletions appengine/users/main.py
Original file line number Diff line number Diff line change
@@ -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, {}! (<a href="{}">sign out</a>)'.format(
nickname, logout_url)
else:
login_url = users.create_login_url('/')
greeting = '<a href="{}">Sign in</a>'.format(login_url)

self.response.write(
'<html><body>{}</body></html>'.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]
43 changes: 43 additions & 0 deletions appengine/users/main_test.py
Original file line number Diff line number Diff line change
@@ -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 '[email protected]' 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