Skip to content

Commit

Permalink
Merge branch 'development' into fix/sub_status
Browse files Browse the repository at this point in the history
  • Loading branch information
warreprovoost authored May 4, 2024
2 parents f8b5ff3 + 3f1cef2 commit 33b645b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 33 deletions.
14 changes: 6 additions & 8 deletions backend/project/endpoints/submissions/submission_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from sqlalchemy import exc
from project.db_in import db
from project.models.submission import Submission
from project.utils.models.submission_utils import submission_response
from project.utils.authentication import (
authorize_submission_request,
authorize_grader
Expand Down Expand Up @@ -87,6 +88,10 @@ def patch(self, submission_id:int) -> dict[str, any]:
return data, 404

# Update the grading field
if set(request.form.keys()) - {"grading"}:
data["message"] = "Invalid data field given, only 'grading' is allowed"
return data, 400

grading = request.form.get("grading")
if grading is not None:
try:
Expand All @@ -105,14 +110,7 @@ def patch(self, submission_id:int) -> dict[str, any]:

data["message"] = f"Submission (submission_id={submission_id}) patched"
data["url"] = urljoin(f"{BASE_URL}/", str(submission.submission_id))
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}"),
"grading": submission.grading,
"time": submission.submission_time,
"status": submission.submission_status
}
data["data"] = submission_response(submission, API_HOST)
return data, 200

except exc.SQLAlchemyError:
Expand Down
28 changes: 14 additions & 14 deletions backend/project/endpoints/submissions/submissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
from project.db_in import db
from project.models.submission import Submission, SubmissionStatus
from project.models.project import Project
from project.models.user import User
from project.models.course import Course
from project.models.course_relation import CourseAdmin
from project.utils.files import all_files_uploaded
from project.utils.project import is_valid_project
from project.utils.authentication import authorize_student_submission, login_required_return_uid
from project.utils.submissions.evaluator import run_evaluator
from project.utils.models.project_utils import get_course_of_project

from project.utils.models.submission_utils import submission_response

API_HOST = getenv("API_HOST")
UPLOAD_FOLDER = getenv("UPLOAD_FOLDER")
Expand All @@ -45,17 +44,21 @@ def get(self, uid=None) -> dict[str, any]:
}
filters = dict(request.args)
try:
invalid_parameters = set(filters.keys()) - {"uid", "project_id"}
if invalid_parameters:
data["message"] = f"Invalid query parameter(s) {invalid_parameters}"
return data, 400

# Check the uid query parameter
user_id = filters.get("uid")
if user_id and not User.query.filter_by(uid=user_id).all():
if user_id and not isinstance(user_id, str):
data["message"] = f"Invalid user (uid={user_id})"
return data, 400

# Check the project_id query parameter
project_id = filters.get("project_id")
if project_id:
if not project_id.isdigit() or \
not Project.query.filter_by(project_id=project_id).all():
if not project_id.isdigit():
data["message"] = f"Invalid project (project_id={project_id})"
return data, 400
filters["project_id"] = int(project_id)
Expand Down Expand Up @@ -109,6 +112,10 @@ def post(self, uid=None) -> dict[str, any]:
"url": BASE_URL
}
try:
if set(request.form.keys()) - {"project_id", "files"}:
data["message"] = "Invalid data fields, only 'project_id' and 'files' are allowed"
return data, 400

with db.session() as session:
submission = Submission()

Expand Down Expand Up @@ -180,15 +187,8 @@ def post(self, uid=None) -> dict[str, any]:

data["message"] = "Successfully fetched the submissions"
data["url"] = urljoin(f"{API_HOST}/", f"/submissions/{submission.submission_id}")
data["data"] = {
"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,
"submission_time": submission.submission_time,
"submission_status": submission.submission_status
}
return data, 202
data["data"] = submission_response(submission, API_HOST)
return data, 200

except exc.SQLAlchemyError:
session.rollback()
Expand Down
11 changes: 11 additions & 0 deletions backend/project/utils/models/submission_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@ def get_course_of_submission(submission_id):
"""Get the course linked to a given submission"""
submission = get_submission(submission_id)
return get_course_of_project(submission.project_id)

