User Profile Management API Implementation

Retrieving User Profile Information

This endpoint queries the user database and returns all user data to the frontend.

API Documentation

URL: /users
Method: GET

Response:
user.to_dict() # Returns all user table data as a dictionary

Business Logic

  1. Retrieve user ID from the g variable
user_id = g.current_user_id

  1. Query the database using the user ID
user_record = User.query.get(user_id)

  1. If the user exists, convert the user object to a dictionary and return to the frontand
return jsonify(error_code=RET.SUCCESS, message="Success", data=user_record.to_dict())

Setting User Avatar

API Documentation

URL: /users/avatar
Method: POST

Response:
{
  "avatar_url": "Image URL stored in Qiniu Cloud",
  "code": 200  # Indicates successful update
}

Business Logic

  1. Retrieve user ID from the g variable
user_id = g.current_user_id

  1. Get and validate the uploaded avatar image (submitted as form data)
avatar_file = request.files.get('avatar')
if avatar_file is None:
    return jsonify(error_code=RET.PARAM_ERROR, message='No image uploaded')

  1. Read the binary image data
image_data = avatar_file.read()

  1. Save the image to Qiniu Cloud, which returns a file name
file_name = storage_service.upload(image_data)

  1. Save the Qiniu Cloud URL to the database
User.query.filter_by(id=user_id).update({"avatar_url": file_name})
db.session.commit()

  1. Consturct the complete avatar URL and return to front end
full_avatar_url = constants.QINIU_DOMAIN + file_name
return jsonify(error_code=RET.SUCCESS, message='Avatar saved successfully', data={"avatar_url": full_avatar_url})

Updating Username

API Documentation

URL: /users/name
Method: PUT

Response:
{
  "code": 200,
  "name": "username"
}

Business Logic

  1. Retrieve user ID from the g variable
user_id = g.current_user_id

  1. Accept the username from the request
request_data = request.get_json()
    if not request_data:
        return jsonify(error_code=RET.PARAM_ERROR, message="Incomplete parameters")
username = request_data.get("name")  # Desired username
if not username:
    return jsonify(error_code=RET.PARAM_ERROR, message="Username cannot be empty")

  1. Save the username and check for duplicates using database unique constraint

    try:
        User.query.filter_by(id=user_id).update({"name": username})
        db.session.commit()
    except Exception as e:
        logging.error(e)
        db.session.rollback()
        return jsonify(error_code=RET.DB_ERROR, message="Username update failed")

  1. Update the name field in the session

    session["username"] = username
    return jsonify(error_code=RET.SUCCESS, message="Success", data={"name": username})

Retrieving User Verification Information

API Documentation

URL: /users/auth
Method: GET

Response:
{
  "code": 200,
  "data": user.to_dict() # Returns user verification data as dictionary
}

Business Logic

  1. Retrieve user ID from the g variable
user_id = g.current_user_id

  1. Query the user table and return all data to the frontend
user_record = User.query.get(user_id)

  1. Validate the user record

    if user_record is None:
        return jsonify(error_code=RET.NO_DATA, message="Invalid operation")

  1. Convert to dictionary and return to frontend
return jsonify(error_code=RET.SUCCESS, message="Success", data=user_record.verification_to_dict())

Saving User Verification Information

API Documentation

URL: /users/auth
Method: POST

Response:
{
  "error_code": RET.SUCCESS,
  "message": "Success"
}

Business Logic

  1. Retrieve user ID from the g variable
user_id = g.current_user_id

  1. Accept verification information from the frontend
request_data = request.get_json()
if not request_data:
   return jsonify(error_code=RET.PARAM_ERROR, message="Invalid parameters")

real_name = request_data.get("real_name")  # Real name
id_card = request_data.get("id_card")  # ID number

    # Parameter validation
if not all([real_name, id_card]):
   return jsonify(error_code=RET.PARAM_ERROR, message="Missing required parameters")

  1. Save real name and ID number to the database
 User.query.filter_by(id=user_id, real_name=None, id_card=None).update({"real_name": real_name, "id_card": id_card})
 db.session.commit()

  1. Return success response
return jsonify(error_code=RET.SUCCESS, message="Verification information saved")

Tags: API Flask SQLAlchemy User Authentication Cloud Storage

Posted on Wed, 13 May 2026 16:23:35 +0000 by cdc5205