Skip to content

Commit

Permalink
Filter course metrics by course IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospri committed Jul 9, 2024
1 parent 1ac289f commit 9de10d3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
26 changes: 16 additions & 10 deletions lms/views/dashboard/api/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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]
Expand Down
46 changes: 46 additions & 0 deletions tests/unit/lms/views/dashboard/api/course_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 9de10d3

Please sign in to comment.