Skip to content

Commit a103968

Browse files
committed
blacken
1 parent 9d85db1 commit a103968

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

flask-google-login/app.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@
3232
# https://flask-login.readthedocs.io/en/latest
3333
login_manager = LoginManager()
3434
login_manager.init_app(app)
35+
36+
3537
@login_manager.unauthorized_handler
3638
def unauthorized():
37-
return 'You must be logged in to access this content.', 403
39+
return "You must be logged in to access this content.", 403
40+
3841

3942
# Naive database setup
4043
try:
@@ -51,6 +54,7 @@ def unauthorized():
5154
def load_user(user_id):
5255
return User.get(user_id)
5356

57+
5458
@app.route("/")
5559
def index():
5660
if current_user.is_authenticated:
@@ -65,6 +69,7 @@ def index():
6569
else:
6670
return '<a class="button" href="/login">Login with Google</a>'
6771

72+
6873
@app.route("/login")
6974
def login():
7075
# Find out what URL to hit for Google login
@@ -80,6 +85,7 @@ def login():
8085
)
8186
return redirect(request_uri)
8287

88+
8389
@app.route("/login/callback")
8490
def callback():
8591
# Get authorization code Google sent back to you
@@ -95,7 +101,7 @@ def callback():
95101
token_endpoint,
96102
authorization_response=request.url,
97103
redirect_url=request.base_url,
98-
code=code
104+
code=code,
99105
)
100106
token_response = requests.post(
101107
token_url,
@@ -139,14 +145,17 @@ def callback():
139145
# Send user back to homepage
140146
return redirect(url_for("index"))
141147

148+
142149
@app.route("/logout")
143150
@login_required
144151
def logout():
145152
logout_user()
146153
return redirect(url_for("index"))
147154

155+
148156
def get_google_provider_cfg():
149157
return requests.get(GOOGLE_DISCOVERY_URL).json()
150158

159+
151160
if __name__ == "__main__":
152161
app.run(ssl_context="adhoc")

flask-google-login/db.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,37 @@
55
from flask import current_app, g
66
from flask.cli import with_appcontext
77

8+
89
def get_db():
910
if "db" not in g:
10-
g.db = sqlite3.connect(
11-
"sqlite_db", detect_types=sqlite3.PARSE_DECLTYPES
12-
)
11+
g.db = sqlite3.connect("sqlite_db", detect_types=sqlite3.PARSE_DECLTYPES)
1312
g.db.row_factory = sqlite3.Row
1413

1514
return g.db
1615

16+
1717
def close_db(e=None):
1818
db = g.pop("db", None)
1919

2020
if db is not None:
2121
db.close()
2222

23+
2324
def init_db():
2425
db = get_db()
2526

2627
with current_app.open_resource("schema.sql") as f:
2728
db.executescript(f.read().decode("utf8"))
2829

30+
2931
@click.command("init-db")
3032
@with_appcontext
3133
def init_db_command():
3234
"""Clear the existing data and create new tables."""
3335
init_db()
3436
click.echo("Initialized the database.")
3537

38+
3639
def init_app(app):
3740
app.teardown_appcontext(close_db)
38-
app.cli.add_command(init_db_command)
41+
app.cli.add_command(init_db_command)

flask-google-login/user.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from db import get_db
44

5+
56
class User(UserMixin):
67
def __init__(self, id_, name, email, profile_pic):
78
self.id = id_
@@ -12,15 +13,11 @@ def __init__(self, id_, name, email, profile_pic):
1213
@staticmethod
1314
def get(user_id):
1415
db = get_db()
15-
user = db.execute(
16-
"SELECT * FROM user WHERE id = ?", (user_id,)
17-
).fetchone()
16+
user = db.execute("SELECT * FROM user WHERE id = ?", (user_id,)).fetchone()
1817
if not user:
1918
return None
2019

21-
user = User(
22-
id_=user[0], name=user[1], email=user[2], profile_pic=user[3]
23-
)
20+
user = User(id_=user[0], name=user[1], email=user[2], profile_pic=user[3])
2421
return user
2522

2623
@staticmethod
@@ -30,4 +27,4 @@ def create(id_, name, email, profile_pic):
3027
"INSERT INTO user (id, name, email, profile_pic) VALUES (?, ?, ?, ?)",
3128
(id_, name, email, profile_pic),
3229
)
33-
db.commit()
30+
db.commit()

0 commit comments

Comments
 (0)