Commit 891a3a87 authored by Anthony Jacob's avatar Anthony Jacob
Browse files

add resume endpoint to manage pdf resume

parent fd968e2a
Loading
Loading
Loading
Loading
+140 −0
Original line number Diff line number Diff line
from datetime import date, datetime
import os
import traceback
from flask import Blueprint, request, jsonify, current_app
from app.helpers.file import deleteFile, get_unique_filename
from helpers.security import require_auth
from helpers.limiter import limiter
from model.language.language import isLanguageCodeExists

resume_bp = Blueprint("resume", __name__)


@resume_bp.route("/resumes/full", methods=["GET"])
@require_auth
@limiter.limit("10/second", override_defaults=False)
def get_resume_full():
    try:
         resumes = []
         file_path_fr = os.path.join(
                current_app.config["FILE_DIR"], "Anthony_JACOB_CV.pdf"
            )

         file_path_en = os.path.join(
                current_app.config["FILE_DIR"], "Anthony_JACOB_Resume.pdf"
            )

         if os.path.isfile(file_path_fr):
            resumes.append({
                "language_code": "fr",
                "file": "Anthony_JACOB_CV.pdf"
            })
         if os.path.isfile(file_path_en):
            resumes.append({
                "language_code": "en",
                "file": "Anthony_JACOB_Resume.pdf"
            })

         return jsonify({"resumes": resumes}), 200

    except Exception as e:
        return jsonify({"error": str(e)}), 500

@resume_bp.route("/resumes/<string:language_code>", methods=["DELETE"])
@require_auth
@limiter.limit("10/second", override_defaults=False)
def delete_resume(language_code):
    try:
        if not language_code:
            return jsonify({"error": "Missing Resume language_code"}), 400
        elif not isLanguageCodeExists(language_code):
            return jsonify({"error": "language_code is not existing"}), 400
        elif language_code not in ["fr", "en"]:
            return jsonify({"error": "language_code is not supported for now"}), 400
        else:

            fileDeleted = True
            if(language_code == "fr"):
                ExistingResume = "Anthony_JACOB_CV.pdf"
            else:
                ExistingResume = "Anthony_JACOB_Resume.pdf"


            fileDeleted = deleteFile(
                os.path.join(
                    current_app.config["FILE_DIR"], ExistingResume
                )
            )

            if fileDeleted:
                return (
                    jsonify(
                        {
                            "message": "Resume deleted",
                            "id": ExistingResume,
                        }
                    ),
                    200,
                )
            else:
                 return jsonify({"error": "Failed to delete resume logo"}), 500

    except Exception as e:
        return jsonify({"error": str(e)}), 500


@resume_bp.route("/resumes/<string:language_code>", methods=["POST"])
@require_auth
@limiter.limit("10/second", override_defaults=False)
def upload_resume(language_code):
    try:
        if not language_code:
            return jsonify({"error": "Missing Resume language_code"}), 400
        elif not isLanguageCodeExists(language_code):
            return jsonify({"error": "language_code is not existing"}), 400
        elif language_code not in ["fr", "en"]:
            return jsonify({"error": "language_code is not supported for now"}), 400
        else:
            file = request.files["file"]
            # Check if the file is a PDF
            if file.mimetype != "application/pdf":
                return jsonify({"error": "The uploaded file must be a PDF"}), 400

            if(language_code == "fr"):
                ExistingResume = "Anthony_JACOB_CV.pdf"
            else:
                ExistingResume = "Anthony_JACOB_Resume.pdf"


            file_path = os.path.join(
                current_app.config["FILE_DIR"], ExistingResume
            )
            file.save(file_path)


            return (
                jsonify(
                    {
                        "message": "Resume updated",
                        "id": ExistingResume,
                    }
                ),
                200,
            )


    except Exception as e:
        # Capture full traceback for debugging
        error_details = traceback.format_exc()

        return (
            jsonify(
                {
                    "error": f"""
                    - Exception: {type(e).__name__} - {str(e)}
                                - Traceback: {error_details}
                                """
                }
            ),
            500,
        )
+3 −1
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ from redis import Redis
from flask_jwt_extended import JWTManager
from app.helpers.db import db_cursor
from config import Config
from controller import diploma, healthcheck, auth, language, keyword, experience, about, service, email, user, apikey
from controller import diploma, healthcheck, auth, language, keyword, experience, about, service, email, user, apikey, resume
from helpers.limiter import limiter
from model.security import is_jwt_revoked

@@ -84,6 +84,8 @@ app.register_blueprint(user.user_bp)

app.register_blueprint(apikey.api_key_bp)

app.register_blueprint(resume.resume_bp)


if __name__ == "__main__":
    app.run(debug=True)