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

Creates project display for students and solves issue 186 #187

Merged
merged 13 commits into from
Apr 10, 2024
27 changes: 14 additions & 13 deletions backend/project/endpoints/submissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from project.models.submission import Submission, SubmissionStatus
from project.models.project import Project
from project.models.user import User
from project.utils.files import filter_files, all_files_uploaded
from project.utils.files import all_files_uploaded
from project.utils.user import is_valid_user
from project.utils.project import is_valid_project
from project.utils.query_agent import query_selected_from_model, delete_by_id_from_model
Expand Down Expand Up @@ -112,11 +112,12 @@ def post(self) -> dict[str, any]:

# Submission files
submission.submission_path = "" # Must be set on creation
files = filter_files(request.files.getlist("files"))
files = request.files.getlist("files")

# Check files otherwise stop
project = session.get(Project, submission.project_id)
if not files or not all_files_uploaded(files, project.regex_expressions):
if project.regex_expressions and \
(not files or not all_files_uploaded(files, project.regex_expressions)):
data["message"] = "No files were uploaded" if not files else \
"Not all required files were uploaded " \
f"(required files={','.join(project.regex_expressions)})"
Expand All @@ -141,12 +142,12 @@ def post(self) -> dict[str, any]:
data["message"] = "Successfully fetched the submissions"
data["url"] = urljoin(f"{API_HOST}/", f"/submissions/{submission.submission_id}")
data["data"] = {
"id": urljoin(f"{BASE_URL}/", submission.submission_id),
"user": urljoin(f"{API_HOST}/", f"/users/{submission.uid}"),
"project": urljoin(f"{API_HOST}/", f"/projects/{submission.project_id}"),
"submission_id": urljoin(f"{BASE_URL}/", str(submission.submission_id)),
"uid": urljoin(f"{API_HOST}/", f"/users/{submission.uid}"),
"project_id": urljoin(f"{API_HOST}/", f"/projects/{submission.project_id}"),
"grading": submission.grading,
"time": submission.submission_time,
"status": submission.submission_status
"submission_time": submission.submission_time,
"submission_status": submission.submission_status
}
return data, 201

Expand Down Expand Up @@ -182,12 +183,12 @@ def get(self, submission_id: int) -> dict[str, any]:

data["message"] = "Successfully fetched the submission"
data["data"] = {
"id": urljoin(f"{BASE_URL}/", str(submission.submission_id)),
"user": urljoin(f"{API_HOST}/", f"/users/{submission.uid}"),
"project": urljoin(f"{API_HOST}/", f"/projects/{submission.project_id}"),
"submission_id": urljoin(f"{BASE_URL}/", str(submission.submission_id)),
"uid": urljoin(f"{API_HOST}/", f"/users/{submission.uid}"),
"project_id": urljoin(f"{API_HOST}/", f"/projects/{submission.project_id}"),
"grading": submission.grading,
"time": submission.submission_time,
"status": submission.submission_status
"submission_time": submission.submission_time,
"submission_status": submission.submission_status
}
return data, 200

Expand Down
14 changes: 0 additions & 14 deletions backend/project/utils/files.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
"""Utility functions for files"""

from os.path import getsize
from re import match
from typing import List, Optional
from io import BytesIO
from zipfile import ZipFile, ZIP_DEFLATED, is_zipfile
from werkzeug.utils import secure_filename
from werkzeug.datastructures import FileStorage

def filter_files(files: List[FileStorage]) -> List[FileStorage]:
"""Filter out bad files

Args:
files (List[FileStorage]): A list of files to filter on

Returns:
List[FileStorage]: The filtered list
"""
return list(filter(lambda file:
file and file.filename != "" and getsize(file.filename) > 0,
files
))

def all_files_uploaded(files: List[FileStorage], regexes: List[str]) -> bool:
"""Check if all the required files are uploaded
Expand Down
10 changes: 5 additions & 5 deletions backend/tests/endpoints/submissions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ def test_get_submission_correct(self, client: FlaskClient, session: Session):
assert response.status_code == 200
assert data["message"] == "Successfully fetched the submission"
assert data["data"] == {
"id": f"{API_HOST}/submissions/{submission.submission_id}",
"user": f"{API_HOST}/users/student01",
"project": f"{API_HOST}/projects/{project.project_id}",
"submission_id": f"{API_HOST}/submissions/{submission.submission_id}",
"uid": f"{API_HOST}/users/student01",
"project_id": f"{API_HOST}/projects/{project.project_id}",
"grading": 16,
"time": "Thu, 14 Mar 2024 12:00:00 GMT",
"status": 'SUCCESS'
"submission_time": "Thu, 14 Mar 2024 12:00:00 GMT",
"submission_status": 'SUCCESS'
}

### PATCH SUBMISSION ###
Expand Down
Loading