From af4801a27a2254d85d848157cdd80abe73ef5908 Mon Sep 17 00:00:00 2001 From: Kushal Dotel Date: Sat, 11 Jan 2025 21:31:24 +0545 Subject: [PATCH] feat: add user profile --- backend/app.py | 5 +++- backend/blueprints/profile/__init__.py | 41 ++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/backend/app.py b/backend/app.py index 92afd4e..aa9c5cb 100644 --- a/backend/app.py +++ b/backend/app.py @@ -1,5 +1,5 @@ from sys import prefix -from flask import Flask +from flask import Flask, send_from_directory # from FreeBug.backend.blueprints import notification from db.model import db from config import * @@ -27,6 +27,9 @@ db.init_app(app) app.register_blueprint(profileBlueprint, url_prefix='/api/profile') app.register_blueprint(sessionBlueprint,url_prefix='/api/session') +@app.route('/media/') +def send_file(filename): + return send_from_directory(USER_UPLOADS_DIR, filename) @app.route('/', methods=['GET', 'POST']) def homepage(): diff --git a/backend/blueprints/profile/__init__.py b/backend/blueprints/profile/__init__.py index 5507f0d..fd01f4b 100644 --- a/backend/blueprints/profile/__init__.py +++ b/backend/blueprints/profile/__init__.py @@ -1,5 +1,5 @@ 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 datetime import datetime from utils.auth import auth_required, requires_role @@ -12,7 +12,7 @@ import os from config import * from utils.utils import password_check_sanity,is_valid_email,InsecurePasswordException from sqlalchemy.exc import IntegrityError - +# from flask import url_for profile = Blueprint('profile', __name__) # Function to check allowed file extensions @@ -103,8 +103,37 @@ def register(): return jsonify({"error": "Registration failed, please try again later."}), 500 #make a get request to get json on hello word -@profile.route('/hello') +@profile.route('/me', methods=['GET']) @auth_required() -@requires_role([UserRole.USER]) -def hello(): - return jsonify({"message": f"Hello {g.current_user.firstName}"}), 200 +def get_profile(): + """ + 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 +