Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
added functionlity for downloading files
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerwoud committed Mar 13, 2024
1 parent 7dc3923 commit 88aef97
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
2 changes: 2 additions & 0 deletions backend/project/__main__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
32 changes: 32 additions & 0 deletions backend/project/endpoints/projects/project_assignment_file.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion backend/project/endpoints/projects/project_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions backend/project/endpoints/projects/project_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -19,3 +20,8 @@
'/projects/<int:project_id>',
view_func=ProjectDetail.as_view('project_detail')
)

project_bp.add_url_rule(
'/projects/<int:project_id>/assignments',
view_func=ProjectAssignmentFiles.as_view('project_assignments')
)
2 changes: 1 addition & 1 deletion backend/project/endpoints/projects/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down

0 comments on commit 88aef97

Please sign in to comment.