def test_get_value_for_org(self): """ Make sure we can do lookups on Microsite configuration based on ORG fields """ # first make sure default value is returned if there's no Microsite ORG match value = get_value_for_org("BogusX", "university", "default_value") self.assertEquals(value, "default_value") # now test when we call in a value Microsite ORG, note this is defined in test.py configuration value = get_value_for_org("TestMicrositeX", "university", "default_value") self.assertEquals(value, "test_microsite")
def test_get_value_for_org(self): """ Tests microsite.get_value_for_org works as expected. """ microsite.set_by_domain(self.microsite_subdomain) self.assertEqual( microsite.get_value_for_org('TestSiteX', 'platform_name'), 'Test Site') # if no config is set microsite.clear() with patch('django.conf.settings.MICROSITE_CONFIGURATION', False): self.assertEqual( microsite.get_value_for_org('TestSiteX', 'platform_name', 'Default Value'), 'Default Value')
def _get_asset_json(display_name, content_type, date, location, thumbnail_location, locked): """ Helper method for formatting the asset information to send to client. """ asset_url = StaticContent.serialize_asset_key_with_slash(location) # eduNEXT 14.07.2016 lms_base = microsite.get_value_for_org(location.org, 'SITE_NAME', settings.LMS_BASE) external_url = lms_base + asset_url return { 'display_name': display_name, 'content_type': content_type, 'date_added': get_default_time_display(date), 'url': asset_url, 'external_url': external_url, 'portable_url': StaticContent.get_static_path_from_location(location), 'thumbnail': StaticContent.serialize_asset_key_with_slash(thumbnail_location) if thumbnail_location else None, 'locked': locked, # Needed for Backbone delete/update. 'id': unicode(location) }
def get_link_for_about_page(course): """ Arguments: course: This can be either a course overview object or a course descriptor. Returns the course sharing url, this can be one of course's social sharing url, marketing url, or lms course about url. """ is_social_sharing_enabled = configuration_helpers.get_value( 'SOCIAL_SHARING_SETTINGS', getattr(settings, 'SOCIAL_SHARING_SETTINGS', {}) ).get('CUSTOM_COURSE_URLS') if is_social_sharing_enabled and course.social_sharing_url: course_about_url = course.social_sharing_url elif settings.FEATURES.get('ENABLE_MKTG_SITE') and getattr(course, 'marketing_url', None): course_about_url = course.marketing_url else: about_base = microsite.get_value_for_org( course.id.org, 'SITE_NAME', settings.LMS_ROOT_URL ) if not about_base.startswith("http"): about_base = u"{protocol}://{base}".format( protocol="https", base=about_base ) course_about_url = u'{about_base_url}/courses/{course_key}/about'.format( about_base_url=about_base, course_key=unicode(course.id), ) return course_about_url
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)) if settings.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: lms_base = settings.FEATURES.get('PREVIEW_LMS_BASE') else: lms_base = 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', 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 settings_handler(request, tag=None, package_id=None, branch=None, version_guid=None, block=None): """ Course settings for dates and about pages GET html: get the page json: get the CourseDetails model PUT json: update the Course and About xblocks through the CourseDetails model """ locator, course_module = _get_locator_and_course(package_id, branch, version_guid, block, request.user) if 'text/html' in request.META.get('HTTP_ACCEPT', '') and request.method == 'GET': upload_asset_url = locator.url_reverse('assets/') # see if the ORG of this course can be attributed to a 'Microsite'. In that case, the # course about page should be editable in Studio about_page_editable = not microsite.get_value_for_org( course_module.location.org, 'ENABLE_MKTG_SITE', settings.FEATURES.get('ENABLE_MKTG_SITE', False)) short_description_editable = settings.FEATURES.get( 'EDITABLE_SHORT_DESCRIPTION', True) return render_to_response( 'settings.html', { 'context_course': course_module, 'course_locator': locator, 'lms_link_for_about_page': utils.get_lms_link_for_about_page(course_module.location), 'course_image_url': utils.course_image_url(course_module), 'details_url': locator.url_reverse('/settings/details/'), 'about_page_editable': about_page_editable, 'short_description_editable': short_description_editable, 'upload_asset_url': upload_asset_url }) elif 'application/json' in request.META.get('HTTP_ACCEPT', ''): if request.method == 'GET': return JsonResponse( CourseDetails.fetch(locator), # encoder serializes dates, old locations, and instances encoder=CourseSettingsEncoder) else: # post or put, doesn't matter. return JsonResponse(CourseDetails.update_from_json( locator, request.json, request.user), encoder=CourseSettingsEncoder)
def test_get_value_for_org(self): """ Tests microsite.get_value_for_org works as expected. """ microsite.set_by_domain(self.microsite_subdomain) self.assertEqual( microsite.get_value_for_org('TestMicrositeX', 'platform_name'), 'Test Microsite' ) # if no config is set microsite.clear() with patch('django.conf.settings.MICROSITE_CONFIGURATION', False): self.assertEqual( microsite.get_value_for_org('TestMicrositeX', 'platform_name', 'Default Value'), 'Default Value' )
def test_get_value_for_org(self, site_backend): """ Make sure get_value_for_org return value of org if it present. """ with patch('microsite_configuration.microsite.BACKEND', get_backend(site_backend, BaseMicrositeBackend)): value = get_value_for_org("TestSiteX", "university", "default_value") self.assertEquals(value, "test_site")
def test_get_value_for_org_when_microsite_has_no_org(self, site_backend): """ Make sure default value is returned if there's no Microsite ORG match """ with patch('microsite_configuration.microsite.BACKEND', get_backend(site_backend, BaseMicrositeBackend)): value = get_value_for_org("BogusX", "university", "default_value") self.assertEquals(value, "default_value")
def test_get_value_for_org(self): """ Tests microsite.get_value_for_org works as expected. """ microsite.set_by_domain(self.microsite.site.domain) self.assertEqual( microsite.get_value_for_org(self.microsite.get_organizations()[0], 'platform_name'), self.microsite.values['platform_name'] )
def test_get_value_for_org(self): """ Tests microsite.get_value_for_org works as expected. """ microsite.set_by_domain(self.microsite.site.domain) self.assertEqual( microsite.get_value_for_org(self.microsite.get_organizations()[0], 'platform_name'), self.microsite.values['platform_name'])
def completion_page(self): base = microsite.get_value_for_org( self.course_id.org, "SITE_NAME", settings.LMS_ROOT_URL, ) if not base.startswith("http"): base = "//{}".format(base) return "{base}/courses/{course_key}/xblock/{usage_key}/handler/completion".format( base=base, course_key=self.course_id, usage_key=self.location, )
def get_handler_url(self, handler_name): base = microsite.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 settings_handler(request, tag=None, package_id=None, branch=None, version_guid=None, block=None): """ Course settings for dates and about pages GET html: get the page json: get the CourseDetails model PUT json: update the Course and About xblocks through the CourseDetails model """ locator, course_module = _get_locator_and_course( package_id, branch, version_guid, block, request.user ) if 'text/html' in request.META.get('HTTP_ACCEPT', '') and request.method == 'GET': upload_asset_url = locator.url_reverse('assets/') # see if the ORG of this course can be attributed to a 'Microsite'. In that case, the # course about page should be editable in Studio about_page_editable = not microsite.get_value_for_org( course_module.location.org, 'ENABLE_MKTG_SITE', settings.FEATURES.get('ENABLE_MKTG_SITE', False) ) short_description_editable = settings.FEATURES.get('EDITABLE_SHORT_DESCRIPTION', True) return render_to_response('settings.html', { 'context_course': course_module, 'course_locator': locator, 'lms_link_for_about_page': utils.get_lms_link_for_about_page(course_module.location), 'course_image_url': utils.course_image_url(course_module), 'details_url': locator.url_reverse('/settings/details/'), 'about_page_editable': about_page_editable, 'short_description_editable': short_description_editable, 'upload_asset_url': upload_asset_url }) elif 'application/json' in request.META.get('HTTP_ACCEPT', ''): if request.method == 'GET': return JsonResponse( CourseDetails.fetch(locator), # encoder serializes dates, old locations, and instances encoder=CourseSettingsEncoder ) else: # post or put, doesn't matter. return JsonResponse( CourseDetails.update_from_json(locator, request.json, request.user), encoder=CourseSettingsEncoder )
def settings_handler(request, course_key_string): """ Course settings for dates and about pages GET html: get the page json: get the CourseDetails model PUT json: update the Course and About xblocks through the CourseDetails model """ course_key = CourseKey.from_string(course_key_string) course_module = _get_course_module(course_key, request.user) if "text/html" in request.META.get("HTTP_ACCEPT", "") and request.method == "GET": upload_asset_url = reverse_course_url("assets_handler", course_key) # see if the ORG of this course can be attributed to a 'Microsite'. In that case, the # course about page should be editable in Studio about_page_editable = not microsite.get_value_for_org( course_module.location.org, "ENABLE_MKTG_SITE", settings.FEATURES.get("ENABLE_MKTG_SITE", False) ) short_description_editable = settings.FEATURES.get("EDITABLE_SHORT_DESCRIPTION", True) return render_to_response( "settings.html", { "context_course": course_module, "course_locator": course_key, "lms_link_for_about_page": utils.get_lms_link_for_about_page(course_key), "course_image_url": utils.course_image_url(course_module), "details_url": reverse_course_url("settings_handler", course_key), "about_page_editable": about_page_editable, "short_description_editable": short_description_editable, "upload_asset_url": upload_asset_url, }, ) elif "application/json" in request.META.get("HTTP_ACCEPT", ""): if request.method == "GET": return JsonResponse( CourseDetails.fetch(course_key), # encoder serializes dates, old locations, and instances encoder=CourseSettingsEncoder, ) else: # post or put, doesn't matter. return JsonResponse( CourseDetails.update_from_json(course_key, request.json, request.user), encoder=CourseSettingsEncoder )
def _get_asset_json(display_name, content_type, date, location, thumbnail_location, locked): ''' Helper method for formatting the asset information to send to client. ''' asset_url = StaticContent.serialize_asset_key_with_slash(location) lms_base = microsite.get_value_for_org(location.org, 'SITE_NAME', settings.LMS_BASE) external_url = lms_base + asset_url return { 'display_name': display_name, 'content_type': content_type, 'date_added': get_default_time_display(date), 'url': asset_url, 'external_url': external_url, 'portable_url': StaticContent.get_static_path_from_location(location), 'thumbnail': StaticContent.serialize_asset_key_with_slash(thumbnail_location) if thumbnail_location else None, 'locked': locked, # needed for Backbone delete/update. 'id': unicode(location) }
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 get_lms_link_for_about_page(course_key): """ Returns the url to the course about page. """ assert isinstance(course_key, CourseKey) if settings.FEATURES.get('ENABLE_MKTG_SITE'): # Root will be "https://www.edx.org". The complete URL will still not be exactly correct, # but redirects exist from www.edx.org to get to the Drupal course about page URL. about_base = settings.MKTG_URLS['ROOT'] else: about_base = settings.LMS_ROOT_URL # eduNEXT 23.12.2015 make the link microsite aware, based on the org of the course about_base = microsite.get_value_for_org(course_key.org, 'SITE_NAME', about_base) return u"{about_base_url}/courses/{course_key}/about".format( about_base_url=about_base, course_key=course_key.to_deprecated_string())
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", microsite.get_value_for_org(course_key.org, 'SITE_NAME', 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_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 = microsite.get_value_for_org(course_key.org, 'SITE_NAME') if not 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_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_about_page(course_key): """ Returns the url to the course about page from the location tuple. """ assert(isinstance(course_key, CourseKey)) if settings.FEATURES.get('ENABLE_MKTG_SITE', False): if not hasattr(settings, 'MKTG_URLS'): log.exception("ENABLE_MKTG_SITE is True, but MKTG_URLS is not defined.") return None marketing_urls = settings.MKTG_URLS # Root will be "https://www.edx.org". The complete URL will still not be exactly correct, # but redirects exist from www.edx.org to get to the Drupal course about page URL. about_base = marketing_urls.get('ROOT', None) if about_base is None: log.exception('There is no ROOT defined in MKTG_URLS') return None # Strip off https:// (or http://) to be consistent with the formatting of LMS_BASE. about_base = re.sub(r"^https?://", "", about_base) elif settings.LMS_BASE is not None: about_base = settings.LMS_BASE # eduNEXT 23.12.2015 make the link microsite aware, based on the org of the course about_base = microsite.get_value_for_org(course_key.org, 'SITE_NAME', about_base) else: return None return u"//{about_base_url}/courses/{course_key}/about".format( about_base_url=about_base, course_key=course_key.to_deprecated_string() )
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(), )