Update Course Catalog API to support filters
This commit is contained in:
@@ -470,13 +470,14 @@ class CourseOverview(TimeStampedModel):
|
||||
return course_overviews
|
||||
|
||||
@classmethod
|
||||
def get_all_courses(cls, org=None):
|
||||
def get_all_courses(cls, org=None, filter_=None):
|
||||
"""
|
||||
Returns all CourseOverview objects in the database.
|
||||
|
||||
Arguments:
|
||||
org (string): Optional parameter that allows filtering
|
||||
by organization.
|
||||
org (string): Optional parameter that allows case-insensitive
|
||||
filtering by organization.
|
||||
filter_ (dict): Optional parameter that allows custom filtering.
|
||||
"""
|
||||
# Note: If a newly created course is not returned in this QueryList,
|
||||
# make sure the "publish" signal was emitted when the course was
|
||||
@@ -489,6 +490,9 @@ class CourseOverview(TimeStampedModel):
|
||||
# Case-insensitive exact matching allows us to deal with this kind of dirty data.
|
||||
course_overviews = course_overviews.filter(org__iexact=org)
|
||||
|
||||
if filter_:
|
||||
course_overviews = course_overviews.filter(**filter_)
|
||||
|
||||
return course_overviews
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -826,3 +826,24 @@ class CourseOverviewImageSetTestCase(ModuleStoreTestCase):
|
||||
}
|
||||
)
|
||||
return course_overview
|
||||
|
||||
def test_get_all_courses_by_mobile_available(self):
|
||||
non_mobile_course = CourseFactory.create(emit_signals=True)
|
||||
mobile_course = CourseFactory.create(mobile_available=True, emit_signals=True)
|
||||
|
||||
test_cases = (
|
||||
(None, {non_mobile_course.id, mobile_course.id}),
|
||||
(dict(mobile_available=True), {mobile_course.id}),
|
||||
(dict(mobile_available=False), {non_mobile_course.id}),
|
||||
)
|
||||
|
||||
for filter_, expected_courses in test_cases:
|
||||
self.assertEqual(
|
||||
{
|
||||
course_overview.id
|
||||
for course_overview in
|
||||
CourseOverview.get_all_courses(filter_=filter_)
|
||||
},
|
||||
expected_courses,
|
||||
"testing CourseOverview.get_all_courses with filter_={}".format(filter_),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user