Exemplo n.º 1
0
 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'
     )
Exemplo n.º 2
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
Exemplo n.º 3
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
Exemplo n.º 4
0
    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
Exemplo n.º 5
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
Exemplo n.º 6
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)