示例#1
0
def theme_picker(request, template_name="theme_editor/theme_picker.html"):
    if not request.user.profile.is_superuser:
        raise Http403

    themes = []
    for theme in theme_choices():
        theme_info = ThemeInfo(theme)
        themes.append(theme_info)

    if request.method == "POST":
        selected_theme = request.POST.get('theme')
        if not is_valid_theme(selected_theme):
            raise Http403
        call_command('set_theme', selected_theme)
        checklist_update('choose-theme')
        msg_string = "Your theme has been changed to %s." % selected_theme.title(
        )
        messages.add_message(request, messages.SUCCESS, _(msg_string))
        return redirect('home')

    active_theme = get_active_theme()
    themes = sorted(themes, key=lambda theme: theme.create_dt)

    return render_to_resp(request=request,
                          template_name=template_name,
                          context={
                              'themes': themes,
                              'current_theme': active_theme,
                              'theme_choices': theme_choices(),
                          })
示例#2
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
示例#3
0
def theme(request):
    context = {}

    if 'theme' in request.GET and request.user.profile.is_superuser:
        theme = request.GET.get('theme')
        if theme:
            request.session['theme'] = theme
        elif 'theme' in request.session:
            del request.session['theme']

    context['ACTIVE_THEME'] = get_active_theme()
    context['THEME'] = theme = get_theme(context['ACTIVE_THEME'])

    context['THEME_INFO'] = get_theme_info(theme)

    # Backward compatibility for old themes
    def warn_theme_urls(value):
        warn("{{ THEME_URL }}media/<path> is deprecated, use {% static '<path>' %} instead", DeprecationWarning)
        return value
    if is_builtin_theme(theme):
        theme_url = '%sthemes/%s/'%(settings.STATIC_URL, get_builtin_theme_dir(theme))
        def warn_theme_url(value=theme_url):  # noqa: E306
            return warn_theme_urls(value)
        context['THEME_URL'] = warn_theme_url
    elif settings.USE_S3_STORAGE:
        theme_url = '%s/%s/%s/themes/%s/'%(
            settings.S3_ROOT_URL, settings.AWS_STORAGE_BUCKET_NAME, settings.AWS_LOCATION, theme)
        def warn_theme_url(value=theme_url):  # noqa: E306
            return warn_theme_urls(value)
        context['THEME_URL'] = warn_theme_url
    else:
        theme_url = '/themes/'+theme+'/'
        def warn_theme_url(value=theme_url):  # noqa: E306
            return warn_theme_urls(value)
        context['THEME_URL'] = warn_theme_url
    local_theme_url = '/themes/'+theme+'/'
    def warn_local_theme_url(value=local_theme_url):  # noqa: E306
        warn("{{ LOCAL_THEME_URL }}media/<path> is deprecated, use {% local_static '<path>' %} instead", DeprecationWarning)
        return value
    context['LOCAL_THEME_URL'] = warn_local_theme_url

    return context
示例#4
0
    def handle_simple(cls, path, local_only, template=None, theme=None):

        active_theme = get_active_theme()
        theme = get_theme(active_theme)
        global _cached_theme_search_info
        cached_theme, theme_search_info = _cached_theme_search_info

        # If the theme changed or the user is previewing a different theme,
        # update _cached_theme_search_info.
        # Note that _cached_theme_search_info may be shared between multiple
        # threads, so you must be careful when reading/writing
        # _cached_theme_search_info to ensure that writes in one thread cannot
        # cause unexpected behavior in another thread that is reading/writing
        # _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):
                    cur_theme_dir = get_builtin_theme_dir(cur_theme)
                    static_path = os.path.join(settings.STATIC_ROOT, 'themes',
                                               cur_theme_dir)
                    if not os.path.isdir(static_path):
                        continue
                    local_static_url = '%sthemes/%s/' % (
                        settings.LOCAL_STATIC_URL, cur_theme_dir)
                    static_url = '%sthemes/%s/' % (settings.STATIC_URL,
                                                   cur_theme_dir)
                    theme_search_info.append(
                        (static_path, local_static_url, static_url))
                else:
                    cur_theme_root = get_theme_root(cur_theme)
                    for static_dir in ['media', 'static']:
                        static_path = os.path.join(cur_theme_root, static_dir)
                        if not os.path.isdir(static_path):
                            continue
                        local_static_url = static_url = '/themes/' + cur_theme + '/' + static_dir + '/'
                        if settings.USE_S3_STORAGE:
                            static_url = '%s/%s/%s/themes/%s/%s/' % (
                                settings.S3_ROOT_URL,
                                settings.AWS_STORAGE_BUCKET_NAME,
                                settings.AWS_LOCATION, cur_theme, static_dir)
                        theme_search_info.append(
                            (static_path, local_static_url, static_url))
            if theme == active_theme:
                _cached_theme_search_info = (theme, theme_search_info)

        # Search for static file in themes
        for static_path, local_static_url, static_url in theme_search_info:
            if not os.path.exists(os.path.join(static_path, path)):
                continue
            return urljoin((local_static_url if local_only else static_url),
                           quote(path))

        # Warn about static files that don't exist in either a theme or
        # STATIC_ROOT
        if not os.path.exists(os.path.join(settings.STATIC_ROOT, path)):
            if not template:
                call = ('local_static' if local_only else 'static')
                warn('%s() call references non-existent static path "%s"' %
                     (call, path))
            else:
                tag = ('{% local_static %}' if local_only else '{% static %}')
                theme_str = ('theme "%s"' %
                             (theme) if theme else 'an installed Django app')
                warn(
                    '%s in template "%s" in %s references non-existent static path "%s"'
                    % (tag, template, theme_str, path))

        # Handle {% local_static %} for files not found in a theme
        if local_only:
            return urljoin(settings.LOCAL_STATIC_URL, quote(path))

        # Default to standard Django {% static %} behavior
        return super(ThemeStaticNode, cls).handle_simple(path)