Skip to content

Commit 6474e73

Browse files
committed
Added two PATCH routes for first and last name
1 parent 7eac293 commit 6474e73

File tree

1 file changed

+68
-41
lines changed
  • backend/PyMatcha/routes/api/profile

1 file changed

+68
-41
lines changed

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

Lines changed: 68 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,20 @@
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
2928
from PyMatcha.utils import hash_password
3029
from PyMatcha.utils.confirm_token import generate_confirmation_token
3130
from PyMatcha.utils.decorators import validate_params
3231
from PyMatcha.utils.errors import BadRequestError
33-
from PyMatcha.utils.errors import NotFoundError
3432
from PyMatcha.utils.errors import UnauthorizedError
3533
from PyMatcha.utils.mail import send_mail_html
3634
from PyMatcha.utils.mail import send_mail_text
3735
from PyMatcha.utils.password import check_password
3836
from PyMatcha.utils.static import FRONTEND_EMAIL_CONFIRMATION_URL
3937
from PyMatcha.utils.success import Success
4038

39+
# from PyMatcha.models.user import get_user
40+
# from PyMatcha.utils.errors import NotFoundError
41+
4142
profile_edit_bp = Blueprint("profile_edit", __name__)
4243

4344
REQUIRED_PARAMS_EDIT_PROFILE = {
@@ -52,54 +53,80 @@
5253
}
5354

5455

55-
@profile_edit_bp.route("/profile/edit", methods=["PUT"])
56-
@validate_params(REQUIRED_PARAMS_EDIT_PROFILE)
56+
@profile_edit_bp.route("/profile/edit/first_name", methods=["PATCH"])
57+
@validate_params({"first_name": str})
5758
@jwt_required
58-
def edit_profile():
59+
def edit_profile_first_name():
5960
if not current_user.is_profile_completed:
6061
raise BadRequestError("The user has not completed his profile.", "Complete your profile and try again.")
6162
data = request.get_json()
6263
first_name = data["first_name"]
63-
last_name = data["last_name"]
64-
username = data["username"]
65-
bio = data["bio"]
66-
gender = data["gender"]
67-
orientation = data["orientation"]
68-
birthdate = data["birthdate"]
69-
70-
try:
71-
birthdate = datetime.datetime.strptime(birthdate, "%d/%m/%Y").date()
72-
except ValueError:
73-
raise BadRequestError("Birthdate format must be %d/%m/%Y (day/month/year).")
74-
75-
today = datetime.datetime.utcnow()
76-
77-
age = today.year - birthdate.year - ((today.month, today.day) < (birthdate.month, birthdate.day))
78-
if age < 18:
79-
raise BadRequestError("You must be 18 years old or older.")
80-
81-
try:
82-
get_user(username)
83-
except NotFoundError:
84-
pass
85-
else:
86-
raise BadRequestError("Username taken.")
87-
88-
if orientation not in ["heterosexual", "homosexual", "bisexual", "other"]:
89-
raise BadRequestError("Orientation must be heterosexual, homosexual, bisexual or other.")
64+
current_user.first_name = first_name
65+
current_user.save()
66+
return Success("First name successfully modified!")
9067

91-
if gender not in ["male", "female", "other"]:
92-
raise BadRequestError("Gender must be male, female or other.")
9368

69+
@profile_edit_bp.route("/profile/edit/first_name", methods=["PATCH"])
70+
@validate_params({"first_name": str})
71+
@jwt_required
72+
def edit_profile_last_name():
73+
if not current_user.is_profile_completed:
74+
raise BadRequestError("The user has not completed his profile.", "Complete your profile and try again.")
75+
data = request.get_json()
76+
first_name = data["first_name"]
9477
current_user.first_name = first_name
95-
current_user.last_name = last_name
96-
current_user.username = username
97-
current_user.bio = bio
98-
current_user.gender = gender
99-
current_user.orientation = orientation
100-
current_user.birthdate = birthdate
10178
current_user.save()
102-
return Success("User successfully modified !")
79+
return Success("First name successfully modified!")
80+
81+
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"]
92+
# bio = data["bio"]
93+
# gender = data["gender"]
94+
# orientation = data["orientation"]
95+
# birthdate = data["birthdate"]
96+
#
97+
# try:
98+
# birthdate = datetime.datetime.strptime(birthdate, "%d/%m/%Y").date()
99+
# except ValueError:
100+
# raise BadRequestError("Birthdate format must be %d/%m/%Y (day/month/year).")
101+
#
102+
# today = datetime.datetime.utcnow()
103+
#
104+
# age = today.year - birthdate.year - ((today.month, today.day) < (birthdate.month, birthdate.day))
105+
# if age < 18:
106+
# raise BadRequestError("You must be 18 years old or older.")
107+
#
108+
# try:
109+
# get_user(username)
110+
# except NotFoundError:
111+
# pass
112+
# else:
113+
# raise BadRequestError("Username taken.")
114+
#
115+
# if orientation not in ["heterosexual", "homosexual", "bisexual", "other"]:
116+
# raise BadRequestError("Orientation must be heterosexual, homosexual, bisexual or other.")
117+
#
118+
# if gender not in ["male", "female", "other"]:
119+
# raise BadRequestError("Gender must be male, female or other.")
120+
#
121+
# current_user.first_name = first_name
122+
# current_user.last_name = last_name
123+
# current_user.username = username
124+
# current_user.bio = bio
125+
# current_user.gender = gender
126+
# current_user.orientation = orientation
127+
# current_user.birthdate = birthdate
128+
# current_user.save()
129+
# return Success("User successfully modified !")
103130

104131

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

0 commit comments

Comments
 (0)