update normal profile

main
Kushal Dotel 6 months ago
parent 99c0411370
commit 7a5ff71010
  1. 93
      backend/blueprints/profile/__init__.py

@ -103,39 +103,76 @@ 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:
profile_url = url_for('send_file', filename=current_user.pfpFilename, _external=True) if request.method == 'GET':
except: try:
profile_url = "" profile_url = url_for('send_file', filename=current_user.pfpFilename, _external=True)
try: except:
# Construct the user profile data profile_url = ""
profile_data = {
"id": str(current_user.id), try:
"email": current_user.email, # Construct the user profile data
"first_name": current_user.firstName, profile_data = {
"last_name": current_user.lastName, "id": str(current_user.id),
"username": current_user.username, "email": current_user.email,
"dob": current_user.dob.isoformat(), "first_name": current_user.firstName,
"joined_date": current_user.joinedDate.isoformat(), "last_name": current_user.lastName,
"last_online": current_user.lastOnline.isoformat(), "username": current_user.username,
"bio": current_user.bio, "dob": current_user.dob.isoformat() if current_user.dob else None,
"role": current_user.role, "joined_date": current_user.joinedDate.isoformat(),
"pfp_filename": current_user.pfpFilename, "last_online": current_user.lastOnline.isoformat(),
"profile_url":profile_url "bio": current_user.bio,
} "role": current_user.role,
"pfp_filename": current_user.pfpFilename,
return jsonify({"profile": profile_data}), 200 "profile_url": profile_url,
except Exception as e: }
return jsonify({"error": f"Failed to fetch profile. Error: {str(e)}"}), 500
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']) @profile.route('/update-profile-picture', methods=['PATCH'])

Loading…
Cancel
Save