2626from flask_jwt_extended import current_user
2727from flask_jwt_extended import jwt_required
2828from ip2geotools .databases .noncommercial import DbIpCity
29- from PyMatcha .models .report import Report
30- from PyMatcha .models .tag import Tag
3129from PyMatcha .models .user import get_user
32- from PyMatcha .models .view import View
3330from PyMatcha .utils import hash_password
3431from PyMatcha .utils .confirm_token import generate_confirmation_token
3532from PyMatcha .utils .decorators import validate_params
3936from PyMatcha .utils .mail import send_mail_html
4037from PyMatcha .utils .mail import send_mail_text
4138from PyMatcha .utils .success import Success
42- from PyMatcha .utils .success import SuccessOutput
4339
44- profile_bp = Blueprint ("profile " , __name__ )
40+ profile_edit_bp = Blueprint ("profile_edit " , __name__ )
4541
46- REQUIRED_PARAMS_COMPLETE_PROFILE = {"gender" : str , "birthdate" : str , "orientation" : str , "bio" : str , "tags" : list }
4742REQUIRED_PARAMS_EDIT_PROFILE = {
4843 "first_name" : str ,
4944 "last_name" : str ,
5651}
5752
5853
59- @profile_bp .route ("/profile/complete" , methods = ["POST" ])
60- @jwt_required
61- @validate_params (REQUIRED_PARAMS_COMPLETE_PROFILE )
62- def complete_profile ():
63- if current_user .is_profile_completed :
64- raise BadRequestError (
65- "The user has already completed his profile" , "Go to your profile settings to edit your profile"
66- )
67- data = request .get_json ()
68- orientation = data ["orientation" ]
69- bio = data ["bio" ]
70- tags = data ["tags" ]
71- gender = data ["gender" ]
72- birthdate = data ["birthdate" ]
73-
74- try :
75- birthdate = datetime .datetime .strptime (birthdate , "%d/%m/%Y" ).date ()
76- except ValueError :
77- raise BadRequestError ("Birthdate format must be %d/%m/%Y (day/month/year)" , "Try again" )
78-
79- if len (bio ) <= 50 :
80- raise BadRequestError ("Bio is too short" , "Try again" )
81-
82- if len (tags ) < 3 :
83- raise BadRequestError ("At least 3 tags are required" , "Try again" )
84-
85- if len (tags ) != len (set (tags )):
86- raise BadRequestError ("Duplicate tags" , "Try again" )
87-
88- today = datetime .datetime .utcnow ()
89-
90- age = today .year - birthdate .year - ((today .month , today .day ) < (birthdate .month , birthdate .day ))
91- if age < 18 :
92- raise BadRequestError ("You must be 18 years old or older" , "Try again later" )
93-
94- for tag in tags :
95- Tag .create (name = tag , user_id = current_user .id )
96-
97- current_user .orientation = orientation
98- current_user .bio = bio
99- current_user .is_profile_completed = True
100- current_user .gender = gender
101- current_user .birthdate = birthdate
102- current_user .save ()
103- return Success ("Profile completed !" )
104-
105-
106- @profile_bp .route ("/profile/edit" , methods = ["PUT" ])
54+ @profile_edit_bp .route ("/profile/edit" , methods = ["PUT" ])
10755@jwt_required
10856@validate_params (REQUIRED_PARAMS_EDIT_PROFILE )
10957def edit_profile ():
@@ -153,7 +101,7 @@ def edit_profile():
153101 return Success ("User successfully modified !" )
154102
155103
156- @profile_bp .route ("/profile/edit/email" , methods = ["PUT" ])
104+ @profile_edit_bp .route ("/profile/edit/email" , methods = ["PUT" ])
157105@jwt_required
158106@validate_params ({"email" : str })
159107def edit_email ():
@@ -171,7 +119,7 @@ def edit_email():
171119 return Success ("Email sent for new email" )
172120
173121
174- @profile_bp .route ("/profile/edit/password" , methods = ["PUT" ])
122+ @profile_edit_bp .route ("/profile/edit/password" , methods = ["PUT" ])
175123@jwt_required
176124@validate_params ({"old_password" : str , "new_password" : str })
177125def edit_password ():
@@ -191,7 +139,7 @@ def edit_password():
191139 return Success ("User password successfully updated." )
192140
193141
194- @profile_bp .route ("/profile/edit/geolocation" , methods = ["PUT" ])
142+ @profile_edit_bp .route ("/profile/edit/geolocation" , methods = ["PUT" ])
195143@jwt_required
196144@validate_params ({"ip" : str }, {"lat" : float , "lng" : float })
197145def edit_geolocation ():
@@ -213,50 +161,3 @@ def edit_geolocation():
213161 current_user .geohash = Geohash .encode (lat , lng )
214162 current_user .save ()
215163 return Success ("New location sucessfully saved." )
216-
217-
218- @profile_bp .route ("/profile/views" , methods = ["GET" ])
219- @jwt_required
220- def get_profile_views ():
221- profile_views = current_user .get_views ()
222- profile_views = [v .to_dict () for v in profile_views ]
223- return SuccessOutput ("views" , profile_views )
224-
225-
226- @profile_bp .route ("/profile/view/<uid>" , methods = ["GET" ])
227- @jwt_required
228- def view_profile (uid ):
229- try :
230- u = get_user (uid )
231- except NotFoundError :
232- raise NotFoundError (f"User { uid } not found" , "try again" )
233-
234- if current_user .id != u .id :
235- View .create (profile_id = u .id , viewer_id = current_user .id )
236-
237- return SuccessOutput ("profile" , u .to_dict ())
238-
239-
240- @profile_bp .route ("/profile/report/<uid>" , methods = ["POST" ])
241- @validate_params ({"reason" : str }, {"details" : str })
242- @jwt_required
243- def report_profile (uid ):
244- data = request .get_json ()
245- reason = data ["reason" ]
246-
247- if reason not in ["harassment" , "bot" , "spam" , "inappropriate content" ]:
248- raise BadRequestError ("Reason must be 'harassment', 'bot', 'spam' or 'inappropriate content'" , "Try again" )
249-
250- try :
251- details = data ["details" ]
252- except KeyError :
253- details = None
254- try :
255- u = get_user (uid )
256- except NotFoundError :
257- raise NotFoundError (f"User { uid } not found" , "try again" )
258- if current_user .id == u .id :
259- raise BadRequestError ("Cannot report yourself" , "Try again" )
260- Report .create (reporter_id = current_user .id , reported_id = u .id , reason = reason , details = details )
261-
262- return Success (f"Report created on user { u .email } " )
0 commit comments