Beispiel #1
0
def strip_site_theme_templates_path(uri):
    """
    The change with respect of the edx-platform version is that in this case, are stripped the
    theme and parent theme from the uri

    See: openedx.core.djangoapps.theming.helpers

    Example:
        >> strip_site_theme_templates_path('/red-theme/lms/templates/header.html')
        'header.html'

    Arguments:
        uri (str): template path from which to remove site theme path. e.g. '/red-theme/lms/templates/header.html'

    Returns:
        (str): template path with site theme path removed.
    """
    theme = ThemingConfiguration.theming_helpers.get_current_theme()
    parent_theme = ThemingConfiguration.get_parent_or_default_theme()

    if theme:
        templates_path = "/".join([
            theme.theme_dir_name,
            ThemingConfiguration.theming_helpers.get_project_root_name(),
            "templates"
        ])

        uri = re.sub(r'^/*' + templates_path + '/*', '', uri)

    if not parent_theme:
        return uri

    # Do the same with the parent theme
    templates_path = "/".join([
        parent_theme.theme_dir_name,
        ThemingConfiguration.theming_helpers.get_project_root_name(),
        "templates"
    ])

    uri = re.sub(r'^/*' + templates_path + '/*', '', uri)

    grandparent_name = ThemingConfiguration.options('theme',
                                                    'grandparent',
                                                    default=None)
    if grandparent_name:
        grandparent_theme = ThemingConfiguration.get_wrapped_theme(
            grandparent_name)
        if not grandparent_theme:
            return uri

        # Do the same with the grandparent theme
        templates_path = "/".join([
            grandparent_theme.theme_dir_name,
            ThemingConfiguration.theming_helpers.get_project_root_name(),
            "templates"
        ])

        uri = re.sub(r'^/*' + templates_path + '/*', '', uri)

    return uri
Beispiel #2
0
def get_template_path_with_theme(relative_path):
    """
    The change with respect of the edx-platform version is that in this case, the templates are
    not searched only on the current site theme. They are also searched in the parent(default) theme.

    See: openedx.core.djangoapps.theming.helpers

    Example:
        >> get_template_path_with_theme('header.html')
        '/red-theme/lms/templates/header.html'

    Parameters:
        relative_path (str): template's path relative to the templates directory e.g. 'footer.html'

    Returns:
        (str): template path in current site's theme
    """
    relative_path = os.path.normpath(relative_path)
    template_name = re.sub(r'^/+', '', relative_path)

    theme = ThemingConfiguration.theming_helpers.get_current_theme()
    parent_theme = ThemingConfiguration.get_parent_or_default_theme()

    # Try with the theme.name
    if theme:
        # strip `/` if present at the start of relative_path
        template_path = theme.template_path / template_name
        absolute_path = theme.path / "templates" / template_name
        if absolute_path.exists():
            return str(template_path)

        if not parent_theme or theme.name == parent_theme.name:
            return relative_path

    if not parent_theme:
        return relative_path

    # Try with the theme.parent site theme
    template_path = parent_theme.template_path / template_name
    absolute_path = parent_theme.path / "templates" / template_name

    if absolute_path.exists():
        return str(template_path)

    # Try with grandparent
    grandparent_name = ThemingConfiguration.options('theme',
                                                    'grandparent',
                                                    default=None)
    if grandparent_name:
        grandparent_theme = ThemingConfiguration.get_wrapped_theme(
            grandparent_name)
        template_path = grandparent_theme.template_path / template_name
        absolute_path = grandparent_theme.path / "templates" / template_name

        if absolute_path.exists():
            return str(template_path)

    return relative_path
 def test_get_parent_theme(self, theme_mock, helper_mock):
     """
     Test that method return parent theme.
     """
     helper_mock.get_project_root_name.return_value = 'lms'
     helper_mock.get_theme_base_dir.return_value = 'templates'
     ThemingConfiguration.get_parent_or_default_theme()
     theme_mock.assert_called_with(
         name='parent-theme',
         project_root='lms',
         theme_dir_name='parent-theme',
         themes_base_dir='templates'
     )
    def get_grandparent_theme_template_sources():
        """
        Return the template dirs of the grandparent theme.
        """
        grandparent_theme = None
        grandparent_name = ThemingConfiguration.options('theme', 'grandparent', default=None)
        if grandparent_name:
            grandparent_theme = ThemingConfiguration.get_wrapped_theme(grandparent_name)

        template_paths = list()
        if grandparent_theme:
            return grandparent_theme.template_dirs

        return template_paths
Beispiel #5
0
def process_scripts(path):
    """
    Process and loads all the extra scripts for the template
    rendered during the request.
    """
    scripts = ThemingConfiguration.options('scripts', default={})

    for regex, values in six.iteritems(scripts):

        regex_path_match = re.compile(regex)

        if regex_path_match.match(path):

            scripts = []

            for script in values:

                validated_script = validate_script_attributes(script)

                if validated_script:

                    scripts.append(validated_script)

            if scripts:
                return scripts

    return None
Beispiel #6
0
    def _get_parent_themes(self):
        """
        Get the parent themes for the EoxTheme instance
        """
        parent_themes = OrderedDict()
        # The order of this list is important!
        parent_types = ['parent', 'grandparent']

        for parent in parent_types:
            parent_themes[parent] = None
            parent_theme_name = ThemingConfiguration.options('theme',
                                                             parent,
                                                             default=None)
            if parent_theme_name:
                parent_themes[parent] = ThemingConfiguration.get_wrapped_theme(
                    parent_theme_name)

        return parent_themes
    def get_parent_theme_template_sources():
        """
        Return the template dirs of the parent theme.
        """
        default_theme = ThemingConfiguration.get_parent_or_default_theme()
        template_paths = list()
        if default_theme:
            return default_theme.template_dirs

        return template_paths
