feat: add user profile

manzilcheck
Kushal Dotel 6 months ago
parent 7bb6b0b8c7
commit af4801a27a
  1. 5
      backend/app.py
  2. 41
      backend/blueprints/profile/__init__.py

@ -1,5 +1,5 @@
from sys import prefix from sys import prefix
from flask import Flask from flask import Flask, send_from_directory
# from FreeBug.backend.blueprints import notification # from FreeBug.backend.blueprints import notification
from db.model import db from db.model import db
from config import * from config import *
@ -27,6 +27,9 @@ db.init_app(app)
app.register_blueprint(profileBlueprint, url_prefix='/api/profile') app.register_blueprint(profileBlueprint, url_prefix='/api/profile')
app.register_blueprint(sessionBlueprint,url_prefix='/api/session') app.register_blueprint(sessionBlueprint,url_prefix='/api/session')
@app.route('/media/<string:filename>')
def send_file(filename):
return send_from_directory(USER_UPLOADS_DIR, filename)
@app.route('/', methods=['GET', 'POST']) @app.route('/', methods=['GET', 'POST'])
def homepage(): def homepage():

@ -1,5 +1,5 @@
from email.policy import default from email.policy import default
from flask import Blueprint, request, jsonify, current_app, g from flask import Blueprint, request, jsonify, current_app, g,url_for
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
from datetime import datetime from datetime import datetime
from utils.auth import auth_required, requires_role from utils.auth import auth_required, requires_role
@ -12,7 +12,7 @@ import os
from config import * from config import *
from utils.utils import password_check_sanity,is_valid_email,InsecurePasswordException from utils.utils import password_check_sanity,is_valid_email,InsecurePasswordException
from sqlalchemy.exc import IntegrityError from sqlalchemy.exc import IntegrityError
# from flask import url_for
profile = Blueprint('profile', __name__) profile = Blueprint('profile', __name__)
# Function to check allowed file extensions # Function to check allowed file extensions
@ -103,8 +103,37 @@ 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('/hello') @profile.route('/me', methods=['GET'])
@auth_required() @auth_required()
@requires_role([UserRole.USER]) def get_profile():
def hello(): """
return jsonify({"message": f"Hello {g.current_user.firstName}"}), 200 Fetch the profile details of the currently logged-in user.
"""
# 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

Loading…
Cancel
Save