From 985417fcf64ecca67daa6a17a6e12c1da5fc1002 Mon Sep 17 00:00:00 2001 From: Don Mitchell Date: Wed, 11 Dec 2013 14:52:19 -0500 Subject: [PATCH] Auth converting to new course_id syntax --- lms/djangoapps/courseware/roles.py | 43 +++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/lms/djangoapps/courseware/roles.py b/lms/djangoapps/courseware/roles.py index 60853e8090..6cb913a521 100644 --- a/lms/djangoapps/courseware/roles.py +++ b/lms/djangoapps/courseware/roles.py @@ -8,6 +8,9 @@ from abc import ABCMeta, abstractmethod from django.contrib.auth.models import User, Group from xmodule.modulestore import Location +from xmodule.modulestore.exceptions import InvalidLocationError, ItemNotFoundError +from xmodule.modulestore.django import loc_mapper +from xmodule.modulestore.locator import CourseLocator class CourseContextRequired(Exception): @@ -134,20 +137,40 @@ class CourseRole(GroupBasedRole): A named role in a particular course """ def __init__(self, role, location, course_context=None): - # pylint: disable=no-member - loc = Location(location) - legacy_group_name = '{0}_{1}'.format(role, loc.course) - - if loc.category.lower() == 'course': - course_id = loc.course_id - else: + # TODO: figure out how to make the group name generation lazy so it doesn't force the + # loc mapping? + if not hasattr(location, 'course_id'): + location = Location(location) + # direct copy from auth.authz.get_all_course_role_groupnames will refactor to one impl asap + groupnames = [] + try: + groupnames.append('{0}_{1}'.format(role, location.course_id)) + except InvalidLocationError: # will occur on old locations where location is not of category course if course_context is None: raise CourseContextRequired() - course_id = course_context + else: + groupnames.append('{0}_{1}'.format(role, course_context)) - group_name = '{0}_{1}'.format(role, course_id) + # pylint: disable=no-member + if isinstance(location, Location): + # least preferred legacy role_course format + groupnames.append('{0}_{1}'.format(role, location.course)) + try: + locator = loc_mapper().translate_location(location.course_id, location, False, False) + groupnames.append('{0}_{1}'.format(role, locator.course_id)) + except (InvalidLocationError, ItemNotFoundError): + # if it's never been mapped, the auth won't be via the Locator syntax + pass + elif isinstance(location, CourseLocator): + # handle old Location syntax + old_location = loc_mapper().translate_locator_to_location(location, get_course=True) + if old_location: + # the slashified version of the course_id (myu/mycourse/myrun) + groupnames.append('{0}_{1}'.format(role, old_location.course_id)) + # add the least desirable but sometimes occurring format. + groupnames.append('{0}_{1}'.format(role, old_location.course)) - super(CourseRole, self).__init__([group_name, legacy_group_name]) + super(CourseRole, self).__init__(groupnames) class OrgRole(GroupBasedRole):