Exemplo n.º 1
0
def test_theme_activation():
    with override_current_theme_class():
        with override_provides("xtheme", [
            "shoop_tests.xtheme.utils:FauxTheme",
            "shoop_tests.xtheme.utils:FauxTheme2"
        ]):
            ThemeSettings.objects.all().delete()
            assert not get_current_theme()
            set_current_theme(FauxTheme.identifier)
            assert isinstance(get_current_theme(), FauxTheme)
            set_current_theme(FauxTheme2.identifier)
            assert isinstance(get_current_theme(), FauxTheme2)
            with pytest.raises(ValueError):
                set_current_theme(printable_gibberish())
Exemplo n.º 2
0
def get_view_config(context):
    """
    Get a view configuration object for a Jinja2 rendering context.

    :param context: Rendering context
    :type context: jinja2.runtime.Context
    :return: View config
    :rtype: shoop.xtheme.view_config.ViewConfig
    """
    # This uses the Jinja context's technically-immutable vars dict
    # to cache the view configuration. This is fine in our case, I'd say.
    request = context.get("request")
    config = context.vars.get("_xtheme_view_config")
    if config is None:
        view_object = context.get("view")
        if view_object:
            view_class = view_object.__class__
            view_name = view_class.__name__
        else:
            view_name = "UnknownView"
        config = ViewConfig(
            theme=get_current_theme(request),
            view_name=view_name,
            draft=is_edit_mode(request)
        )
        context.vars["_xtheme_view_config"] = config
    return config
Exemplo n.º 3
0
def get_view_config(context):
    """
    Get a view configuration object for a Jinja2 rendering context.

    :param context: Rendering context
    :type context: jinja2.runtime.Context
    :return: View config
    :rtype: shoop.xtheme.view_config.ViewConfig
    """
    # This uses the Jinja context's technically-immutable vars dict
    # to cache the view configuration. This is fine in our case, I'd say.
    request = context.get("request")
    config = context.vars.get("_xtheme_view_config")
    if config is None:
        view_object = context.get("view")
        if view_object:
            view_class = view_object.__class__
            view_name = view_class.__name__
        else:
            view_name = "UnknownView"
        config = ViewConfig(
            theme=get_current_theme(request),
            view_name=view_name,
            draft=is_edit_mode(request)
        )
        context.vars["_xtheme_view_config"] = config
    return config
Exemplo n.º 4
0
 def get_context_data(self, **kwargs):
     context = super(ThemeConfigView, self).get_context_data(**kwargs)
     context["theme_classes"] = sorted(
         [t for t in get_provide_objects("xtheme") if t.identifier],
         key=lambda t: (t.name or t.identifier)
     )
     context["current_theme"] = get_current_theme()
     return context
Exemplo n.º 5
0
 def get_context_data(self, **kwargs):
     context = super(ThemeConfigView, self).get_context_data(**kwargs)
     context["theme_classes"] = sorted(
         [t for t in get_provide_objects("xtheme") if t.identifier],
         key=lambda t: (t.name or t.identifier)
     )
     context["current_theme"] = get_current_theme()
     return context
Exemplo n.º 6
0
    def get_notifications(self, request):
        try:
            engine = engines["jinja2"]
        except KeyError:
            engine = None

        if engine and isinstance(engine, Jinja2):  # The engine is what we expect...
            if isinstance(engine.env, XthemeEnvironment):  # ... and it's capable of loading themes...
                if not get_current_theme(request):  # ... but there's no theme active?!
                    # Panic!
                    yield Notification(
                        text=_("No theme is active. Click here to activate one."),
                        title=_("Theming"),
                        url="shoop_admin:xtheme.config"
                    )
Exemplo n.º 7
0
def extra_view_dispatch(request, view):
    """
    Dispatch to an Xtheme extra view.

    :param request: A request
    :type request: django.http.HttpRequest
    :param view: View name
    :type view: str
    :return: A response of some ilk
    :rtype: django.http.HttpResponse
    """
    theme = get_current_theme(request)
    view_func = get_view_by_name(theme, view)
    if not view_func:
        msg = "%s/%s: Not found" % (getattr(theme, "identifier", None), view)
        return HttpResponseNotFound(msg)
    return view_func(request)
