def get_all_course_role_groupnames(location, role, use_filter=True): ''' Get all of the possible groupnames for this role location pair. If use_filter==True, only return the ones defined in the groups collection. ''' location = Locator.to_locator_or_location(location) groupnames = [] if isinstance(location, Location): try: groupnames.append('{0}_{1}'.format(role, location.course_id)) except InvalidLocationError: # will occur on old locations where location is not of category course pass try: locator = loc_mapper().translate_location(location.course_id, location, False, False) groupnames.append('{0}_{1}'.format(role, locator.package_id)) except (InvalidLocationError, ItemNotFoundError): pass # least preferred role_course format for legacy reasons groupnames.append('{0}_{1}'.format(role, location.course)) elif isinstance(location, CourseLocator): groupnames.append('{0}_{1}'.format(role, location.package_id)) 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)) # filter to the ones which exist default = groupnames[0] if use_filter: groupnames = [group.name for group in Group.objects.filter(name__in=groupnames)] return groupnames, default
def get_all_course_role_groupnames(location, role, use_filter=True): ''' Get all of the possible groupnames for this role location pair. If use_filter==True, only return the ones defined in the groups collection. ''' location = Locator.to_locator_or_location(location) # hack: check for existence of a group name in the legacy LMS format <role>_<course> # if it exists, then use that one, otherwise use a <role>_<course_id> which contains # more information 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 pass if isinstance(location, Location): # least preferred 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): pass elif isinstance(location, CourseLocator): 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)) # filter to the ones which exist default = groupnames[0] if use_filter: groupnames = [group for group in groupnames if Group.objects.filter(name=group).exists()] return groupnames, default
def get_course_groupname_for_role(location, role): location = Locator.to_locator_or_location(location) # hack: check for existence of a group name in the legacy LMS format <role>_<course> # if it exists, then use that one, otherwise use a <role>_<course_id> which contains # more information groupnames = [] groupnames.append('{0}_{1}'.format(role, location.course_id)) if isinstance(location, Location): groupnames.append('{0}_{1}'.format(role, location.course)) elif isinstance(location, CourseLocator): groupnames.append('{0}_{1}'.format(role, location.as_old_location_course_id)) for groupname in groupnames: if Group.objects.filter(name=groupname).exists(): return groupname return groupnames[0]
def __init__(self, role, location, course_context=None): """ Location may be either a Location, a string, dict, or tuple which Location will accept in its constructor, or a CourseLocator. Handle all these giving some preference to the preferred naming. """ # TODO: figure out how to make the group name generation lazy so it doesn't force the # loc mapping? self.location = Locator.to_locator_or_location(location) self.role = role # direct copy from auth.authz.get_all_course_role_groupnames will refactor to one impl asap groupnames = [] if isinstance(self.location, Location): try: groupnames.append(u'{0}_{1}'.format(role, self.location.course_id)) course_context = self.location.course_id # course_id is valid for translation except InvalidLocationError: # will occur on old locations where location is not of category course if course_context is None: raise CourseContextRequired() else: groupnames.append(u'{0}_{1}'.format(role, course_context)) try: locator = loc_mapper().translate_location_to_course_locator( course_context, self.location) groupnames.append(u'{0}_{1}'.format(role, locator.package_id)) except (InvalidLocationError, ItemNotFoundError): # if it's never been mapped, the auth won't be via the Locator syntax pass # least preferred legacy role_course format groupnames.append(u'{0}_{1}'.format(role, self.location.course)) # pylint: disable=E1101, E1103 elif isinstance(self.location, CourseLocator): groupnames.append(u'{0}_{1}'.format(role, self.location.package_id)) # handle old Location syntax old_location = loc_mapper().translate_locator_to_location( self.location, get_course=True) if old_location: # the slashified version of the course_id (myu/mycourse/myrun) groupnames.append(u'{0}_{1}'.format(role, old_location.course_id)) # add the least desirable but sometimes occurring format. groupnames.append(u'{0}_{1}'.format(role, old_location.course)) # pylint: disable=E1101, E1103 super(CourseRole, self).__init__(groupnames)
def __init__(self, role, location, course_context=None): """ Location may be either a Location, a string, dict, or tuple which Location will accept in its constructor, or a CourseLocator. Handle all these giving some preference to the preferred naming. """ # TODO: figure out how to make the group name generation lazy so it doesn't force the # loc mapping? self.location = Locator.to_locator_or_location(location) self.role = role # direct copy from auth.authz.get_all_course_role_groupnames will refactor to one impl asap groupnames = [] # pylint: disable=no-member if isinstance(self.location, Location): try: groupnames.append('{0}_{1}'.format(role, self.location.course_id)) course_context = self.location.course_id # course_id is valid for translation except InvalidLocationError: # will occur on old locations where location is not of category course if course_context is None: raise CourseContextRequired() else: groupnames.append('{0}_{1}'.format(role, course_context)) try: locator = loc_mapper().translate_location_to_course_locator(course_context, self.location) groupnames.append('{0}_{1}'.format(role, locator.package_id)) except (InvalidLocationError, ItemNotFoundError): # if it's never been mapped, the auth won't be via the Locator syntax pass # least preferred legacy role_course format groupnames.append('{0}_{1}'.format(role, self.location.course)) elif isinstance(self.location, CourseLocator): groupnames.append('{0}_{1}'.format(role, self.location.package_id)) # handle old Location syntax old_location = loc_mapper().translate_locator_to_location(self.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__(groupnames)
def get_all_course_role_groupnames(location, role, use_filter=True): ''' Get all of the possible groupnames for this role location pair. If use_filter==True, only return the ones defined in the groups collection. ''' location = Locator.to_locator_or_location(location) # hack: check for existence of a group name in the legacy LMS format <role>_<course> # if it exists, then use that one, otherwise use a <role>_<course_id> which contains # more information 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 pass if isinstance(location, Location): # least preferred 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): pass elif isinstance(location, CourseLocator): 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)) # filter to the ones which exist default = groupnames[0] if use_filter: groupnames = [ group for group in groupnames if Group.objects.filter(name=group).exists() ] return groupnames, default
def get_course_groupname_for_role(location, role): location = Locator.to_locator_or_location(location) # hack: check for existence of a group name in the legacy LMS format <role>_<course> # if it exists, then use that one, otherwise use a <role>_<course_id> which contains # more information 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 pass if isinstance(location, Location): groupnames.append('{0}_{1}'.format(role, location.course)) elif isinstance(location, CourseLocator): old_location = loc_mapper().translate_locator_to_location(location, get_course=True) if old_location: groupnames.append('{0}_{1}'.format(role, old_location.course_id)) for groupname in groupnames: if Group.objects.filter(name=groupname).exists(): return groupname return groupnames[0]
def get_all_course_role_groupnames(location, role, use_filter=True): ''' Get all of the possible groupnames for this role location pair. If use_filter==True, only return the ones defined in the groups collection. ''' location = Locator.to_locator_or_location(location) groupnames = [] if isinstance(location, Location): try: groupnames.append('{0}_{1}'.format(role, location.course_id)) except InvalidLocationError: # will occur on old locations where location is not of category course pass try: locator = loc_mapper().translate_location(location.course_id, location, False, False) groupnames.append('{0}_{1}'.format(role, locator.package_id)) except (InvalidLocationError, ItemNotFoundError): pass # least preferred role_course format for legacy reasons groupnames.append('{0}_{1}'.format(role, location.course)) elif isinstance(location, CourseLocator): groupnames.append('{0}_{1}'.format(role, location.package_id)) 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)) # filter to the ones which exist default = groupnames[0] if use_filter: groupnames = [ group.name for group in Group.objects.filter(name__in=groupnames) ] return groupnames, default
def test_old_location_helpers(self): """ Test the functions intended to help with the conversion from old locations to locators """ location_tuple = ('i4x', 'mit', 'eecs.6002x', 'course', 't3_2013') location = Location(location_tuple) self.assertEqual(location, Locator.to_locator_or_location(location)) self.assertEqual(location, Locator.to_locator_or_location(location_tuple)) self.assertEqual(location, Locator.to_locator_or_location(list(location_tuple))) self.assertEqual(location, Locator.to_locator_or_location(location.dict())) locator = BlockUsageLocator(course_id='foo.bar', branch='alpha', usage_id='deep') self.assertEqual(locator, Locator.to_locator_or_location(locator)) self.assertEqual( locator.as_course_locator(), Locator.to_locator_or_location(locator.as_course_locator())) self.assertEqual(location, Locator.to_locator_or_location(location.url())) self.assertEqual(locator, Locator.to_locator_or_location(locator.url())) self.assertEqual(locator, Locator.to_locator_or_location(locator.__dict__)) asset_location = Location( ['c4x', 'mit', 'eecs.6002x', 'asset', 'selfie.jpeg']) self.assertEqual(asset_location, Locator.to_locator_or_location(asset_location)) self.assertEqual(asset_location, Locator.to_locator_or_location(asset_location.url())) def_location_url = "defx://version/" + '{:024x}'.format( random.randrange(16**24)) self.assertEqual(DefinitionLocator(def_location_url), Locator.to_locator_or_location(def_location_url)) with self.assertRaises(ValueError): Locator.to_locator_or_location(22) with self.assertRaises(ValueError): Locator.to_locator_or_location("hello.world.not.a.url") self.assertIsNone(Locator.parse_url("unknown://foo.bar/baz"))
def test_old_location_helpers(self): """ Test the functions intended to help with the conversion from old locations to locators """ location_tuple = ('i4x', 'mit', 'eecs.6002x', 'course', 't3_2013') location = Location(location_tuple) self.assertEqual(location, Locator.to_locator_or_location(location)) self.assertEqual(location, Locator.to_locator_or_location(location_tuple)) self.assertEqual(location, Locator.to_locator_or_location(list(location_tuple))) self.assertEqual(location, Locator.to_locator_or_location(location.dict())) locator = BlockUsageLocator(package_id='foo.bar', branch='alpha', block_id='deep') self.assertEqual(locator, Locator.to_locator_or_location(locator)) self.assertEqual(locator.as_course_locator(), Locator.to_locator_or_location(locator.as_course_locator())) self.assertEqual(location, Locator.to_locator_or_location(location.url())) self.assertEqual(locator, Locator.to_locator_or_location(locator.url())) self.assertEqual(locator, Locator.to_locator_or_location(locator.__dict__)) asset_location = Location(['c4x', 'mit', 'eecs.6002x', 'asset', 'selfie.jpeg']) self.assertEqual(asset_location, Locator.to_locator_or_location(asset_location)) self.assertEqual(asset_location, Locator.to_locator_or_location(asset_location.url())) def_location_url = "defx://version/" + '{:024x}'.format(random.randrange(16 ** 24)) self.assertEqual(DefinitionLocator(def_location_url), Locator.to_locator_or_location(def_location_url)) with self.assertRaises(ValueError): Locator.to_locator_or_location(22) with self.assertRaises(ValueError): Locator.to_locator_or_location("hello.world.not.a.url") self.assertIsNone(Locator.parse_url("unknown://foo.bar/baz"))