def submission_response(submission, api_host):
"""Return the response data for a submission"""
return {
"submission_id": f"{api_host}/submissions/{submission.submission_id}",
"uid": f"{api_host}/users/{submission.uid}",
"project_id": f"{api_host}/projects/{submission.project_id}",
"grading": submission.grading,
"submission_time": submission.submission_time,
"submission_status": submission.submission_status
}
25 changes: 18 additions & 7 deletions backend/tests/endpoints/submissions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ def test_get_submissions_wrong_user(self, client: FlaskClient):
"""Test getting submissions for a non-existing user"""
csrf = get_csrf_from_login(client, "teacher")
response = client.get("/submissions?uid=-20", headers = {"X-CSRF-TOKEN":csrf})
assert response.status_code == 400
assert response.status_code == 200
assert response.json["data"] == []

def test_get_submissions_wrong_project(self, client: FlaskClient):
"""Test getting submissions for a non-existing project"""
csrf = get_csrf_from_login(client, "teacher")
response = client.get("/submissions?project_id=123456789",
headers = {"X-CSRF-TOKEN":csrf})
assert response.status_code == 400
assert response.status_code == 200
assert response.json["data"] == []
assert "message" in response.json

def test_get_submissions_wrong_project_type(self, client: FlaskClient):
Expand All @@ -43,6 +45,15 @@ def test_get_submissions_project(self, client: FlaskClient, valid_submission_ent
assert response.status_code == 200
assert "message" in data

def test_get_submission_wrong_parameter(self, client: FlaskClient):
"""Test a submission filtering on a non existing parameter"""
response = client.get(
"/submissions?parameter=0",
headers = {"X-CSRF-TOKEN":get_csrf_from_login(client, "teacher")}
)
assert response.status_code == 400



### GET SUBMISSION ###
def test_get_submission_wrong_id(self, client: FlaskClient, session: Session):
Expand Down Expand Up @@ -127,10 +138,10 @@ def test_patch_submission_correct_teacher(self, client: FlaskClient, session: Se
assert data["message"] == f"Submission (submission_id={submission.submission_id}) patched"
assert data["url"] == f"{API_HOST}/submissions/{submission.submission_id}"
assert data["data"] == {
"id": f"{API_HOST}/submissions/{submission.submission_id}",
"user": f"{API_HOST}/users/student02",
"project": f"{API_HOST}/projects/{project.project_id}",
"submission_id": f"{API_HOST}/submissions/{submission.submission_id}",
"uid": f"{API_HOST}/users/student02",
"project_id": f"{API_HOST}/projects/{project.project_id}",
"grading": 20,
"time": 'Thu, 14 Mar 2024 23:59:59 GMT',
"status": 'FAIL'
"submission_time": 'Thu, 14 Mar 2024 23:59:59 GMT',
"submission_status": 'FAIL'
}
4 changes: 1 addition & 3 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ const router = createBrowserRouter(
path="project/:projectId/overview"
element={<SubmissionsOverview />}
/>
<Route path="project">
<Route path=":projectId" element={<ProjectView />}></Route>
</Route>
<Route path="courses">
<Route index element={<AllCoursesTeacher />} loader={dataLoaderCourses}/>
<Route path="join" loader={synchronizeJoinCode} />
Expand All @@ -52,6 +49,7 @@ const router = createBrowserRouter(
element={<ProjectOverView />}
loader={fetchProjectPage}
/>
<Route path=":projectId" element={<ProjectView />}></Route>
<Route path="create" element={<ProjectCreateHome />} loader={fetchProjectForm}/>
</Route>
</Route>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/project/projectOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default function ProjectOverView() {
return (
<Grid container spacing={2} key={index}>
<Grid item xs={12}>
<Link href={`/${i18n.language}/course/${courseProjects[0].course.course_id}`} style={{color: 'inherit'}}
<Link href={`/${i18n.language}/courses/${courseProjects[0].course.course_id}`} style={{color: 'inherit'}}
underline={'none'}>
<Typography variant="h6">{courseProjects[0].course.name} {courseProjects[0].course.ufora_id}</Typography>
</Link>
Expand Down

0 comments on commit 33b645b

Please sign in to comment.