2323from flask import Blueprint
2424from flask import request
2525from PyMatcha .errors import BadRequestError
26+ from PyMatcha .errors import NotFoundError
2627from PyMatcha .models .tag import Tag
2728from PyMatcha .success import Success
2829from PyMatcha .utils .decorators import validate_params
3334profile_bp = Blueprint ("profile" , __name__ )
3435
3536REQUIRED_PARAMS_COMPLETE_PROFILE = {"gender" : str , "birthdate" : int , "orientation" : str , "bio" : str , "tags" : list }
37+ REQUIRED_PARAMS_EDIT_PROFILE = {
38+ "first_name" : str ,
39+ "last_name" : str ,
40+ "username" : str ,
41+ "bio" : str ,
42+ "gender" : str ,
43+ "orientation" : str ,
44+ "birthdate" : int ,
45+ "tags" : list ,
46+ }
3647
3748
3849@profile_bp .route ("/profile/complete" , methods = ["POST" ])
@@ -51,12 +62,6 @@ def complete_profile():
5162 gender = data ["gender" ]
5263 birthdate = data ["birthdate" ]
5364
54- if orientation not in ["heterosexual" , "homosexual" , "bisexual" , "other" ]:
55- raise BadRequestError ("Orientation must be heterosexual, homosexual, bisexual or other" , "Try again" )
56-
57- if gender not in ["male" , "female" , "other" ]:
58- raise BadRequestError ("Gender must be male, female or other" , "Try again" )
59-
6065 for tag in tags :
6166 Tag .create (name = tag , user_id = current_user .id )
6267
@@ -67,3 +72,45 @@ def complete_profile():
6772 current_user .birthdate = datetime .date .fromtimestamp (int (birthdate ))
6873 current_user .save ()
6974 return Success ("Profile completed !" )
75+
76+
77+ @profile_bp .route ("/profile/edit" , methods = ["PUT" ])
78+ @fjwt .jwt_required
79+ @validate_params (REQUIRED_PARAMS_EDIT_PROFILE )
80+ def edit_profile ():
81+ current_user = fjwt .current_user
82+ if not current_user .is_profile_completed :
83+ raise BadRequestError ("The user has not completed his profile" , "Complete your profile and try again" )
84+ data = request .get_json ()
85+ first_name = data ["first_name" ]
86+ last_name = data ["last_name" ]
87+ username = data ["username" ]
88+ bio = data ["bio" ]
89+ gender = data ["gender" ]
90+ orientation = data ["orientation" ]
91+ birthdate = data ["birthdate" ]
92+
93+ try :
94+ get_user (username )
95+ except NotFoundError :
96+ pass
97+ else :
98+ raise BadRequestError ("Username taken" , "Try again" )
99+
100+ if orientation not in ["heterosexual" , "homosexual" , "bisexual" , "other" ]:
101+ raise BadRequestError ("Orientation must be heterosexual, homosexual, bisexual or other" , "Try again" )
102+
103+ if gender not in ["male" , "female" , "other" ]:
104+ raise BadRequestError ("Gender must be male, female or other" , "Try again" )
105+
106+ birthdate = datetime .date .fromtimestamp (birthdate )
107+
108+ current_user .first_name = first_name
109+ current_user .last_name = last_name
110+ current_user .username = username
111+ current_user .bio = bio
112+ current_user .gender = gender
113+ current_user .orientation = orientation
114+ current_user .birthdate = birthdate
115+ current_user .save ()
116+ return Success ("User successfully modified !" )
0 commit comments