def _assert_courses_not_in_overview(self, *courses): """ Assert that courses doesn't exist in the course overviews. """ course_keys = CourseOverview.get_all_course_keys() for expected_course_key in courses: self.assertNotIn(expected_course_key, course_keys)
def _assert_courses_in_overview(self, *courses): """ Assert courses exists in course overviews. """ course_keys = CourseOverview.get_all_course_keys() for expected_course_key in courses: self.assertIn(expected_course_key, course_keys)
def _assert_courses_in_overview(self, *courses): """ Assert courses exists in course overviews. """ course_keys = CourseOverview.get_all_course_keys() for expected_course_key in courses: self.assertIn(expected_course_key, course_keys)
def enqueue(cls, site, current_date, day_offset, override_recipient_email=None): # lint-amnesty, pylint: disable=missing-function-docstring set_code_owner_attribute_from_module(__name__) target_datetime = (current_date - datetime.timedelta(days=day_offset)) if not cls.is_enqueue_enabled(site): cls.log_info(u'Message queuing disabled for site %s', site.domain) return cls.log_info(u'Target date = %s', target_datetime.date().isoformat()) for course_key in CourseOverview.get_all_course_keys(): task_args = ( site.id, serialize( target_datetime ), # Need to leave as a datetime for serialization purposes here str(course_key ), # Needs to be a string for celery to properly process override_recipient_email, ) cls.log_info(u'Launching task with args = %r', task_args) cls.task_instance.apply_async( task_args, retry=False, )
def _assert_courses_not_in_overview(self, *courses): """ Assert that courses doesn't exist in the course overviews. """ course_keys = CourseOverview.get_all_course_keys() for expected_course_key in courses: self.assertNotIn(expected_course_key, course_keys)
def _get_courses_with_access_type(self, user, access_type): # Check the application cache and update if not present. The application # cache is useful since there are calls to different endpoints in close # succession, for example the id_token and user_info endpoints. key = '-'.join([str(self.__class__), str(user.id), access_type]) course_ids = cache.get(key) if not course_ids: course_keys = CourseOverview.get_all_course_keys() # Global staff have access to all courses. Filter courses for non-global staff. if not GlobalStaff().has_user(user): course_keys = [ course_key for course_key in course_keys if has_access(user, access_type, course_key) ] course_ids = [ six.text_type(course_key) for course_key in course_keys ] cache.set(key, course_ids, self.COURSE_CACHE_TIMEOUT) return course_ids
def list_course_keys(request, username, role): """ Yield all available CourseKeys for the user having the given role. The courses returned include those for which the user identified by `username` has the given role. Additionally, the logged in user should have permission to view courses available to that user. Note: This function does not use branding to determine courses. Arguments: request (HTTPRequest): Used to identify the logged-in user and to instantiate the course module to retrieve the course about description username (string): The name of the user the logged-in user would like to be identified as Keyword Arguments: role (string): Course keys are filtered such that only those for which the user has the specified role are returned. Return value: Yield `CourseKey` objects representing the collection of courses. """ user = get_effective_user(request.user, username) course_keys = CourseOverview.get_all_course_keys() # Global staff have access to all courses. Filter courses for non-global staff. if GlobalStaff().has_user(user): return course_keys return LazySequence( ( course_key for course_key in course_keys if has_access(user, role, course_key) ), est_len=len(course_keys) )
def _get_courses_with_access_type(self, user, access_type): # Check the application cache and update if not present. The application # cache is useful since there are calls to different endpoints in close # succession, for example the id_token and user_info endpoints. key = '-'.join([str(self.__class__), str(user.id), access_type]) course_ids = cache.get(key) if not course_ids: course_keys = CourseOverview.get_all_course_keys() # Global staff have access to all courses. Filter courses for non-global staff. if not GlobalStaff().has_user(user): course_keys = [course_key for course_key in course_keys if has_access(user, access_type, course_key)] course_ids = [six.text_type(course_key) for course_key in course_keys] cache.set(key, course_ids, self.COURSE_CACHE_TIMEOUT) return course_ids
def enqueue(cls, site, current_date, day_offset, override_recipient_email=None): target_date = (current_date - datetime.timedelta(days=day_offset)).date() if not cls.is_enqueue_enabled(site): cls.log_info(u'Message queuing disabled for site %s', site.domain) return cls.log_info(u'Target date = %s', target_date.isoformat()) for course_key in CourseOverview.get_all_course_keys(): task_args = ( site.id, serialize(target_date), course_key, override_recipient_email, ) cls.log_info(u'Launching task with args = %r', task_args) cls().apply_async( task_args, retry=False, )
def list_course_keys(request, username, role): """ Yield all available CourseKeys for the user having the given role. The courses returned include those for which the user identified by `username` has the given role. Additionally, the logged in user should have permission to view courses available to that user. Note: This function does not use branding to determine courses. Arguments: request (HTTPRequest): Used to identify the logged-in user and to instantiate the course module to retrieve the course about description username (string): The name of the user the logged-in user would like to be identified as Keyword Arguments: role (string): Course keys are filtered such that only those for which the user has the specified role are returned. Return value: Yield `CourseKey` objects representing the collection of courses. """ user = get_effective_user(request.user, username) all_course_keys = CourseOverview.get_all_course_keys() # Global staff have access to all courses. Filter courses for non-global staff. if GlobalStaff().has_user(user): return all_course_keys if role == 'staff': # This short-circuit implementation bypasses has_access() which we think is too slow for some users when # evaluating staff-level course access for Insights. Various tickets have context on this issue: CR-2487, # TNL-7448, DESUPPORT-416, and probably more. # # This is a simplified implementation that does not consider org-level access grants (e.g. when course_id is # empty). filtered_course_keys = ( CourseAccessRole.objects.filter( user=user, # Having the instructor role implies staff access. role__in=['staff', 'instructor'], ) # We need to check against CourseOverview so that we don't return any Libraries. .extra(tables=['course_overviews_courseoverview'], where=['course_id = course_overviews_courseoverview.id']) # For good measure, make sure we don't return empty course IDs. .exclude(course_id=CourseKeyField.Empty).order_by( 'course_id').values_list('course_id', flat=True).distinct()) else: # This is the original implementation which still covers the case where role = "instructor": filtered_course_keys = LazySequence( (course_key for course_key in all_course_keys if has_access(user, role, course_key)), est_len=len(all_course_keys)) return filtered_course_keys