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 = self.store.make_course_key('Org1', 'Course1', 'Run1') 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( 'xmodule.modulestore.mixed.MixedModuleStore.get_course_summaries', return_value=[mocked_ccx_course]): courses_iter, __ = _accessible_courses_iter_for_tests(self.request) self.assertEqual(len(list(courses_iter)), 0)
def set_global_course_creator_status(request, user, set_global_creator): """ Updates global course creator status of a user. """ from course_creators.models import CourseCreator from course_creators.views import update_course_creator_group request_user = request.user course_creator, __ = CourseCreator.objects.get_or_create(user=user) course_creator.state = CourseCreator.GRANTED if set_global_creator else CourseCreator.UNREQUESTED course_creator.note = 'Global course creator user was updated by panel admin {} on {}'.format( request_user.email, datetime.now()) course_creator.admin = request_user course_creator.save() edly_user_info_cookie = request.COOKIES.get( settings.EDLY_USER_INFO_COOKIE_NAME, None) edx_org = get_edx_org_from_cookie(edly_user_info_cookie) update_course_creator_group(request_user, user, set_global_creator) if set_global_creator: GlobalCourseCreatorRole(edx_org).add_users(user) else: GlobalCourseCreatorRole(edx_org).remove_users(user) instructor_courses = UserBasedRole( user, CourseInstructorRole.ROLE).courses_with_role() staff_courses = UserBasedRole( user, CourseStaffRole.ROLE).courses_with_role() instructor_courses_keys = [ course.course_id for course in instructor_courses ] staff_courses_keys = [course.course_id for course in staff_courses] UserBasedRole( user, CourseInstructorRole.ROLE).remove_courses(*instructor_courses_keys) UserBasedRole(user, CourseStaffRole.ROLE).remove_courses(*staff_courses_keys)