From 9de10d324e74c59b64f87659506c8dbb0080f94a Mon Sep 17 00:00:00 2001 From: Marcos Prieto Date: Tue, 9 Jul 2024 10:05:15 +0200 Subject: [PATCH] Filter course metrics by course IDs --- lms/views/dashboard/api/course.py | 26 +++++++---- .../lms/views/dashboard/api/course_test.py | 46 +++++++++++++++++++ 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/lms/views/dashboard/api/course.py b/lms/views/dashboard/api/course.py index ac599a4259..2a2f1acd5f 100644 --- a/lms/views/dashboard/api/course.py +++ b/lms/views/dashboard/api/course.py @@ -28,6 +28,9 @@ class CoursesMetricsSchema(PyramidRequestSchema): assignment_ids = fields.List(fields.Integer()) """Return metrics for these assignments only.""" + courses_ids = fields.List(fields.Integer()) + """Return metrics for these courses only.""" + class CourseViews: def __init__(self, request) -> None: @@ -71,17 +74,20 @@ def courses(self) -> APICourses: def organization_courses(self) -> APICourses: filter_by_h_userids = self.request.parsed_params.get("h_userids") filter_by_assignment_ids = self.request.parsed_params.get("assignment_ids") + filter_by_course_ids = self.request.parsed_params.get("course_ids") org = self.dashboard_service.get_request_organization(self.request) - courses = self.request.db.scalars( - self.course_service.get_courses( - organization=org, - instructor_h_userid=self.request.user.h_userid - if self.request.user - else None, - h_userids=filter_by_h_userids, - assignment_ids=filter_by_assignment_ids, - ) - ).all() + courses_query = self.course_service.get_courses( + organization=org, + instructor_h_userid=self.request.user.h_userid + if self.request.user + else None, + h_userids=filter_by_h_userids, + assignment_ids=filter_by_assignment_ids, + ) + if filter_by_course_ids: + courses_query = courses_query.where(Course.id.in_(filter_by_course_ids)) + courses = self.request.db.scalars(courses_query).all() + courses_assignment_counts = ( self.assignment_service.get_courses_assignments_count( [c.id for c in courses] diff --git a/tests/unit/lms/views/dashboard/api/course_test.py b/tests/unit/lms/views/dashboard/api/course_test.py index 9f25dec180..4819ad8104 100644 --- a/tests/unit/lms/views/dashboard/api/course_test.py +++ b/tests/unit/lms/views/dashboard/api/course_test.py @@ -85,6 +85,52 @@ def test_get_organization_courses( ] } + def test_get_organization_courses_filter_by_courses( + self, + course_service, + pyramid_request, + views, + db_session, + dashboard_service, + assignment_service, + ): + org = factories.Organization() + courses = factories.Course.create_batch(5) + dashboard_service.get_request_organization.return_value = org + course_service.get_courses.return_value = select(Course).order_by(Course.id) + pyramid_request.matchdict["organization_public_id"] = sentinel.public_id + db_session.flush() + pyramid_request.parsed_params = {"course_ids": [courses[0].id]} + + response = views.organization_courses() + + dashboard_service.get_request_organization.assert_called_once_with( + pyramid_request + ) + course_service.get_courses.assert_called_once_with( + organization=org, + instructor_h_userid=pyramid_request.user.h_userid, + h_userids=None, + assignment_ids=None, + ) + assignment_service.get_courses_assignments_count.assert_called_once_with( + [courses[0].id] + ) + + assert response == { + "courses": [ + { + "id": c.id, + "title": c.lms_name, + "course_metrics": { + "assignments": assignment_service.get_courses_assignments_count.return_value.get.return_value, + "last_launched": c.updated.isoformat(), + }, + } + for c in courses[0:1] + ] + } + def test_course(self, views, pyramid_request, dashboard_service): pyramid_request.matchdict["course_id"] = sentinel.id course = factories.Course()