Create Python API for program_enrollments: Part I (#21511)

This is the first in a series of commits to create
a Python API for the LMS program_enrollments app.
We do some general refactoring, renaming, and clean-up
in order to move toward the creation of that API.

EDUCATOR-4321
This commit is contained in:
Kyle McCormick
2019-09-04 14:06:00 -04:00
committed by GitHub
parent b229e9749b
commit ea5652010c
24 changed files with 737 additions and 714 deletions

View File

@@ -39,6 +39,7 @@ from openedx.core.djangoapps.catalog.tests.mixins import CatalogIntegrationMixin
from openedx.core.djangoapps.catalog.utils import (
child_programs,
course_run_keys_for_program,
is_course_run_in_program,
get_course_run_details,
get_course_runs,
get_course_runs_for_course,
@@ -790,6 +791,11 @@ class TestProgramCourseRunCrawling(TestCase):
}
self.assertEqual(expected_course_runs, course_run_keys_for_program(self.complex_program))
def test_is_course_run_in_program(self):
self.assertTrue(is_course_run_in_program('course-run-4', self.complex_program))
self.assertFalse(is_course_run_in_program('course-run-5', self.complex_program))
self.assertFalse(is_course_run_in_program('course-run-4', self.simple_program))
@skip_unless_lms
class TestGetProgramsByType(CacheIsolationTestCase):

View File

@@ -573,6 +573,29 @@ def get_course_run_details(course_run_key, fields):
return course_run_details
def is_course_run_in_program(course_run_key, program):
"""
Check if a course run is part of a program.
Arguments:
program (dict): program data, as returned by get_programs()
course_run_key (CourseKey|str)
Returns: bool
Whether the program exists AND the course run is part of it.
"""
# Right now, this function simply loads all the program data from the cache,
# walks the structure to collect the set of course run keys,
# and then sees if `course_run_key` is in that set.
# If we need to optimize this later, we can.
course_run_key_str = (
str(course_run_key) if isinstance(course_run_key, CourseKey)
else course_run_key
)
course_run_keys = course_run_keys_for_program(program)
return course_run_key_str in course_run_keys
def course_run_keys_for_program(parent_program):
"""
All of the course run keys associated with this ``parent_program``, either