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