From 7a5ff710101b3662e6d18c29a6f19aa63f5e7aab Mon Sep 17 00:00:00 2001 From: Kushal Dotel Date: Sat, 11 Jan 2025 22:34:37 +0545 Subject: [PATCH] update normal profile --- backend/blueprints/profile/__init__.py | 93 ++++++++++++++++++-------- 1 file changed, 65 insertions(+), 28 deletions(-) diff --git a/backend/blueprints/profile/__init__.py b/backend/blueprints/profile/__init__.py index 4dbd24c..2d2dee0 100644 --- a/backend/blueprints/profile/__init__.py +++ b/backend/blueprints/profile/__init__.py @@ -103,39 +103,76 @@ def register(): return jsonify({"error": "Registration failed, please try again later."}), 500 #make a get request to get json on hello word -@profile.route('/me', methods=['GET']) +@profile.route('/me', methods=['GET', 'PUT']) @auth_required() -def get_profile(): +def manage_profile(): """ - Fetch the profile details of the currently logged-in user. + Handle GET and PUT requests for the logged-in user's profile. """ - # Get the current user from the `@auth_required` decorator current_user: User = g.current_user - try: - profile_url = url_for('send_file', filename=current_user.pfpFilename, _external=True) - except: - profile_url = "" - try: - # Construct the user profile data - profile_data = { - "id": str(current_user.id), - "email": current_user.email, - "first_name": current_user.firstName, - "last_name": current_user.lastName, - "username": current_user.username, - "dob": current_user.dob.isoformat(), - "joined_date": current_user.joinedDate.isoformat(), - "last_online": current_user.lastOnline.isoformat(), - "bio": current_user.bio, - "role": current_user.role, - "pfp_filename": current_user.pfpFilename, - "profile_url":profile_url - } - - return jsonify({"profile": profile_data}), 200 - except Exception as e: - return jsonify({"error": f"Failed to fetch profile. Error: {str(e)}"}), 500 + if request.method == 'GET': + try: + profile_url = url_for('send_file', filename=current_user.pfpFilename, _external=True) + except: + profile_url = "" + + try: + # Construct the user profile data + profile_data = { + "id": str(current_user.id), + "email": current_user.email, + "first_name": current_user.firstName, + "last_name": current_user.lastName, + "username": current_user.username, + "dob": current_user.dob.isoformat() if current_user.dob else None, + "joined_date": current_user.joinedDate.isoformat(), + "last_online": current_user.lastOnline.isoformat(), + "bio": current_user.bio, + "role": current_user.role, + "pfp_filename": current_user.pfpFilename, + "profile_url": profile_url, + } + + return jsonify({"profile": profile_data}), 200 + except Exception as e: + return jsonify({"error": f"Failed to fetch profile. Error: {str(e)}"}), 500 + + elif request.method == 'PUT': + # Update the user's profile using form data + try: + # email = request.form.get('email') + first_name = request.form.get('first_name') + last_name = request.form.get('last_name') + username = request.form.get('username') + dob = request.form.get('dob') + bio = request.form.get('bio') + + # Update fields if provided + # if email: + # current_user.email = email + if first_name: + current_user.firstName = first_name + if last_name: + current_user.lastName = last_name + if username: + current_user.username = username + if dob: + current_user.dob = dob # Ensure the date format is validated + if bio: + current_user.bio = bio + + # Commit changes to the database + db.session.commit() + + return jsonify({"message": "Profile updated successfully."}), 200 + except IntegrityError: + db.session.rollback() + return jsonify({"error": "Username must be unique. Please choose another."}), 400 + except Exception as e: + db.session.rollback() + return jsonify({"error": f"Failed to update profile. Error: {str(e)}"}), 500 + @profile.route('/update-profile-picture', methods=['PATCH'])