Skip to content

Commit 024ab4d

Browse files
committed
Added tags, orientation and birthdate patch routes
1 parent 567b302 commit 024ab4d

File tree

1 file changed

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

1 file changed

+53
-27
lines changed

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

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
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.tag import Tag
2829
from PyMatcha.models.user import get_user
2930
from PyMatcha.utils import hash_password
3031
from PyMatcha.utils.confirm_token import generate_confirmation_token
@@ -116,33 +117,58 @@ def edit_profile_gender():
116117
return Success("Gender successfully modified!")
117118

118119

119-
# gender = data["gender"]
120-
# orientation = data["orientation"]
121-
# birthdate = data["birthdate"]
122-
# TAGS
123-
#
124-
# try:
125-
# birthdate = datetime.datetime.strptime(birthdate, "%d/%m/%Y").date()
126-
# except ValueError:
127-
# raise BadRequestError("Birthdate format must be %d/%m/%Y (day/month/year).")
128-
#
129-
# today = datetime.datetime.utcnow()
130-
#
131-
# age = today.year - birthdate.year - ((today.month, today.day) < (birthdate.month, birthdate.day))
132-
# if age < 18:
133-
# raise BadRequestError("You must be 18 years old or older.")
134-
#
135-
136-
#
137-
# if orientation not in ["heterosexual", "homosexual", "bisexual", "other"]:
138-
# raise BadRequestError("Orientation must be heterosexual, homosexual, bisexual or other.")
139-
#
140-
141-
#
142-
# current_user.gender = gender
143-
# current_user.orientation = orientation
144-
# current_user.birthdate = birthdate
145-
# current_user.save()
120+
@profile_edit_bp.route("/profile/edit/orientation", methods=["PATCH"])
121+
@validate_params({"orientation": str})
122+
@jwt_required
123+
def edit_profile_orientation():
124+
if not current_user.is_profile_completed:
125+
raise BadRequestError("The user has not completed his profile.", "Complete your profile and try again.")
126+
data = request.get_json()
127+
orientation = data["orientation"]
128+
if orientation not in ["heterosexual", "homosexual", "bisexual", "other"]:
129+
raise BadRequestError("Orientation must be heterosexual, homosexual, bisexual or other.")
130+
current_user.orientation = orientation
131+
current_user.save()
132+
return Success("Orientation successfully modified!")
133+
134+
135+
@profile_edit_bp.route("/profile/edit/birthdate", methods=["PATCH"])
136+
@validate_params({"birthdate": str})
137+
@jwt_required
138+
def edit_profile_birthdate():
139+
if not current_user.is_profile_completed:
140+
raise BadRequestError("The user has not completed his profile.", "Complete your profile and try again.")
141+
data = request.get_json()
142+
birthdate = data["birthdate"]
143+
try:
144+
birthdate = datetime.datetime.strptime(birthdate, "%d/%m/%Y").date()
145+
except ValueError:
146+
raise BadRequestError("Birthdate format must be %d/%m/%Y (day/month/year).")
147+
148+
today = datetime.datetime.utcnow()
149+
150+
age = today.year - birthdate.year - ((today.month, today.day) < (birthdate.month, birthdate.day))
151+
if age < 18:
152+
raise BadRequestError("You must be 18 years old or older.")
153+
current_user.birthdate = birthdate
154+
current_user.save()
155+
return Success("Birthdate successfully modified!")
156+
157+
158+
@profile_edit_bp.route("/profile/edit/tags", methods=["PATCH"])
159+
@validate_params({"tags": list})
160+
@jwt_required
161+
def edit_profile_tags():
162+
if not current_user.is_profile_completed:
163+
raise BadRequestError("The user has not completed his profile.", "Complete your profile and try again.")
164+
data = request.get_json()
165+
tags = data["tags"]
166+
current_tags = Tag.get_multis(user_id=current_user.id)
167+
for t in current_tags:
168+
t.delete()
169+
for tag in tags:
170+
Tag.create(name=tag, user_id=current_user.id)
171+
return Success("Tags successfully modified!")
146172

147173

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

0 commit comments

Comments
 (0)