diff --git a/backend/project/__main__.py b/backend/project/__main__.py index 444d1410..a1bdd3bc 100644 --- a/backend/project/__main__.py +++ b/backend/project/__main__.py @@ -1,5 +1,7 @@ """Main entry point for the application.""" +import sys +sys.path.append(".") from dotenv import load_dotenv from project import create_app_with_db from project.db_in import url diff --git a/backend/project/endpoints/projects/project_assignment_file.py b/backend/project/endpoints/projects/project_assignment_file.py new file mode 100644 index 00000000..bc360c3d --- /dev/null +++ b/backend/project/endpoints/projects/project_assignment_file.py @@ -0,0 +1,32 @@ +""" +Module for getting the assignment files +of a project +""" +import os +from urllib.parse import urljoin + +from flask import jsonify, send_from_directory, send_file +from werkzeug.utils import safe_join + +from flask_restful import Resource + +from project.models.project import Project +from project.utils.query_agent import query_by_id_from_model + +API_URL = os.getenv('API_HOST') +RESPONSE_URL = urljoin(API_URL, "projects") +UPLOAD_FOLDER = os.getenv('UPLOAD_URL') + +class ProjectAssignmentFiles(Resource): + """ + Class for getting the assignment files of a project + """ + def get(self, project_id): + + project = Project.query.filter(getattr(Project, "project_id") == project_id).first() + + file_url = urljoin(f"{UPLOAD_FOLDER}", f"{project_id}") + "/" + + directory = safe_join(os.getcwd(), file_url) + + return send_from_directory(directory, project.assignment_file, as_attachment=True) \ No newline at end of file diff --git a/backend/project/endpoints/projects/project_detail.py b/backend/project/endpoints/projects/project_detail.py index 85d7b99c..fc2008aa 100644 --- a/backend/project/endpoints/projects/project_detail.py +++ b/backend/project/endpoints/projects/project_detail.py @@ -14,10 +14,10 @@ patch_by_id_from_model - API_URL = getenv('API_HOST') RESPONSE_URL = urljoin(API_URL, "projects") + class ProjectDetail(Resource): """ Class for projects/id endpoints diff --git a/backend/project/endpoints/projects/project_endpoint.py b/backend/project/endpoints/projects/project_endpoint.py index 09938878..0c4eee20 100644 --- a/backend/project/endpoints/projects/project_endpoint.py +++ b/backend/project/endpoints/projects/project_endpoint.py @@ -7,6 +7,7 @@ from project.endpoints.projects.projects import ProjectsEndpoint from project.endpoints.projects.project_detail import ProjectDetail +from project.endpoints.projects.project_assignment_file import ProjectAssignmentFiles project_bp = Blueprint('project_endpoint', __name__) @@ -19,3 +20,8 @@ '/projects/', view_func=ProjectDetail.as_view('project_detail') ) + +project_bp.add_url_rule( + '/projects//assignments', + view_func=ProjectAssignmentFiles.as_view('project_assignments') +) diff --git a/backend/project/endpoints/projects/projects.py b/backend/project/endpoints/projects/projects.py index c394a85d..88752808 100644 --- a/backend/project/endpoints/projects/projects.py +++ b/backend/project/endpoints/projects/projects.py @@ -30,7 +30,6 @@ def get(self): Get method for listing all available projects that are currently in the API """ - response_url = urljoin(API_URL, "projects") return query_selected_from_model( Project, @@ -49,6 +48,7 @@ def post(self): file = request.files["assignment_file"] project_json = parse_project_params() filename = os.path.split(file.filename)[1] + project_json["assignment_file"] = filename # save the file that is given with the request try: