Skip to content

Commit 7f3f829

Browse files
committed
Added missing logging
1 parent 976ce05 commit 7f3f829

26 files changed

+191
-57
lines changed

backend/PyMatcha/models/notification.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from typing import Dict
2424
from typing import Optional
2525

26+
from flask import current_app
2627
from PyMatcha.utils import create_notifications_table
2728
from PyMatcha.utils.orm import Field
2829
from PyMatcha.utils.orm import Model
@@ -61,6 +62,7 @@ def create(
6162
return None
6263
if not dt_received:
6364
dt_received = datetime.utcnow()
65+
current_app.logger.debug(f"Creating notification for user {user_id} from {trigger_id} with type {type}")
6466
new_notif = Notification(
6567
user_id=user_id, content=content, type=type, link_to=link_to, is_seen=is_seen, dt_received=dt_received
6668
)

backend/PyMatcha/routes/api/auth/email.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,49 +42,46 @@
4242

4343
@auth_email_bp.route("/auth/confirm/<token>", methods=["POST"])
4444
def confirm_email(token):
45-
current_app.logger.debug("/auth/confirm/{} -> Call".format(token))
4645
try:
4746
email, token_type = confirm_token(token, expiration=7200)
4847
except (SignatureExpired, BadSignature) as e:
4948
if e == SignatureExpired:
50-
current_app.logger.debug("/auth/confirm -> Signature Expired")
49+
current_app.logger.debug(f"Signature Expired for token {token}")
5150
raise BadRequestError("Signature Expired.", "Request another email confirmation and try again.")
5251
else:
53-
current_app.logger.debug("/auth/confirm -> Bad Expired")
52+
current_app.logger.debug(f"Bad Expired for token {token}")
5453
raise BadRequestError("Bad Signature.", "Request another password reset and try again.")
5554
else:
5655
if token_type != "confirm":
57-
current_app.logger.debug("/auth/confirm -> Wrong token type")
56+
current_app.logger.debug(f"Wrong token type for token {token}")
5857
raise BadRequestError("Wrong token type.")
5958
try:
6059
u = get_user(email)
6160
except NotFoundError:
62-
current_app.logger.debug("/auth/confirm -> User not found")
6361
raise NotFoundError("User not found.")
6462
if u.is_confirmed:
65-
current_app.logger.debug("/auth/confirm -> User already confirmed")
63+
current_app.logger.debug("User already confirmed")
6664
raise BadRequestError("Email already confirmed.", "")
6765
else:
6866
u.is_confirmed = True
6967
u.confirmed_on = datetime.datetime.utcnow()
7068
u.save()
71-
current_app.logger.debug("/auth/confirm -> User {} confirmed.".format(u.id))
69+
current_app.logger.debug(f"User {u.id} confirmed.")
7270
return Success("Confirmation successful.")
7371

7472

7573
@auth_email_bp.route("/auth/confirm/new", methods=["POST"])
7674
@validate_params(REQUIRED_KEYS_NEW_EMAIL_CONF)
7775
def request_new_email_conf():
78-
current_app.logger.debug("/auth/confirm/new -> Call")
7976
data = request.get_json()
8077
email = data["email"]
8178
try:
8279
u = get_user(email)
8380
except NotFoundError:
84-
current_app.logger.debug("/auth/confirm/new -> User not found")
81+
pass
8582
else:
8683
if u.is_confirmed:
87-
current_app.logger.debug("/auth/confirm/new -> User found, Already confirmed.")
84+
current_app.logger.debug("Already confirmed.")
8885
else:
8986
current_app.logger.debug("/auth/confirm/new -> User found, sending new confirmation email")
9087
token = generate_confirmation_token(email=email, token_type="confirm")

backend/PyMatcha/routes/api/auth/login.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,22 @@
4545
@auth_login_bp.route("/auth/login", methods=["POST"])
4646
@validate_params(REQUIRED_KEYS_LOGIN)
4747
def auth_login():
48-
current_app.logger.debug("/auth/login -> Call")
4948
data = request.get_json()
5049
username = data["username"]
5150
password = data["password"]
5251
try:
5352
u = get_user(username)
5453
except NotFoundError:
55-
current_app.logger.debug("/auth/login -> User not found")
54+
current_app.logger.debug("User not found for login")
5655
raise UnauthorizedError("Incorrect username or password.")
5756
if not check_password(u.password, password):
58-
current_app.logger.debug("/auth/login -> Password invalid")
57+
current_app.logger.debug("Password invalid for login")
5958
raise UnauthorizedError("Incorrect username or password.")
6059

6160
if not u.is_confirmed:
62-
current_app.logger.debug("/auth/login -> User is trying to login unconfirmed")
61+
current_app.logger.debug("User is trying to login unconfirmed")
6362
raise UnauthorizedError("User needs to be confirmed first.", "Try again when you have confirmed your email.")
63+
current_app.logger.debug(f"Creating token for {u.id}")
6464
access_token = create_access_token(identity=u.get_jwt_info(), fresh=True)
6565
refresh_token = create_refresh_token(identity=u.get_jwt_info())
6666
access_jti = get_jti(access_token)
@@ -69,7 +69,7 @@ def auth_login():
6969
redis.set("is_revoked_jti:" + access_jti, "false", ACCESS_TOKEN_EXPIRES * 1.2)
7070
redis.set("is_revoked_jti:" + refresh_jti, "false", REFRESH_TOKEN_EXPIRES * 1.2)
7171

72-
current_app.logger.debug("/auth/login -> Returning access token for user {}".format(username))
72+
current_app.logger.debug("Returning access token for user {}".format(username))
7373
u.is_online = True
7474
u.dt_lastseen = datetime.datetime.utcnow()
7575
u.save()
@@ -81,6 +81,7 @@ def auth_login():
8181
@jwt_refresh_token_required
8282
def refresh():
8383
current_user = get_jwt_identity()
84+
current_app.logger.info(f"Refreshing token for {current_user['id']}")
8485
access_token = create_access_token(identity=current_user)
8586
access_jti = get_jti(encoded_token=access_token)
8687
redis.set("is_revoked_jti:" + access_jti, "false", ACCESS_TOKEN_EXPIRES * 1.2)
@@ -97,4 +98,5 @@ def logout():
9798
refresh_jti = get_jti(refresh_token)
9899
redis.set("is_revoked_jti:" + access_jti, "true", ACCESS_TOKEN_EXPIRES * 1.2)
99100
redis.set("is_revoked_jti:" + refresh_jti, "true", REFRESH_TOKEN_EXPIRES * 1.2)
101+
current_app.logger.info(f"Revoked tokens with jtis {access_jti}, {refresh_jti}")
100102
return Success("Logout successful.")

backend/PyMatcha/routes/api/auth/password.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,78 +42,74 @@
4242
@auth_password_bp.route("/auth/password/forgot", methods=["POST"])
4343
@validate_params(REQUIRED_KEYS_PASSWORD_FORGOT)
4444
def forgot_password():
45-
current_app.logger.debug("/auth/password/forgot -> Call")
4645
data = request.get_json()
4746
try:
4847
get_user(data["email"])
4948
except NotFoundError:
50-
current_app.logger.debug("/auth/password/forgot -> User {} not found, no email sent".format(data["email"]))
49+
current_app.logger.debug("User not found, no email sent")
5150
pass
5251
else:
5352
token = generate_confirmation_token(email=data["email"], token_type="reset")
5453
link = FRONTEND_PASSWORD_RESET_URL + token
5554
rendered_html = render_template("password_reset.html", link=link)
56-
current_app.logger.debug("/auth/password/forgot -> Sending worker request to send email")
55+
current_app.logger.debug("Sending worker request to send email")
5756
send_mail_html.delay(dest=data["email"], subject="Reset your password on PyMatcha", html=rendered_html)
58-
current_app.logger.debug(
59-
"/auth/password/forgot -> Password reset mail sent successfully for user {}".format(data["email"])
60-
)
57+
current_app.logger.debug("Password reset mail sent successfully for user.")
6158
return Success("Password reset mail sent successfully if user exists in DB.")
6259

6360

6461
@auth_password_bp.route("/auth/password/reset", methods=["POST"])
6562
@validate_params(REQUIRED_KEYS_PASSWORD_RESET)
6663
def reset_password():
67-
current_app.logger.debug("/auth/password/reset -> Call")
6864
data = request.get_json()
65+
token = data["token"]
6966
try:
70-
email, token_type = confirm_token(data["token"], expiration=7200)
67+
email, token_type = confirm_token(token, expiration=7200)
7168
except (SignatureExpired, BadSignature) as e:
7269
if e == SignatureExpired:
73-
current_app.logger.debug("/auth/password/reset -> Signature Expired")
70+
current_app.logger.debug(f"Signature Expired for {token}")
7471
raise BadRequestError("Signature Expired.", "Request another password reset and try again.")
7572
else:
76-
current_app.logger.debug("/auth/password/reset -> Bad Signature")
73+
current_app.logger.debug(f"Bad Signature for {token}")
7774
raise BadRequestError("Bad Signature.", "Request another password reset and try again.")
7875
else:
7976
if token_type != "reset":
80-
current_app.logger.debug("/auth/password/reset -> Wrong token type")
77+
current_app.logger.debug(f"Wrong token type for {token}")
8178
raise BadRequestError("Wrong token type.")
8279
try:
8380
u = get_user(email)
8481
except NotFoundError:
85-
current_app.logger.debug("/auth/password/reset -> User not found")
8682
raise NotFoundError("User not found.")
87-
if u.previous_reset_token == data["token"]:
88-
current_app.logger.debug("/auth/password/reset -> Token already used")
83+
if u.previous_reset_token == token:
84+
current_app.logger.debug("Token already used")
8985
raise BadRequestError("Token already used", "Please request a new one.")
9086
u.password = hash_password(data["password"])
91-
u.previous_reset_token = data["token"]
87+
u.previous_reset_token = token
9288
u.save()
93-
current_app.logger.debug("/auth/password/reset -> Password reset successfully")
89+
current_app.logger.debug("Password reset successfully")
9490
return Success("Password reset successful.")
9591

9692

9793
@auth_password_bp.route("/auth/password/check_token", methods=["POST"])
9894
@validate_params({"token": str})
9995
def check_token_validity():
10096
data = request.get_json()
97+
token = data["token"]
10198
try:
102-
email, token_type = confirm_token(data["token"], expiration=7200)
99+
email, token_type = confirm_token(token, expiration=7200)
103100
except (SignatureExpired, BadSignature) as e:
104101
if e == SignatureExpired:
105-
current_app.logger.debug("/auth/password/reset -> Signature Expired")
102+
current_app.logger.debug(f"Signature Expired for {token}")
106103
raise BadRequestError("Signature Expired.", "Request another password reset and try again.")
107104
else:
108-
current_app.logger.debug("/auth/password/reset -> Bad Signature")
105+
current_app.logger.debug(f"Bad Signature for {token}")
109106
raise BadRequestError("Bad Signature.", "Request another password reset and try again.")
110107
else:
111108
try:
112109
u = get_user(email)
113110
except NotFoundError:
114-
current_app.logger.debug("/auth/password/reset -> User not found")
115111
raise NotFoundError("User not found.")
116-
if u.previous_reset_token == data["token"]:
117-
current_app.logger.debug("/auth/password/reset -> Token already used")
112+
if u.previous_reset_token == token:
113+
current_app.logger.debug("Token already used")
118114
raise BadRequestError("Token already used", "Please request a new one.")
119115
return Success("Reset token is correct.")

backend/PyMatcha/routes/api/auth/register.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,10 @@
3636
@auth_register_bp.route("/auth/register", methods=["POST"])
3737
@validate_params(REQUIRED_KEYS_USER_CREATION)
3838
def api_create_user():
39-
current_app.logger.debug("/auth/register -> Call")
4039
data = request.get_json()
4140
data["email"] = data["email"].lower()
4241
try:
43-
current_app.logger.debug("Trying to register new user {}, {}".format(data["email"], data["username"]))
42+
current_app.logger.debug("Trying to register new user")
4443
new_user = User.register(
4544
email=data["email"],
4645
username=data["username"],
@@ -56,4 +55,5 @@ def api_create_user():
5655
link = FRONTEND_EMAIL_CONFIRMATION_URL + token
5756
rendered_html = render_template("confirm_email.html", link=link)
5857
send_mail_html.delay(dest=data["email"], subject="Confirm your email on PyMatcha", html=rendered_html)
58+
current_app.logger.info("Registered new user successfully.")
5959
return SuccessOutputMessage("email", new_user.email, "New user successfully created.")

backend/PyMatcha/routes/api/history.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
1+
"""
2+
PyMatcha - A Python Dating Website
3+
Copyright (C) 2018-2019 jlasne/gmorer
4+
<jlasne@student.42.fr> - <lauris.skraucis@gmail.com>
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
"""
119
from flask import Blueprint
20+
from flask import current_app
221
from flask_jwt_extended import current_user
322
from flask_jwt_extended import jwt_required
423
from PyMatcha.models.user import User
@@ -25,6 +44,7 @@ def history_viewed_people():
2544
user_dict["common_tags"] = common_tags
2645
user_dict["distance"] = distance
2746
viewed_people.append(user_dict)
47+
current_app.logger.info(f"Returning viewed profiles for user {current_user.id}")
2848
return SuccessOutput("viewed", viewed_people)
2949

3050

@@ -43,6 +63,7 @@ def history_viewed_me():
4363
user_dict["common_tags"] = common_tags
4464
user_dict["distance"] = distance
4565
viewed_people.append(user_dict)
66+
current_app.logger.info(f"Returning profiles who viewed user {current_user.id}")
4667
return SuccessOutput("viewed_me", viewed_people)
4768

4869

@@ -61,6 +82,7 @@ def history_liked_people():
6182
user_dict["common_tags"] = common_tags
6283
user_dict["distance"] = distance
6384
liked_people.append(user_dict)
85+
current_app.logger.info(f"Returning liked profiles for user {current_user.id}")
6486
return SuccessOutput("liked", liked_people)
6587

6688

@@ -79,6 +101,7 @@ def history_liked_me():
79101
user_dict["common_tags"] = common_tags
80102
user_dict["distance"] = distance
81103
liked_people.append(user_dict)
104+
current_app.logger.info(f"Returning profiles who liked user {current_user.id}")
82105
return SuccessOutput("liked_me", liked_people)
83106

84107

@@ -97,4 +120,5 @@ def history_blocked():
97120
user_dict["common_tags"] = common_tags
98121
user_dict["distance"] = distance
99122
blocked_people.append(user_dict)
123+
current_app.logger.info(f"Returning blocked profiles for user {current_user.id}")
100124
return SuccessOutput("blocked", blocked_people)

backend/PyMatcha/routes/api/like.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from datetime import timedelta
33

44
from flask import Blueprint
5+
from flask import current_app
56
from flask import request
67
from flask_jwt_extended import current_user
78
from flask_jwt_extended import jwt_required
@@ -50,6 +51,7 @@ def like_profile(uid):
5051
Like.create(liker_id=current_user.id, liked_id=u.id, is_superlike=is_superlike)
5152

5253
if u.already_likes(current_user.id):
54+
current_app.logger.debug(f"Creating match between user {current_user.id} and {u.id}")
5355
Match.create(user_1=current_user.id, user_2=u.id)
5456
Notification.create(
5557
trigger_id=current_user.id,
@@ -75,6 +77,7 @@ def like_profile(uid):
7577
return Success("It's a match !")
7678

7779
if is_superlike:
80+
current_app.logger.debug(f"Creating superlike between user {current_user.id} and {u.id}")
7881
Notification.create(
7982
trigger_id=current_user.id,
8083
user_id=u.id,
@@ -84,6 +87,7 @@ def like_profile(uid):
8487
)
8588
return Success("Superliked user.")
8689
else:
90+
current_app.logger.debug(f"Creating like between user {current_user.id} and {u.id}")
8791
Notification.create(
8892
trigger_id=current_user.id,
8993
user_id=u.id,
@@ -106,15 +110,17 @@ def unlike_profile(uid):
106110
if not current_user.already_likes(u.id):
107111
raise BadRequestError("You never liked this person in the first place.")
108112
Like.get_multi(liked_id=u.id, liker_id=current_user.id).delete()
113+
current_app.logger.debug(f"Deleting like between user {current_user.id} and {u.id}")
109114

110115
m1 = Match.get_multi(user_1=u.id, user_2=current_user.id)
111116
m2 = Match.get_multi(user_1=current_user.id, user_2=u.id)
112117

113118
if m1:
119+
current_app.logger.debug(f"Deleting match between user {current_user.id} and {u.id}")
114120
m1.delete()
115121
elif m2:
122+
current_app.logger.debug(f"Deleting match between user {current_user.id} and {u.id}")
116123
m2.delete()
117-
118124
Notification.create(
119125
trigger_id=current_user.id,
120126
user_id=u.id,

backend/PyMatcha/routes/api/match.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from flask import Blueprint
2+
from flask import current_app
23
from flask_jwt_extended import current_user
34
from flask_jwt_extended import jwt_required
45
from PyMatcha.utils.success import SuccessOutput
@@ -9,4 +10,5 @@
910
@match_bp.route("/matches", methods=["GET"])
1011
@jwt_required
1112
def get_user_matches():
13+
current_app.logger.debug(f"Getting matches for {current_user.id}")
1214
return SuccessOutput("matches", [m.to_dict() for m in current_user.get_matches()])

0 commit comments

Comments
 (0)