示例#1
0
def override_current_theme_class(theme_class=_not_set, shop=None):
    """
    Context manager for overriding the currently active theme class for testing.

    An instance of this class is then returned by `get_current_theme`.

    A falsy value means `None` is returned from `get_current_theme`, which is also
    useful for testing.

    :param theme_class: A theme class object
    :type theme_class: class[Theme]
    """
    # Circular import avoidance:
    from shuup.xtheme.views.extra import clear_view_cache
    old_theme_class = cache.get(get_theme_cache_key(shop))

    if theme_class is _not_set or not theme_class:
        cache.set(get_theme_cache_key(shop), None)
    else:
        from shuup.xtheme.models import ThemeSettings
        theme_settings = ThemeSettings.objects.get_or_create(
            shop=shop, theme_identifier=theme_class.identifier)[0]
        theme = theme_class(theme_settings)
        set_middleware_current_theme(theme)
        cache.set(get_theme_cache_key(shop), theme)

    clear_view_cache()
    yield

    cache.set(get_theme_cache_key(shop), old_theme_class)
    clear_view_cache()
示例#2
0
文件: _theme.py 项目: ruqaiya/shuup
def override_current_theme_class(theme_class=_not_set, shop=None):
    """
    Context manager for overriding the currently active theme class for testing.

    An instance of this class is then returned by `get_current_theme`.

    A falsy value means `None` is returned from `get_current_theme`, which is also
    useful for testing.

    :param theme_class: A theme class object
    :type theme_class: class[Theme]
    """
    # Circular import avoidance:
    from shuup.xtheme.views.extra import clear_view_cache
    old_theme_class = cache.get(get_theme_cache_key(shop))

    if theme_class is _not_set or not theme_class:
        cache.set(get_theme_cache_key(shop), None)
    else:
        from shuup.xtheme.models import ThemeSettings
        theme_settings = ThemeSettings.objects.get_or_create(
            shop=shop,
            theme_identifier=theme_class.identifier
        )[0]
        theme = theme_class(theme_settings)
        set_middleware_current_theme(theme)
        cache.set(get_theme_cache_key(shop), theme)

    clear_view_cache()
    yield

    cache.set(get_theme_cache_key(shop), old_theme_class)
    clear_view_cache()
示例#3
0
def override_current_theme_class(theme_class=_not_set):
    """
    Context manager for overriding the currently active theme class for testing.

    An instance of this class is then returned by `get_current_theme`.

    A falsy value means `None` is returned from `get_current_theme`, which is also
    useful for testing.

    :param theme_class: A theme class object
    :type theme_class: class[Theme]
    """
    global _current_theme_class
    # Circular import avoidance:
    from shuup.xtheme.views.extra import clear_view_cache
    old_theme_class = _current_theme_class
    _current_theme_class = theme_class
    clear_view_cache()
    yield
    _current_theme_class = old_theme_class
    clear_view_cache()
示例#4
0
def override_current_theme_class(theme_class=_not_set):
    """
    Context manager for overriding the currently active theme class for testing.

    An instance of this class is then returned by `get_current_theme`.

    A falsy value means `None` is returned from `get_current_theme`, which is also
    useful for testing.

    :param theme_class: A theme class object
    :type theme_class: class[Theme]
    """
    global _current_theme_class
    # Circular import avoidance:
    from shuup.xtheme.views.extra import clear_view_cache
    old_theme_class = _current_theme_class
    _current_theme_class = theme_class
    clear_view_cache()
    yield
    _current_theme_class = old_theme_class
    clear_view_cache()