Beispiel #1
0
    def _create_cached_program(self):
        """ helper method to create a cached program """
        program = ProgramFactory.create()

        for course_key in self.course_keys:
            program['courses'].append(CourseFactory(id=course_key))

        program['type'] = 'MicroBachelors'
        program['type_attrs']['coaching_supported'] = True

        for course in program['courses']:
            cache.set(
                CATALOG_COURSE_PROGRAMS_CACHE_KEY_TPL.format(course_uuid=course['uuid']),
                [program['uuid']],
                None
            )

            course_run = course['course_runs'][0]['key']
            cache.set(
                COURSE_PROGRAMS_CACHE_KEY_TPL.format(course_run_id=course_run),
                [program['uuid']],
                None
            )
        cache.set(
            PROGRAM_CACHE_KEY_TPL.format(uuid=program['uuid']),
            program,
            None
        )

        return program
Beispiel #2
0
    def get_catalog_courses(self, programs):
        """
        Get all catalog courses for the programs.
        """
        courses = defaultdict(list)

        for program in programs.values():
            for course_uuid in course_uuids_for_program(program):
                course_cache_key = CATALOG_COURSE_PROGRAMS_CACHE_KEY_TPL.format(course_uuid=course_uuid)
                courses[course_cache_key].append(program['uuid'])
        return courses
Beispiel #3
0
def get_programs(site=None, uuid=None, uuids=None, course=None, catalog_course_uuid=None, organization=None):
    """Read programs from the cache.

    The cache is populated by a management command, cache_programs.

    Keyword Arguments:
        site (Site): django.contrib.sites.models object to fetch programs of.
        uuid (string): UUID identifying a specific program to read from the cache.
        uuids (list of string): UUIDs identifying a specific programs to read from the cache.
        course (string): course run id identifying a specific course run to read from the cache.
        catalog_course_uuid (string): Catalog Course UUID
        organization (string): short name for specific organization to read from the cache.

    Returns:
        list of dict, representing programs.
        dict, if a specific program is requested.
    """
    if len([arg for arg in (site, uuid, uuids, course, catalog_course_uuid, organization) if arg is not None]) != 1:
        raise TypeError('get_programs takes exactly one argument')

    if uuid:
        program = cache.get(PROGRAM_CACHE_KEY_TPL.format(uuid=uuid))
        if not program:
            logger.warning(missing_details_msg_tpl.format(uuid=uuid))

        return program
    elif course:
        uuids = cache.get(COURSE_PROGRAMS_CACHE_KEY_TPL.format(course_run_id=course))
        if not uuids:
            # Currently, the cache does not differentiate between a cache miss and a course
            # without programs. After this is changed, log any cache misses here.
            return []
    elif catalog_course_uuid:
        uuids = cache.get(CATALOG_COURSE_PROGRAMS_CACHE_KEY_TPL.format(course_uuid=catalog_course_uuid))
        if not uuids:
            # Currently, the cache does not differentiate between a cache miss and a course
            # without programs. After this is changed, log any cache misses here.
            return []
    elif site:
        site_config = getattr(site, 'configuration', None)
        catalog_url = site_config.get_value('COURSE_CATALOG_API_URL') if site_config else None
        if site_config and catalog_url:
            uuids = cache.get(SITE_PROGRAM_UUIDS_CACHE_KEY_TPL.format(domain=site.domain), [])
            if not uuids:
                logger.warning(u'Failed to get program UUIDs from the cache for site {}.'.format(site.domain))
        else:
            uuids = []
    elif organization:
        uuids = get_programs_for_organization(organization)
        if not uuids:
            return []

    return get_programs_by_uuids(uuids)
Beispiel #4
0
    def test_get_from_catalog_course(self, mock_warning, _mock_info):
        expected_program = ProgramFactory()
        expected_catalog_course = expected_program['courses'][0]

        assert get_programs(
            catalog_course_uuid=expected_catalog_course['uuid']) == []

        cache.set(
            CATALOG_COURSE_PROGRAMS_CACHE_KEY_TPL.format(
                course_uuid=expected_catalog_course['uuid']),
            [expected_program['uuid']], None)
        cache.set(PROGRAM_CACHE_KEY_TPL.format(uuid=expected_program['uuid']),
                  expected_program, None)

        actual_program = get_programs(
            catalog_course_uuid=expected_catalog_course['uuid'])

        assert actual_program == [expected_program]
        assert not mock_warning.called