From 2945b923d3596983fa3a090fbe9f78b29762f044 Mon Sep 17 00:00:00 2001 From: Clinton Blackburn Date: Wed, 7 Jan 2015 17:22:51 -0500 Subject: [PATCH] Updated _get_courses_with_access_type - Return all courses for global staff without iteration - Filter all courses on block type --- lms/djangoapps/oauth2_handler/handlers.py | 31 +++++++++++++---------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/lms/djangoapps/oauth2_handler/handlers.py b/lms/djangoapps/oauth2_handler/handlers.py index fe1957bbee..be784cd166 100644 --- a/lms/djangoapps/oauth2_handler/handlers.py +++ b/lms/djangoapps/oauth2_handler/handlers.py @@ -2,14 +2,14 @@ from django.conf import settings from django.core.cache import cache +from xmodule.modulestore.django import modulestore from courseware.access import has_access +from openedx.core.djangoapps.user_api.models import UserPreference from student.models import anonymous_id_for_user from student.models import UserProfile -from openedx.core.djangoapps.user_api.models import UserPreference from lang_pref import LANGUAGE_KEY -from xmodule.modulestore.django import modulestore -from xmodule.course_module import CourseDescriptor +from student.roles import GlobalStaff, CourseStaffRole, CourseInstructorRole class OpenIDHandler(object): @@ -134,7 +134,7 @@ class CourseAccessHandler(object): # TODO: unfortunately there is not a faster and still correct way to # check if a user is instructor of at least one course other than # checking the access type against all known courses. - course_ids = self.find_courses(data['user'], 'instructor') + course_ids = self.find_courses(data['user'], CourseInstructorRole.ROLE) return ['instructor_courses'] if course_ids else None def scope_course_staff(self, data): @@ -144,7 +144,7 @@ class CourseAccessHandler(object): """ # TODO: see :method:CourseAccessHandler.scope_course_instructor - course_ids = self.find_courses(data['user'], 'staff') + course_ids = self.find_courses(data['user'], CourseStaffRole.ROLE) return ['staff_courses'] if course_ids else None @@ -155,7 +155,7 @@ class CourseAccessHandler(object): """ - return self.find_courses(data['user'], 'instructor', data.get('values')) + return self.find_courses(data['user'], CourseInstructorRole.ROLE, data.get('values')) def claim_staff_courses(self, data): """ @@ -164,7 +164,7 @@ class CourseAccessHandler(object): """ - return self.find_courses(data['user'], 'staff', data.get('values')) + return self.find_courses(data['user'], CourseStaffRole.ROLE, data.get('values')) def find_courses(self, user, access_type, values=None): """ @@ -192,17 +192,22 @@ class CourseAccessHandler(object): # pylint: disable=missing-docstring 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 endpoins. + # 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 course_ids is None: - course_ids = [unicode(course.id) for course in _get_all_courses() - if has_access(user, access_type, course)] + if not course_ids: + courses = _get_all_courses() + + # Global staff have access to all courses. Filter courses for non-global staff. + if not GlobalStaff().has_user(user): + courses = [course for course in courses if has_access(user, access_type, course)] + + course_ids = [unicode(course.id) for course in courses] + cache.set(key, course_ids, self.COURSE_CACHE_TIMEOUT) return course_ids @@ -235,6 +240,6 @@ def _get_all_courses(): """ Utility function to list all available courses. """ ms_courses = modulestore().get_courses() - courses = [c for c in ms_courses if isinstance(c, CourseDescriptor)] + courses = [course for course in ms_courses if course.scope_ids.block_type == 'course'] return courses