Exemplo n.º 8
0
    def get_notifications(self, request):
        try:
            engine = engines["jinja2"]
        except KeyError:
            engine = None

        if engine and isinstance(engine,
                                 Jinja2):  # The engine is what we expect...
            if isinstance(engine.env, XthemeEnvironment
                          ):  # ... and it's capable of loading themes...
                if not get_current_theme(
                        request):  # ... but there's no theme active?!
                    # Panic!
                    yield Notification(text=_(
                        "No theme is active. Click here to activate one."),
                                       title=_("Theming"),
                                       url="shoop_admin:xtheme.config")
Exemplo n.º 9
0
def extra_view_dispatch(request, view):
    """
    Dispatch to an Xtheme extra view.

    :param request: A request
    :type request: django.http.HttpRequest
    :param view: View name
    :type view: str
    :return: A response of some ilk
    :rtype: django.http.HttpResponse
    """
    theme = get_current_theme(request)
    view_func = get_view_by_name(theme, view)
    if not view_func:
        msg = "%s/%s: Not found" % (getattr(theme, "identifier", None), view)
        return HttpResponseNotFound(msg)
    return view_func(request)
Exemplo n.º 10
0
    def __getitem__(self, item):
        """
        Look for additional helper callables in the active theme.

        Callables marked with the Django standard `alters_data` attribute will not be honored.

        :param item: Template helper name
        :type item: str
        :return: Template helper, maybe
        :rtype: object|None
        """
        theme = get_current_theme()
        if theme:
            helper = getattr(theme, item, None)
            if helper and callable(helper) and not getattr(helper, "alters_data", False):
                return helper
        raise KeyError("No such template helper: %s" % item)
Exemplo n.º 11
0
    def get(self, context, name, default=None):
        """
        Get a theme setting value.

        :param context: Implicit Jinja2 context
        :type context: jinja2.runtime.Context
        :param name: Setting name
        :type name: str
        :param default: Default value if setting is not found
        :type default: object
        :return: Value
        :rtype: object
        """
        request = context["request"]
        theme = get_current_theme(request)
        if theme:
            return theme.get_setting(name, default=default)
        return default
Exemplo n.º 12
0
    def __getitem__(self, item):
        """
        Look for additional helper callables in the active theme.

        Callables marked with the Django standard `alters_data` attribute will not be honored.

        :param item: Template helper name
        :type item: str
        :return: Template helper, maybe
        :rtype: object|None
        """
        theme = get_current_theme()
        if theme:
            helper = getattr(theme, item, None)
            if helper and callable(helper) and not getattr(
                    helper, "alters_data", False):
                return helper
        raise KeyError("No such template helper: %s" % item)
Exemplo n.º 13
0
    def get(self, context, name, default=None):
        """
        Get a theme setting value.

        :param context: Implicit Jinja2 context
        :type context: jinja2.runtime.Context
        :param name: Setting name
        :type name: str
        :param default: Default value if setting is not found
        :type default: object
        :return: Value
        :rtype: object
        """
        request = context["request"]
        theme = get_current_theme(request)
        if theme:
            return theme.get_setting(name, default=default)
        return default
Exemplo n.º 14
0
    def _get_themed_template_names(self, name):
        """
        Get theme-prefixed paths for the given template name.

        For instance, if the template_dir or identifier of the current theme is `mystery` and we're looking up
        `shoop/front/bar.jinja`, we'll look at `mystery/shoop/front/bar.jinja`, then at `shoop/front/bar.jinja`.

        :param name: Template name
        :type name: str
        :return: A template name or a list thereof
        :rtype: str|list[str]
        """
        if name.startswith("shoop/admin"):  # Ignore the admin.
            return name
        theme = get_current_theme()
        if not theme:
            return name
        return [
            "%s/%s" % ((theme.template_dir or theme.identifier), name), name
        ]
Exemplo n.º 15
0
    def _get_themed_template_names(self, name):
        """
        Get theme-prefixed paths for the given template name.

        For instance, if the template_dir or identifier of the current theme is `mystery` and we're looking up
        `shoop/front/bar.jinja`, we'll look at `mystery/shoop/front/bar.jinja`, then at `shoop/front/bar.jinja`.

        :param name: Template name
        :type name: str
        :return: A template name or a list thereof
        :rtype: str|list[str]
        """
        if name.startswith("shoop/admin"):  # Ignore the admin.
            return name
        theme = get_current_theme()
        if not theme:
            return name
        return [
            "%s/%s" % ((theme.template_dir or theme.identifier), name),
            name
        ]