def get_lms_link_for_item(location, preview=False): """ Returns an LMS link to the course with a jump_to to the provided location. :param location: the location to jump to :param preview: True if the preview version of LMS should be returned. Default value is false. """ assert isinstance(location, UsageKey) # checks LMS_BASE value in site configuration for the given course_org_filter(org) # if not found returns settings.LMS_BASE lms_base = SiteConfiguration.get_value_for_org(location.org, "LMS_BASE", settings.LMS_BASE) if lms_base is None: return None if preview: # checks PREVIEW_LMS_BASE value in site configuration for the given course_org_filter(org) # if not found returns settings.FEATURES.get('PREVIEW_LMS_BASE') lms_base = SiteConfiguration.get_value_for_org( location.org, "PREVIEW_LMS_BASE", settings.FEATURES.get('PREVIEW_LMS_BASE')) return u"//{lms_base}/courses/{course_key}/jump_to/{location}".format( lms_base=lms_base, course_key=text_type(location.course_key), location=text_type(location), )
def get_lms_link_for_item(location, preview=False): """ Returns an LMS link to the course with a jump_to to the provided location. :param location: the location to jump to :param preview: True if the preview version of LMS should be returned. Default value is false. """ assert isinstance(location, UsageKey) # checks LMS_BASE value in site configuration for the given course_org_filter(org) # if not found returns settings.LMS_BASE lms_base = SiteConfiguration.get_value_for_org( location.org, "LMS_BASE", settings.LMS_BASE ) if lms_base is None: return None if preview: # checks PREVIEW_LMS_BASE value in site configuration for the given course_org_filter(org) # if not found returns settings.FEATURES.get('PREVIEW_LMS_BASE') lms_base = SiteConfiguration.get_value_for_org( location.org, "PREVIEW_LMS_BASE", settings.FEATURES.get('PREVIEW_LMS_BASE') ) return u"//{lms_base}/courses/{course_key}/jump_to/{location}".format( lms_base=lms_base, course_key=location.course_key.to_deprecated_string(), location=location.to_deprecated_string(), )
def test_get_value_for_org(self): """ Test that get_value_for_org returns correct value for any given key. """ # add SiteConfiguration to database SiteConfigurationFactory.create( site=self.site, site_values=self.test_config1 ) SiteConfigurationFactory.create( site=self.site2, site_values=self.test_config2 ) # Make sure entry is saved and retrieved correctly assert SiteConfiguration.get_value_for_org(self.test_config1['course_org_filter'], 'university') ==\ self.test_config1['university'] assert SiteConfiguration.get_value_for_org(self.test_config1['course_org_filter'], 'platform_name') ==\ self.test_config1['platform_name'] assert SiteConfiguration.get_value_for_org(self.test_config1['course_org_filter'], 'SITE_NAME') ==\ self.test_config1['SITE_NAME'] assert SiteConfiguration.get_value_for_org(self.test_config1['course_org_filter'], 'css_overrides_file') ==\ self.test_config1['css_overrides_file'] assert SiteConfiguration.get_value_for_org(self.test_config1['course_org_filter'], 'ENABLE_MKTG_SITE') ==\ self.test_config1['ENABLE_MKTG_SITE'] # Make sure entry is saved and retrieved correctly assert SiteConfiguration.get_value_for_org(self.test_config2['course_org_filter'], 'university') ==\ self.test_config2['university'] assert SiteConfiguration.get_value_for_org(self.test_config2['course_org_filter'], 'platform_name') ==\ self.test_config2['platform_name'] assert SiteConfiguration\ .get_value_for_org(self.test_config2['course_org_filter'], 'SITE_NAME') == \ self.test_config2['SITE_NAME'] assert SiteConfiguration\ .get_value_for_org(self.test_config2['course_org_filter'], 'css_overrides_file') == self.test_config2['css_overrides_file'] assert SiteConfiguration\ .get_value_for_org(self.test_config2['course_org_filter'], 'ENABLE_MKTG_SITE') == self.test_config2['ENABLE_MKTG_SITE'] # Test that the default value is returned if the value for the given key is not found in the configuration assert SiteConfiguration\ .get_value_for_org(self.test_config1['course_org_filter'], 'non-existent', 'dummy-default-value') == 'dummy-default-value' # Test that the default value is returned if the value for the given key is not found in the configuration assert SiteConfiguration\ .get_value_for_org(self.test_config2['course_org_filter'], 'non-existent', 'dummy-default-value') == 'dummy-default-value' # Test that the default value is returned if org is not found in the configuration assert SiteConfiguration.get_value_for_org('non-existent-org', 'platform_name', 'dummy-default-value') ==\ 'dummy-default-value'
def get_lms_link_for_item(location, preview=False): """ Returns an LMS link to the course with a jump_to to the provided location. :param location: the location to jump to :param preview: True if the preview version of LMS should be returned. Default value is false. """ assert isinstance(location, UsageKey) # checks LMS_BASE value in site configuration for the given course_org_filter(org) # if not found returns settings.LMS_BASE lms_base = SiteConfiguration.get_value_for_org( location.org, "LMS_BASE", microsite.get_value_for_org(location.org, 'SITE_NAME', settings.LMS_BASE), ) if lms_base is None: return None # eduNEXT Disable preview for now (not microsite aware), it will redirect to a normal course. preview = False if preview: # checks PREVIEW_LMS_BASE value in site configuration for the given course_org_filter(org) # if not found returns settings.FEATURES.get('PREVIEW_LMS_BASE') lms_base = SiteConfiguration.get_value_for_org( location.org, "PREVIEW_LMS_BASE", settings.FEATURES.get('PREVIEW_LMS_BASE') ) return u"//{lms_base}/courses/{course_key}/jump_to/{location}".format( lms_base=lms_base, course_key=text_type(location.course_key), location=text_type(location), )
def get_lms_link_for_certificate_web_view(course_key, mode): """ Returns the url to the certificate web view. """ assert isinstance(course_key, CourseKey) # checks LMS_BASE value in SiteConfiguration against course_org_filter if not found returns settings.LMS_BASE lms_base = SiteConfiguration.get_value_for_org(course_key.org, "LMS_BASE", settings.LMS_BASE) if lms_base is None: return None return "//{certificate_web_base}/certificates/course/{course_id}?preview={mode}".format( certificate_web_base=lms_base, course_id=str(course_key), mode=mode)
def get_lms_link_for_item(location, preview=False): """ Returns an LMS link to the course with a jump_to to the provided location. :param location: the location to jump to :param preview: True if the preview version of LMS should be returned. Default value is false. """ assert isinstance(location, UsageKey) # checks LMS_BASE value in site configuration for the given course_org_filter(org) # if not found returns settings.LMS_BASE # eduNEXT 19.11.2015 make the link microsite aware, based on the org of the course lms_base = microsite.get_value_for_org(location.org, 'SITE_NAME') if not lms_base: lms_base = SiteConfiguration.get_value_for_org(location.org, "LMS_BASE", settings.LMS_BASE) if lms_base is None: return None # eduNEXT 19.11.2015 Disable preview for now (not microsite aware), it will redirect to a normal course. preview = False if preview: # checks PREVIEW_LMS_BASE value in site configuration for the given course_org_filter(org) # if not found returns settings.FEATURES.get('PREVIEW_LMS_BASE') lms_base = SiteConfiguration.get_value_for_org( location.org, "PREVIEW_LMS_BASE", settings.FEATURES.get('PREVIEW_LMS_BASE')) return u"//{lms_base}/courses/{course_key}/jump_to/{location}".format( lms_base=lms_base, course_key=location.course_key.to_deprecated_string(), location=location.to_deprecated_string(), )
def get_handler_url(self, handler_name): base = SiteConfiguration.get_value_for_org( self.course_id.org, "LMS_ROOT_URL", settings.LMS_ROOT_URL, ) if base.endswith("/"): base = base[:-1] return "{base}/courses/{course_key}/xblock/{usage_key}/handler/{handler_name}".format( base=base, course_key=self.course_id, usage_key=self.location, handler_name=handler_name, )
def get_lms_link_for_certificate_web_view(user_id, course_key, mode): """ Returns the url to the certificate web view. """ assert isinstance(course_key, CourseKey) # checks LMS_BASE value in SiteConfiguration against course_org_filter if not found returns settings.LMS_BASE lms_base = SiteConfiguration.get_value_for_org(course_key.org, "LMS_BASE", settings.LMS_BASE) if lms_base is None: return None return u"//{certificate_web_base}/certificates/user/{user_id}/course/{course_id}?preview={mode}".format( certificate_web_base=lms_base, user_id=user_id, course_id=unicode(course_key), mode=mode )
def get_value_for_org(org, val_name, default=None): """ This returns a configuration value for a site configuration which has an org_filter that matches with the argument. Args: org (str): Course org filter, this value will be used to filter out the correct site configuration. val_name (str): Name of the key for which to return configuration value. default: default value to return if key is not present in the configuration Returns: Configuration value for the given key. """ # Import is placed here to avoid model import at project startup. from openedx.core.djangoapps.site_configuration.models import SiteConfiguration if SiteConfiguration.has_org(org): return SiteConfiguration.get_value_for_org(org, val_name, default) else: return default
def get_value_for_org(org, val_name, default=None): """ This returns a configuration value for a site configuration or microsite configuration which has an org_filter that matches with the argument. Args: org (str): Course org filter, this value will be used to filter out the correct site configuration. name (str): Name of the key for which to return configuration value. default: default value to return if key is not present in the configuration Returns: Configuration value for the given key. """ # Here we first look for the asked org inside site configuration, and if org is not present in site configuration # then we go ahead and look it inside microsite configuration. if SiteConfiguration.has_org(org): return SiteConfiguration.get_value_for_org(org, val_name, default) else: return microsite.get_value_for_org(org, val_name, default)
def retrieve_last_sitewide_block_completed(username): """ Completion utility From a string 'username' or object User retrieve the last course block marked as 'completed' and construct a URL :param username: str(username) or obj(User) :return: block_lms_url """ if not completion_waffle.waffle().is_enabled( completion_waffle.ENABLE_COMPLETION_TRACKING): return if not isinstance(username, User): userobj = User.objects.get(username=username) else: userobj = username latest_completions_by_course = BlockCompletion.latest_blocks_completed_all_courses( userobj) known_site_configs = [ other_site_config.get_value('course_org_filter') for other_site_config in SiteConfiguration.objects.all() if other_site_config.get_value('course_org_filter') ] current_site_configuration = get_config_value_from_site_or_settings( name='course_org_filter', site=get_current_site()) # courses.edx.org has no 'course_org_filter' # however the courses within DO, but those entries are not found in # known_site_configs, which are White Label sites # This is necessary because the WL sites and courses.edx.org # have the same AWS RDS mySQL instance candidate_course = None candidate_block_key = None latest_date = None # Go through dict, find latest for course, [modified_date, block_key] in latest_completions_by_course.items(): if not current_site_configuration: # This is a edx.org if course.org in known_site_configs: continue if not latest_date or modified_date > latest_date: candidate_course = course candidate_block_key = block_key latest_date = modified_date else: # This is a White Label site, and we should find candidates from the same site if course.org not in current_site_configuration: # Not the same White Label, or a edx.org course continue if not latest_date or modified_date > latest_date: candidate_course = course candidate_block_key = block_key latest_date = modified_date if not candidate_course: return lms_root = SiteConfiguration.get_value_for_org(candidate_course.org, "LMS_ROOT_URL", settings.LMS_ROOT_URL) try: item = modulestore().get_item(candidate_block_key, depth=1) except ItemNotFoundError: item = None if not (lms_root and item): return return u"{lms_root}/courses/{course_key}/jump_to/{location}".format( lms_root=lms_root, course_key=text_type(item.location.course_key), location=text_type(item.location), )
def test_get_value_for_org(self): """ Test that get_value_for_org returns correct value for any given key. """ # add SiteConfiguration to database SiteConfigurationFactory.create( site=self.site, values=self.test_config1, ) SiteConfigurationFactory.create( site=self.site2, values=self.test_config2, ) # Make sure entry is saved and retrieved correctly self.assertEqual( SiteConfiguration.get_value_for_org( self.test_config1['course_org_filter'], "university"), self.test_config1['university'], ) self.assertEqual( SiteConfiguration.get_value_for_org( self.test_config1['course_org_filter'], "platform_name"), self.test_config1['platform_name'], ) self.assertEqual( SiteConfiguration.get_value_for_org( self.test_config1['course_org_filter'], "SITE_NAME"), self.test_config1['SITE_NAME'], ) self.assertEqual( SiteConfiguration.get_value_for_org( self.test_config1['course_org_filter'], "css_overrides_file"), self.test_config1['css_overrides_file'], ) self.assertEqual( SiteConfiguration.get_value_for_org( self.test_config1['course_org_filter'], "ENABLE_MKTG_SITE"), self.test_config1['ENABLE_MKTG_SITE'], ) # Make sure entry is saved and retrieved correctly self.assertEqual( SiteConfiguration.get_value_for_org( self.test_config2['course_org_filter'], "university"), self.test_config2['university'], ) self.assertEqual( SiteConfiguration.get_value_for_org( self.test_config2['course_org_filter'], "platform_name"), self.test_config2['platform_name'], ) self.assertEqual( SiteConfiguration.get_value_for_org( self.test_config2['course_org_filter'], "SITE_NAME"), self.test_config2['SITE_NAME'], ) self.assertEqual( SiteConfiguration.get_value_for_org( self.test_config2['course_org_filter'], "css_overrides_file"), self.test_config2['css_overrides_file'], ) self.assertEqual( SiteConfiguration.get_value_for_org( self.test_config2['course_org_filter'], "ENABLE_MKTG_SITE"), self.test_config2['ENABLE_MKTG_SITE'], ) # Test that the default value is returned if the value for the given key is not found in the configuration self.assertEqual( SiteConfiguration.get_value_for_org( self.test_config1['course_org_filter'], "non-existent", "dummy-default-value"), "dummy-default-value", ) # Test that the default value is returned if the value for the given key is not found in the configuration self.assertEqual( SiteConfiguration.get_value_for_org( self.test_config2['course_org_filter'], "non-existent", "dummy-default-value"), "dummy-default-value", ) # Test that the default value is returned if org is not found in the configuration self.assertEqual( SiteConfiguration.get_value_for_org("non-existent-org", "platform_name", "dummy-default-value"), "dummy-default-value", )
def retrieve_last_sitewide_block_completed(user): """ Completion utility From a string 'username' or object User retrieve the last course block marked as 'completed' and construct a URL :param user: obj(User) :return: block_lms_url """ if not completion_waffle.waffle().is_enabled(completion_waffle.ENABLE_COMPLETION_TRACKING): return latest_completions_by_course = BlockCompletion.latest_blocks_completed_all_courses(user) known_site_configs = [ other_site_config.get_value('course_org_filter') for other_site_config in SiteConfiguration.objects.all() if other_site_config.get_value('course_org_filter') ] current_site_configuration = get_config_value_from_site_or_settings( name='course_org_filter', site=get_current_site() ) # courses.edx.org has no 'course_org_filter' # however the courses within DO, but those entries are not found in # known_site_configs, which are White Label sites # This is necessary because the WL sites and courses.edx.org # have the same AWS RDS mySQL instance candidate_course = None candidate_block_key = None latest_date = None # Go through dict, find latest for course, [modified_date, block_key] in latest_completions_by_course.items(): if not current_site_configuration: # This is a edx.org if course.org in known_site_configs: continue if not latest_date or modified_date > latest_date: candidate_course = course candidate_block_key = block_key latest_date = modified_date else: # This is a White Label site, and we should find candidates from the same site if course.org not in current_site_configuration: # Not the same White Label, or a edx.org course continue if not latest_date or modified_date > latest_date: candidate_course = course candidate_block_key = block_key latest_date = modified_date if not candidate_course: return lms_root = SiteConfiguration.get_value_for_org(candidate_course.org, "LMS_ROOT_URL", settings.LMS_ROOT_URL) try: item = modulestore().get_item(candidate_block_key, depth=1) except ItemNotFoundError: item = None if not (lms_root and item): return return u"{lms_root}/courses/{course_key}/jump_to/{location}".format( lms_root=lms_root, course_key=text_type(item.location.course_key), location=text_type(item.location), )
def test_get_value_for_org(self): """ Test that get_value_for_org returns correct value for any given key. """ # add SiteConfiguration to database SiteConfigurationFactory.create(site=self.site, values=self.test_config1) SiteConfigurationFactory.create(site=self.site2, values=self.test_config2) # Make sure entry is saved and retrieved correctly self.assertEqual( SiteConfiguration.get_value_for_org(self.test_config1["course_org_filter"], "university"), self.test_config1["university"], ) self.assertEqual( SiteConfiguration.get_value_for_org(self.test_config1["course_org_filter"], "platform_name"), self.test_config1["platform_name"], ) self.assertEqual( SiteConfiguration.get_value_for_org(self.test_config1["course_org_filter"], "SITE_NAME"), self.test_config1["SITE_NAME"], ) self.assertEqual( SiteConfiguration.get_value_for_org(self.test_config1["course_org_filter"], "css_overrides_file"), self.test_config1["css_overrides_file"], ) self.assertEqual( SiteConfiguration.get_value_for_org(self.test_config1["course_org_filter"], "ENABLE_MKTG_SITE"), self.test_config1["ENABLE_MKTG_SITE"], ) # Make sure entry is saved and retrieved correctly self.assertEqual( SiteConfiguration.get_value_for_org(self.test_config2["course_org_filter"], "university"), self.test_config2["university"], ) self.assertEqual( SiteConfiguration.get_value_for_org(self.test_config2["course_org_filter"], "platform_name"), self.test_config2["platform_name"], ) self.assertEqual( SiteConfiguration.get_value_for_org(self.test_config2["course_org_filter"], "SITE_NAME"), self.test_config2["SITE_NAME"], ) self.assertEqual( SiteConfiguration.get_value_for_org(self.test_config2["course_org_filter"], "css_overrides_file"), self.test_config2["css_overrides_file"], ) self.assertEqual( SiteConfiguration.get_value_for_org(self.test_config2["course_org_filter"], "ENABLE_MKTG_SITE"), self.test_config2["ENABLE_MKTG_SITE"], ) # Test that the default value is returned if the value for the given key is not found in the configuration self.assertEqual( SiteConfiguration.get_value_for_org( self.test_config1["course_org_filter"], "non-existent", "dummy-default-value" ), "dummy-default-value", ) # Test that the default value is returned if the value for the given key is not found in the configuration self.assertEqual( SiteConfiguration.get_value_for_org( self.test_config2["course_org_filter"], "non-existent", "dummy-default-value" ), "dummy-default-value", ) # Test that the default value is returned if org is not found in the configuration self.assertEqual( SiteConfiguration.get_value_for_org("non-existent-org", "platform_name", "dummy-default-value"), "dummy-default-value", )