Beispiel #8
0
    def url(self, name):
        """
        Returns url of the asset, themed url will be returned if the asset is themed otherwise default
        asset url will be returned.

        Args:
            name: name of the asset, e.g. 'images/logo.png'

        Returns:
            url of the asset, e.g. '/static/red-theme/images/logo.png' if current theme is red-theme and logo
            is provided by red-theme otherwise '/static/images/logo.png'
        """
        prefix = ''
        theme = ThemingConfiguration.theming_helpers.get_current_theme()
        parent_theme = ThemingConfiguration.get_parent_or_default_theme()

        # get theme prefix from site address if if asset is accessed via a url
        if theme:
            prefix = theme.theme_dir_name

        # get theme prefix from storage class, if asset is accessed during collectstatic run
        elif self.prefix:
            prefix = self.prefix

        # join theme prefix with asset name if theme is applied and themed asset exists
        if prefix and self.themed(name, prefix):
            return super(EoxThemeStorage, self).url(name)

        if parent_theme and self.themed(name, parent_theme.theme_dir_name):
            name = os.path.join(parent_theme.theme_dir_name, name)
            return super(EoxThemeStorage, self).url(name)

        grandparent_name = ThemingConfiguration.options('theme', 'grandparent', default=None)
        if grandparent_name:
            grandparent_theme = ThemingConfiguration.get_wrapped_theme(grandparent_name)
            if grandparent_theme and self.themed(name, grandparent_theme.theme_dir_name):
                name = os.path.join(grandparent_theme.theme_dir_name, name)

        return super(EoxThemeStorage, self).url(name)
Beispiel #9
0
    def process_request(self, request):
        """
        Set the request's 'site_theme'
        """
        # TODO: resolve. If the site does not exist, should we create it? or use a placeholder site_id

        theme_name = ThemingConfiguration.get_theme_name()

        if theme_name:
            current_theme = SITE_THEME(
                site_id=1,
                theme_dir_name=theme_name,
            )
            current_theme.id = 1
            request.site_theme = current_theme
Beispiel #10
0
    def _processed_asset_name(self, name):
        """
        Returns either a themed or unthemed version of the given asset name,
        depending on several factors.

        See the class docstring for more info.
        """
        theme = ThemingConfiguration.theming_helpers.get_current_theme()
        if theme and theme.theme_dir_name not in name:
            # during server run, append theme name to the asset name if it is not already there
            # this is ensure that correct hash is created and default asset is not always
            # used to create hash of themed assets.
            name = os.path.join(theme.theme_dir_name, name)
        parsed_name = urlsplit(unquote(name))
        clean_name = parsed_name.path.strip()
        asset_name = name
        if not self.exists(clean_name):
            # if themed asset does not exists then use default asset
            theme_name = name.split("/", 1)[0]
            # verify that themed asset was accessed
            if theme_name in [theme.theme_dir_name for theme in ThemingConfiguration.theming_helpers.get_themes()]:
                asset_name = "/".join(name.split("/")[1:])
        elif theme and theme.theme_dir_name in asset_name:
            return asset_name

        # Try the same with default theme
        parent_theme = ThemingConfiguration.get_parent_or_default_theme()

        if theme and parent_theme.name == theme.name:
            return asset_name

        theme = parent_theme
        if theme and theme.theme_dir_name not in asset_name:
            # during server run, append theme name to the asset name if it is not already there
            # this is ensure that correct hash is created and default asset is not always
            # used to create hash of themed assets.
            name = os.path.join(theme.theme_dir_name, asset_name)
        parsed_name = urlsplit(unquote(name))
        clean_name = parsed_name.path.strip()
        asset_name = name
        if not self.exists(clean_name):
            # if themed asset does not exists then use default asset
            theme = name.split("/", 1)[0]
            # verify that themed asset was accessed
            if theme in [theme.theme_dir_name for theme in ThemingConfiguration.theming_helpers.get_themes()]:
                asset_name = "/".join(name.split("/")[1:])

        return asset_name
 def test_comfiguration_helpers_called(self, conf_helpers_mock):
     """ If not found on settings it should use configuration helpers """
     ThemingConfiguration.options('section', 'item', default='Unique_string')
     conf_helpers_mock.get_value.assert_called()
Beispiel #12
0
 def is_current_theme(self):
     """
     Check if the theme instance is the current tenant theme
     """
     return self.name == ThemingConfiguration.get_theme_name()
 def test_options_call_exists_django_settings(self):
     """ Calling options must return the real value  """
     result = ThemingConfiguration.options('section', 'item', default='Unique_string')
     self.assertEqual('Given_String', result)
 def test_options_call_error(self):
     """ Calling options with error data must return the Default """
     result = ThemingConfiguration.options('section', 'item', default='Unique_string')
     self.assertEqual('Unique_string', result)
 def test_falsy_value(self):
     """ Calling options must return the value defined in the source even if that is false """
     result = ThemingConfiguration.options('section', 'item', default=None)
     self.assertIsNotNone(result)
     self.assertFalse(result)
Beispiel #16
0
 def render(self, context):
     return ThemingConfiguration.options(*self.options_args,
                                         default=self.options_default)
 def test_get_theme_name(self):
     """
     Test get_theme_name function.
     """
     theme = ThemingConfiguration.get_theme_name()
     self.assertEqual(theme, 'default-theme')
 def test_get_theme_name_tenant(self):
     """
     Test get_theme_name for microsite v0.
     """
     theme = ThemingConfiguration.get_theme_name()
     self.assertEqual(theme, 'other-theme')