Skip to content

Commit 7c9f4a4

Browse files
committed
Added PATCH route for username
1 parent 6474e73 commit 7c9f4a4

File tree

1 file changed

+27
-28
lines changed
  • backend/PyMatcha/routes/api/profile

1 file changed

+27
-28
lines changed

backend/PyMatcha/routes/api/profile/edit.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,19 @@
2525
from flask import request
2626
from flask_jwt_extended import current_user
2727
from flask_jwt_extended import jwt_required
28+
from PyMatcha.models.user import get_user
2829
from PyMatcha.utils import hash_password
2930
from PyMatcha.utils.confirm_token import generate_confirmation_token
3031
from PyMatcha.utils.decorators import validate_params
3132
from PyMatcha.utils.errors import BadRequestError
33+
from PyMatcha.utils.errors import NotFoundError
3234
from PyMatcha.utils.errors import UnauthorizedError
3335
from PyMatcha.utils.mail import send_mail_html
3436
from PyMatcha.utils.mail import send_mail_text
3537
from PyMatcha.utils.password import check_password
3638
from PyMatcha.utils.static import FRONTEND_EMAIL_CONFIRMATION_URL
3739
from PyMatcha.utils.success import Success
3840

39-
# from PyMatcha.models.user import get_user
40-
# from PyMatcha.utils.errors import NotFoundError
41-
4241
profile_edit_bp = Blueprint("profile_edit", __name__)
4342

4443
REQUIRED_PARAMS_EDIT_PROFILE = {
@@ -66,29 +65,38 @@ def edit_profile_first_name():
6665
return Success("First name successfully modified!")
6766

6867

69-
@profile_edit_bp.route("/profile/edit/first_name", methods=["PATCH"])
70-
@validate_params({"first_name": str})
68+
@profile_edit_bp.route("/profile/edit/last_name", methods=["PATCH"])
69+
@validate_params({"last_name": str})
7170
@jwt_required
7271
def edit_profile_last_name():
7372
if not current_user.is_profile_completed:
7473
raise BadRequestError("The user has not completed his profile.", "Complete your profile and try again.")
7574
data = request.get_json()
76-
first_name = data["first_name"]
77-
current_user.first_name = first_name
75+
last_name = data["last_name"]
76+
current_user.last_name = last_name
7877
current_user.save()
79-
return Success("First name successfully modified!")
78+
return Success("Last name successfully modified!")
79+
80+
81+
@profile_edit_bp.route("/profile/edit/username", methods=["PATCH"])
82+
@validate_params({"username": str})
83+
@jwt_required
84+
def edit_profile_username():
85+
if not current_user.is_profile_completed:
86+
raise BadRequestError("The user has not completed his profile.", "Complete your profile and try again.")
87+
data = request.get_json()
88+
username = data["username"]
89+
try:
90+
get_user(username)
91+
except NotFoundError:
92+
pass
93+
else:
94+
raise BadRequestError("Username taken.")
95+
current_user.username = username
96+
current_user.save()
97+
return Success("Username successfully modified!")
8098

8199

82-
# @profile_edit_bp.route("/profile/edit", methods=["PUT"])
83-
# @validate_params(REQUIRED_PARAMS_EDIT_PROFILE)
84-
# @jwt_required
85-
# def edit_profile():
86-
# if not current_user.is_profile_completed:
87-
# raise BadRequestError("The user has not completed his profile.", "Complete your profile and try again.")
88-
# data = request.get_json()
89-
# first_name = data["first_name"]
90-
# last_name = data["last_name"]
91-
# username = data["username"]
92100
# bio = data["bio"]
93101
# gender = data["gender"]
94102
# orientation = data["orientation"]
@@ -105,28 +113,19 @@ def edit_profile_last_name():
105113
# if age < 18:
106114
# raise BadRequestError("You must be 18 years old or older.")
107115
#
108-
# try:
109-
# get_user(username)
110-
# except NotFoundError:
111-
# pass
112-
# else:
113-
# raise BadRequestError("Username taken.")
116+
114117
#
115118
# if orientation not in ["heterosexual", "homosexual", "bisexual", "other"]:
116119
# raise BadRequestError("Orientation must be heterosexual, homosexual, bisexual or other.")
117120
#
118121
# if gender not in ["male", "female", "other"]:
119122
# raise BadRequestError("Gender must be male, female or other.")
120123
#
121-
# current_user.first_name = first_name
122-
# current_user.last_name = last_name
123-
# current_user.username = username
124124
# current_user.bio = bio
125125
# current_user.gender = gender
126126
# current_user.orientation = orientation
127127
# current_user.birthdate = birthdate
128128
# current_user.save()
129-
# return Success("User successfully modified !")
130129

131130

132131
@profile_edit_bp.route("/profile/edit/email", methods=["PUT"])

0 commit comments

Comments
 (0)