Skip to content

Commit b9bcd4d

Browse files
committed
Test refactor
Change-Id: I1827bfedba0c4af4e42d8d5a970bb09a8fa1f60f
1 parent 0426f1b commit b9bcd4d

22 files changed

+909
-0
lines changed
751 Bytes
Binary file not shown.

flask/04-context/config.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python3
2+
# Copyright 2021 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
# use this file except in compliance with the License. You may obtain a copy of
6+
# the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
# License for the specific language governing permissions and limitations under
14+
# the License.
15+
"""The Flask server configuration."""
16+
17+
import os
18+
19+
DATABASE_FILE_NAME = os.path.join(os.path.abspath(os.path.dirname(__file__)),
20+
'data.sqlite')
21+
22+
class Config(object):
23+
# Note: A secret key is included in the sample so that it works.
24+
# If you use this code in your application, replace this with a truly secret
25+
# key. See https://flask.palletsprojects.com/quickstart/#sessions.
26+
SECRET_KEY = os.environ.get(
27+
'SECRET_KEY') or "REPLACE ME - this value is here as a placeholder."
28+
29+
# Configure the flask cookie settings per the iframe security recommendations:
30+
# https://developers.google.com/classroom/eap/add-ons-alpha/iframes#iframe_security_guidelines
31+
SESSION_COOKIE_SECURE = True
32+
SESSION_COOKIE_HTTPONLY = True
33+
SESSION_COOKIE_SAMESITE = "None"
34+
35+
# Point to a database file in the project root.
36+
37+
SQLALCHEMY_DATABASE_URI=f"sqlite:///{DATABASE_FILE_NAME}"
38+
SQLALCHEMY_TRACK_MODIFICATIONS=False

flask/04-context/requirements.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Flask==2.0.2
2+
Flask_SQLAlchemy==2.5.1
3+
google_api_python_client==2.33.0
4+
google_auth_oauthlib==0.4.6
5+
protobuf==3.19.1
6+
requests==2.26.0

flask/04-context/webapp.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python3
2+
# Copyright 2021 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
# use this file except in compliance with the License. You may obtain a copy of
6+
# the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
# License for the specific language governing permissions and limitations under
14+
# the License.
15+
"""Entry point for the Flask server.
16+
17+
Loads the webapp module and starts the server."""
18+
19+
from webapp import app
20+
import os
21+
22+
if __name__ == "__main__":
23+
# Allow the OAuth flow to adjust scopes.
24+
os.environ["OAUTHLIB_RELAX_TOKEN_SCOPE"] = "1"
25+
26+
app.run(host="localhost",
27+
ssl_context=("localhost.pem", "localhost-key.pem"),
28+
debug=True)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from flask import Flask
2+
import config
3+
from flask_sqlalchemy import SQLAlchemy
4+
from os import path
5+
6+
app = Flask(__name__)
7+
app.config.from_object(config.Config)
8+
9+
db = SQLAlchemy(app)
10+
11+
from webapp import routes, models
12+
13+
# Initialize the database file if not created.
14+
if not path.exists(config.DATABASE_FILE_NAME):
15+
db.create_all()
612 Bytes
Binary file not shown.
501 Bytes
Binary file not shown.
733 Bytes
Binary file not shown.
7.39 KB
Binary file not shown.

flask/04-context/webapp/models.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python3
2+
# Copyright 2021 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
# use this file except in compliance with the License. You may obtain a copy of
6+
# the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
# License for the specific language governing permissions and limitations under
14+
# the License.
15+
"""Data models for the Flask server.
16+
17+
Uses the flask-sqlalchemy database object defined in the webapp module."""
18+
19+
from webapp import db
20+
21+
# Database model to represent a user.
22+
class User(db.Model):
23+
# The user's identifying information:
24+
id = db.Column(db.String(120), primary_key=True)
25+
display_name = db.Column(db.String(80))
26+
email = db.Column(db.String(120), unique=True)
27+
portrait_url = db.Column(db.Text())
28+
29+
# The user's refresh token, which will be used to obtain an access token.
30+
# Note that refresh tokens will become invalid if:
31+
# - The refresh token has not been used for six months.
32+
# - The user revokes your app's access permissions.
33+
# - The user changes passwords.
34+
# - The user belongs to a Google Cloud Platform organization
35+
# that has session control policies in effect.
36+
refresh_token = db.Column(db.Text())
37+
38+
def __repr__(self):
39+
return f"<User {self.display_name}, ID {self.id}>"

0 commit comments

Comments
 (0)