ayush 6 months ago
commit 43b82377e9
  1. 51
      backend/blueprints/profile/__init__.py

@ -103,19 +103,20 @@ def register():
return jsonify({"error": "Registration failed, please try again later."}), 500 return jsonify({"error": "Registration failed, please try again later."}), 500
#make a get request to get json on hello word #make a get request to get json on hello word
@profile.route('/me', methods=['GET']) @profile.route('/me', methods=['GET', 'PUT'])
@auth_required() @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 current_user: User = g.current_user
try:
if request.method == 'GET':
try:
profile_url = url_for('send_file', filename=current_user.pfpFilename, _external=True) profile_url = url_for('send_file', filename=current_user.pfpFilename, _external=True)
except: except:
profile_url = "" profile_url = ""
try: try:
# Construct the user profile data # Construct the user profile data
profile_data = { profile_data = {
@ -124,19 +125,55 @@ def get_profile():
"first_name": current_user.firstName, "first_name": current_user.firstName,
"last_name": current_user.lastName, "last_name": current_user.lastName,
"username": current_user.username, "username": current_user.username,
"dob": current_user.dob.isoformat(), "dob": current_user.dob.isoformat() if current_user.dob else None,
"joined_date": current_user.joinedDate.isoformat(), "joined_date": current_user.joinedDate.isoformat(),
"last_online": current_user.lastOnline.isoformat(), "last_online": current_user.lastOnline.isoformat(),
"bio": current_user.bio, "bio": current_user.bio,
"role": current_user.role, "role": current_user.role,
"pfp_filename": current_user.pfpFilename, "pfp_filename": current_user.pfpFilename,
"profile_url":profile_url "profile_url": profile_url,
} }
return jsonify({"profile": profile_data}), 200 return jsonify({"profile": profile_data}), 200
except Exception as e: except Exception as e:
return jsonify({"error": f"Failed to fetch profile. Error: {str(e)}"}), 500 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']) @profile.route('/update-profile-picture', methods=['PATCH'])
@auth_required() @auth_required()

Loading…
Cancel
Save