From 96de0c2cfe3b33944a085929b004b607ea107a7c Mon Sep 17 00:00:00 2001 From: Marcos Prieto Date: Wed, 4 Sep 2024 17:58:49 +0200 Subject: [PATCH] Expose auto grading config on the assigment API --- lms/product/canvas/_plugin/misc.py | 2 +- lms/views/dashboard/api/assignment.py | 13 ++++++- .../views/dashboard/api/assignment_test.py | 35 ++++++++++++++++++- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/lms/product/canvas/_plugin/misc.py b/lms/product/canvas/_plugin/misc.py index 7ea36aabc8..0448f9e60e 100644 --- a/lms/product/canvas/_plugin/misc.py +++ b/lms/product/canvas/_plugin/misc.py @@ -59,7 +59,7 @@ def get_assignment_configuration( if auto_grading_config := self.get_deep_linked_assignment_configuration( request ).get("auto_grading_config"): - # Auto grading is a complex structure, deserialize it before hand + # Auto grading is a complex structure, deserialize it beforehand assignment_config["auto_grading_config"] = cast( AutoGradingConfig, json.loads(auto_grading_config) ) diff --git a/lms/views/dashboard/api/assignment.py b/lms/views/dashboard/api/assignment.py index f07415a72f..44c25a0def 100644 --- a/lms/views/dashboard/api/assignment.py +++ b/lms/views/dashboard/api/assignment.py @@ -6,6 +6,7 @@ APIAssignment, APIAssignments, APICourse, + AutoGradingConfig, ) from lms.models import Assignment, RoleScope, RoleType from lms.security import Permissions @@ -98,7 +99,7 @@ def assignments(self) -> APIAssignments: ) def assignment(self) -> APIAssignment: assignment = self.dashboard_service.get_request_assignment(self.request) - return APIAssignment( + api_assignment = APIAssignment( id=assignment.id, title=assignment.title, created=assignment.created.isoformat(), @@ -108,6 +109,16 @@ def assignment(self) -> APIAssignment: ), ) + if auto_grading_config := assignment.auto_grading_config: + api_assignment["auto_grading_config"] = AutoGradingConfig( + grading_type=auto_grading_config.grading_type, + activity_calculation=auto_grading_config.activity_calculation, + required_annotations=auto_grading_config.required_annotations, + required_replies=auto_grading_config.required_replies, + ) + + return api_assignment + @view_config( route_name="api.dashboard.course.assignments.metrics", request_method="GET", diff --git a/tests/unit/lms/views/dashboard/api/assignment_test.py b/tests/unit/lms/views/dashboard/api/assignment_test.py index 222abb8459..9f720f05f8 100644 --- a/tests/unit/lms/views/dashboard/api/assignment_test.py +++ b/tests/unit/lms/views/dashboard/api/assignment_test.py @@ -3,7 +3,7 @@ import pytest from sqlalchemy import select -from lms.models import Assignment, RoleScope, RoleType, User +from lms.models import Assignment, AutoGradingConfig, RoleScope, RoleType, User from lms.views.dashboard.api.assignment import AssignmentViews from tests import factories @@ -65,6 +65,39 @@ def test_assignment( "course": {"id": assignment.course.id, "title": assignment.course.lms_name}, } + def test_assignment_with_auto_grading( + self, views, pyramid_request, assignment, db_session, dashboard_service + ): + assignment.auto_grading_config = AutoGradingConfig( + activity_calculation="separate", + grading_type="scaled", + required_annotations=1, + required_replies=1, + ) + + db_session.flush() + pyramid_request.matchdict["assignment_id"] = sentinel.id + dashboard_service.get_request_assignment.return_value = assignment + + response = views.assignment() + + dashboard_service.get_request_assignment.assert_called_once_with( + pyramid_request + ) + + assert response == { + "id": assignment.id, + "title": assignment.title, + "created": assignment.created.isoformat(), + "course": {"id": assignment.course.id, "title": assignment.course.lms_name}, + "auto_grading_config": { + "activity_calculation": "separate", + "grading_type": "scaled", + "required_annotations": 1, + "required_replies": 1, + }, + } + def test_course_assignments( self, views,