-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
<html> | ||
<head> | ||
<title>VisitMe Example</title> | ||
</head> | ||
<body> | ||
|
||
<h1>VisitMe example</h1> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
<html> | ||
<head> | ||
<title>VisitMe Example</title> | ||
</head> | ||
<body> | ||
|
||
<h1>VisitMe example</h1> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
<html> | ||
<head> | ||
<title>VisitMe Example</title> | ||
</head> | ||
<body> | ||
|
||
<h1>VisitMe example</h1> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
<html> | ||
<head> | ||
<title>VisitMe Example</title> | ||
</head> | ||
<body> | ||
|
||
<h1>VisitMe example</h1> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
<html> | ||
<head> | ||
<title>VisitMe Example</title> | ||
</head> | ||
<body> | ||
|
||
<h1>VisitMe example</h1> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
<html> | ||
<head> | ||
<title>VisitMe Example</title> | ||
</head> | ||
<body> | ||
|
||
<h1>VisitMe example</h1> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
<html> | ||
<head> | ||
<title>VisitMe Example</title> | ||
</head> | ||
<body> | ||
|
||
<h1>VisitMe example</h1> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
<html> | ||
<head> | ||
<title>VisitMe Example</title> | ||
</head> | ||
<body> | ||
|
||
<h1>VisitMe example</h1> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
<html> | ||
<head> | ||
<title>VisitMe Example</title> | ||
</head> | ||
<body> | ||
|
||
<h1>VisitMe example</h1> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
<html> | ||
<head> | ||
<title>VisitMe Example</title> | ||
</head> | ||
<body> | ||
|
||
<h1>VisitMe example</h1> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
<html> | ||
<head> | ||
<title>VisitMe Example</title> | ||
</head> | ||
<body> | ||
|
||
<h1>VisitMe example</h1> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# This file specifies files that are *not* uploaded to Google Cloud Platform | ||
# using gcloud. It follows the same syntax as .gitignore, with the addition of | ||
# "#!include" directives (which insert the entries of the given .gitignore-style | ||
# file at that point). | ||
# | ||
# For more information, run: | ||
# $ gcloud topic gcloudignore | ||
# | ||
.gcloudignore | ||
|
||
# Source code control files | ||
.git/ | ||
.gitignore | ||
.hgignore | ||
.hg/ | ||
|
||
# README/text files | ||
LICENSE | ||
*.md | ||
|
||
# Tests/results (not in .gitignore) | ||
noxfile.py | ||
pylintrc | ||
pylintrc.test | ||
|
||
# most of .gitignore (except `lib`) | ||
# | ||
# Python | ||
*.py[cod] | ||
__pycache__/ | ||
/setup.cfg | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Packages | ||
*.egg | ||
*.egg-info | ||
dist | ||
build | ||
eggs | ||
.eggs | ||
parts | ||
bin | ||
var | ||
sdist | ||
develop-eggs | ||
.installed.cfg | ||
lib64 | ||
*.tgz | ||
|
||
# Installer logs | ||
pip-log.txt | ||
|
||
# Tests/results | ||
.nox/ | ||
.pytest_cache/ | ||
.cache | ||
.pytype | ||
.coverage | ||
coverage.xml | ||
*sponge_log.xml | ||
system_tests/local_test_setup | ||
|
||
# Mac | ||
.DS_Store | ||
|
||
# IDEs/editors | ||
*.sw[op] | ||
*~ | ||
.vscode | ||
.idea | ||
|
||
# Built documentation | ||
docs/_build | ||
docs.metadata | ||
|
||
# Virtual environment | ||
env/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Module 20 - Add usage of App Engine `users` to Flask `ndb` sample app | ||
|
||
This repo folder is the corresponding (Python 2) code to the _forthcoming_ Module 20 codelab. The tutorial STARTs with the Python 2 code in the [Module 1 repo folder](/mod1-flask) and leads developers through adding usage of the Users API via App Engine `users`, culminating in the code in this folder. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# 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. | ||
|
||
runtime: python27 | ||
threadsafe: yes | ||
api_version: 1 | ||
|
||
handlers: | ||
- url: /.* | ||
script: main.app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# 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. | ||
|
||
from google.appengine.ext import vendor | ||
|
||
# Set PATH to your libraries folder. | ||
PATH = 'lib' | ||
# Add libraries installed in the PATH folder. | ||
vendor.add(PATH) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# 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. | ||
|
||
from flask import Flask, render_template, request | ||
from google.appengine.api import app_identity, users | ||
from google.appengine.ext import ndb | ||
|
||
app = Flask(__name__) | ||
|
||
|
||
class Visit(ndb.Model): | ||
'Visit entity registers visitor IP address & timestamp' | ||
visitor = ndb.StringProperty() | ||
timestamp = ndb.DateTimeProperty(auto_now_add=True) | ||
|
||
def store_visit(remote_addr, user_agent): | ||
'create new Visit entity in Datastore' | ||
Visit(visitor='{}: {}'.format(remote_addr, user_agent)).put() | ||
|
||
def fetch_visits(limit): | ||
'get most recent visits' | ||
return Visit.query().order(-Visit.timestamp).fetch(limit) | ||
|
||
|
||
@app.route('/') | ||
def root(): | ||
'main application (GET) handler' | ||
store_visit(request.remote_addr, request.user_agent) | ||
visits = fetch_visits(10) | ||
|
||
# put together users context for web template | ||
user = users.get_current_user() | ||
context = { # logged in | ||
'who': user.nickname(), | ||
'admin': '(admin)' if users.is_current_user_admin() else '', | ||
'sign': 'Logout', | ||
'link': '/_ah/logout?continue=https://%s/' % \ | ||
app_identity.get_default_version_hostname() | ||
} if user else { # not logged in | ||
'who': 'user', | ||
'admin': '', | ||
'sign': 'Login', | ||
'link': users.create_login_url('/'), | ||
} | ||
|
||
# add visits to context and render template | ||
context['visits'] = visits | ||
return render_template('index.html', **context) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
flask |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>VisitMe Example</title> | ||
</head> | ||
|
||
<body> | ||
<p> | ||
Welcome, {{ who }} <code>{{ admin }}</code> <button id="loginbtn">{{ sign }}</button> | ||
</p><hr> | ||
|
||
<h1>VisitMe example</h1> | ||
<h3>Last 10 visits</h3> | ||
<ul> | ||
{% for visit in visits %} | ||
<li>{{ visit.timestamp.ctime() }} from {{ visit.visitor }}</li> | ||
{% endfor %} | ||
</ul> | ||
|
||
<script> | ||
document.getElementById("loginbtn").onclick = () => { | ||
window.location.href = '{{ link }}'; | ||
}; | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
<html> | ||
<head> | ||
<title>VisitMe Example</title> | ||
</head> | ||
<body> | ||
|
||
<h1>VisitMe example</h1> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
<html> | ||
<head> | ||
<title>VisitMe Example</title> | ||
</head> | ||
<body> | ||
|
||
<h1>VisitMe example</h1> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
<html> | ||
<head> | ||
<title>VisitMe Example</title> | ||
</head> | ||
<body> | ||
|
||
<h1>VisitMe example</h1> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
<html> | ||
<head> | ||
<title>VisitMe Example</title> | ||
</head> | ||
<body> | ||
|
||
<h1>VisitMe example</h1> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
<html> | ||
<head> | ||
<title>VisitMe Example</title> | ||
</head> | ||
<body> | ||
|
||
<h1>VisitMe example</h1> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
<html> | ||
<head> | ||
<title>VisitMe Example</title> | ||
</head> | ||
<body> | ||
|
||
<h1>VisitMe example</h1> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
<html> | ||
<head> | ||
<title>VisitMe Example</title> | ||
</head> | ||
<body> | ||
|
||
<h1>VisitMe example</h1> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
<html> | ||
<head> | ||
<title>VisitMe Example</title> | ||
</head> | ||
<body> | ||
|
||
<h1>VisitMe example</h1> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
<html> | ||
<head> | ||
<title>VisitMe Example</title> | ||
</head> | ||
<body> | ||
|
||
<h1>VisitMe example</h1> | ||
|
Oops, something went wrong.