示例#1
0
    def get_template_sources(self, template_name, template_dirs=None):
        """Return the absolute paths to "template_name", when appended to the
        selected theme directory in THEMES_DIR.
        Any paths that don't lie inside one of the
        template dirs are excluded from the result set, for security reasons.
        """
        theme_templates = []
        current_request = get_current_request()
        # this is needed when the theme is changed
        self.theme_root = get_theme_root()
        if current_request and current_request.mobile:
            theme_templates.append(os.path.join(self.theme_root, 'mobile'))
        theme_templates.append(os.path.join(self.theme_root, 'templates'))

        for template_path in theme_templates:
            try:
                if settings.USE_S3_THEME:
                    yield os.path.join(template_path, template_name)
                else:
                    yield safe_join(template_path, template_name)
            except SuspiciousFileOperation:
                # The joined path was located outside of this particular
                # template_dir (it might be inside another one, so this isn't
                # fatal).
                pass
示例#2
0
def get_theme():
    request = get_current_request()
    if request:
        theme = request.session.get('theme', get_setting('module', 'theme_editor', 'theme'))
    else:
        theme = get_setting('module', 'theme_editor', 'theme')
    return theme
示例#3
0
def get_theme(active_theme=None):
    if active_theme is None:
        active_theme = get_active_theme()
    request = get_current_request()
    if request:
        return request.session.get('theme', active_theme)
    return active_theme
示例#4
0
    def get_template_sources(self, template_name, template_dirs=None):
        """Return the absolute paths to "template_name", when appended to the
        selected theme directory in THEMES_DIR.
        Any paths that don't lie inside one of the
        template dirs are excluded from the result set, for security reasons.
        """
        theme_templates = []
        current_request = get_current_request()
        # this is needed when the theme is changed
        self.theme_root = get_theme_root()
        if current_request and current_request.mobile:
            theme_templates.append(os.path.join(self.theme_root, 'mobile'))
        theme_templates.append(os.path.join(self.theme_root, 'templates'))

        for template_path in theme_templates:
            try:
                if settings.USE_S3_THEME:
                    yield os.path.join(template_path, template_name)
                else:
                    yield safe_join(template_path, template_name)
            except SuspiciousFileOperation:
                # The joined path was located outside of this particular
                # template_dir (it might be inside another one, so this isn't
                # fatal).
                pass
示例#5
0
    def get_template_sources(self, template_name, template_dirs=None):
        """
        Return possible absolute paths to "template_name" in the current theme
        and any themes it inherits from.
        Any paths that don't lie inside one of the template dirs are excluded
        from the result set for security reasons.
        """
        request = get_current_request()
        mobile = (request and request.mobile)

        active_theme = get_active_theme()
        theme = get_theme(active_theme)
        cached_theme, theme_search_info = self.cached_theme_search_info

        # If the theme changed or the user is previewing a different theme,
        # recalculate theme_search_info.
        # Note that this Loader instance may be shared between multiple threads,
        # so you must be careful when reading/writing
        # self.cached_theme_search_info to ensure that writes in one thread
        # cannot cause unexpected behavior in another thread that is
        # reading/writing self.cached_theme_search_info at the same time.
        if cached_theme != theme:
            theme_search_info = []
            for cur_theme in get_theme_search_order(theme):
                if is_builtin_theme(cur_theme) or not settings.USE_S3_THEME:
                    theme_search_info.append(
                        (cur_theme, get_theme_root(cur_theme), False))
                else:
                    theme_search_info.append((cur_theme, cur_theme, True))
            if theme == active_theme:
                self.cached_theme_search_info = (theme, theme_search_info)

        for cur_theme, cur_theme_root, use_s3_theme in theme_search_info:
            for template_path in (['mobile', 'templates']
                                  if mobile else ['templates']):
                if not use_s3_theme:
                    try:
                        template_file = safe_join(cur_theme_root,
                                                  template_path, template_name)
                    except SuspiciousFileOperation:
                        # The joined path was located outside of template_path,
                        # although it might be inside another one, so this isn't
                        # fatal.
                        continue
                else:
                    template_file = os.path.join(cur_theme_root, template_path,
                                                 template_name)
                origin = Origin(name=template_file,
                                template_name=template_name,
                                loader=self)
                origin.theme = cur_theme
                origin.use_s3_theme = use_s3_theme
                yield origin
示例#6
0
    def get_template(self, *args, **kwargs):
        request = get_current_request()
        disable_cache = (request and 'theme' in request.session)

        if not disable_cache:
            return super(CachedLoader, self).get_template(*args, **kwargs)

        # django.template.loaders.cached.Loader calls super().get_template()
        # (where super is django.template.loaders.base.Loader) to get templates
        # that are not in the cache.  To skip the cache, we do the same, except
        # we must call django.template.loaders.base.Loader directly instead of
        # using super() since super is django.template.loaders.cached.Loader in
        # this case.
        return DjangoLoader.get_template(self, *args, **kwargs)