def get_course_keys_user_is_staff_for(self, user): """ Return all the course keys the user is course instructor or course staff role for """ # Get all the courses of which the user is course staff for. If None, return false def filter_ccx(course_access): """ CCXs cannot be edited in Studio and should not be filtered """ return not isinstance(course_access.course_id, CCXLocator) instructor_courses = UserBasedRole(user, CourseInstructorRole.ROLE).courses_with_role() staff_courses = UserBasedRole(user, CourseStaffRole.ROLE).courses_with_role() all_courses = list(filter(filter_ccx, instructor_courses | staff_courses)) course_keys = {} for course_access in all_courses: if course_access.course_id is not None: course_keys[course_access.course_id] = course_access.course_id return list(course_keys.values())
def test_courses_list_with_ccx_courses(self): """ Tests that CCX courses are filtered in course listing. """ # Create a course and assign access roles to user. course_location = CourseLocator('Org1', 'Course1', 'Course1') course = self._create_course_with_access_groups( course_location, self.user) # Create a ccx course key and add assign access roles to user. ccx_course_key = CCXLocator.from_course_locator(course.id, '1') self._add_role_access_to_user(self.user, ccx_course_key) # Test that CCX courses are filtered out. courses_list, __ = _accessible_courses_list_from_groups(self.request) self.assertEqual(len(courses_list), 1) self.assertNotIn(ccx_course_key, [course.id for course in courses_list]) # Get all courses which user has access. instructor_courses = UserBasedRole( self.user, CourseInstructorRole.ROLE).courses_with_role() staff_courses = UserBasedRole( self.user, CourseStaffRole.ROLE).courses_with_role() all_courses = (instructor_courses | staff_courses) # Verify that CCX course exists in access but filtered by `_accessible_courses_list_from_groups`. self.assertIn(ccx_course_key, [access.course_id for access in all_courses]) # Verify that CCX courses are filtered out while iterating over all courses mocked_ccx_course = Mock(id=ccx_course_key) with patch( 'openedx.core.djangoapps.content.course_overviews.models.CourseOverview.get_all_courses', return_value=[mocked_ccx_course], ): courses_iter, __ = _accessible_courses_iter_for_tests(self.request) self.assertEqual(len(list(courses_iter)), 0)