Beispiel #1
0
 def get_ui_info_map(cls):
     map = {}
     for identifier, object in six.iteritems(get_identifier_to_object_map(cls.provide_category)):
         map[identifier] = {
             "identifier": str(identifier),
             "name": force_text(object.name),
             "description": force_text(getattr(object, "description", None) or ""),
         }
     return map
Beispiel #2
0
def test_provides():
    IDENTIFIED_OBJECT_SPEC = "%s:IdentifiedObject" % __name__
    category = str(uuid.uuid4())
    with override_provides(category, [
        IDENTIFIED_OBJECT_SPEC,
        "%s:UnidentifiedObject" % __name__,
        "%s:VeryUnidentifiedObject" % __name__,
    ]):
        objects = get_provide_objects(category)
        assert set(objects) == set((IdentifiedObject, UnidentifiedObject, VeryUnidentifiedObject))
        assert get_identifier_to_object_map(category)["identifier"] == IdentifiedObject
        assert get_identifier_to_spec_map(category)["identifier"] == IDENTIFIED_OBJECT_SPEC
        assert get_provide_specs_and_objects(category)[IDENTIFIED_OBJECT_SPEC] == IdentifiedObject

    # Test the context manager clears things correctly
    assert empty_iterable(get_provide_objects(category))
    assert empty_iterable(get_provide_specs_and_objects(category))
    assert empty_iterable(get_identifier_to_object_map(category))
    assert empty_iterable(get_identifier_to_spec_map(category))
Beispiel #3
0
    def load(cls, identifier):
        """
        Get a plugin class based on the identifier from the `xtheme_plugin` provides registry.

        :param identifier: Plugin class identifier
        :type identifier: str
        :return: A plugin class, or None
        :rtype: class[Plugin]|None
        """
        return get_identifier_to_object_map("xtheme_plugin").get(identifier)
Beispiel #4
0
def test_provides():
    IDENTIFIED_OBJECT_SPEC = "%s:IdentifiedObject" % __name__
    category = str(uuid.uuid4())
    with override_provides(category, [
        IDENTIFIED_OBJECT_SPEC,
        "%s:UnidentifiedObject" % __name__,
        "%s:VeryUnidentifiedObject" % __name__,
    ]):
        objects = get_provide_objects(category)
        assert set(objects) == set((IdentifiedObject, UnidentifiedObject, VeryUnidentifiedObject))
        assert get_identifier_to_object_map(category)["identifier"] == IdentifiedObject
        assert get_identifier_to_spec_map(category)["identifier"] == IDENTIFIED_OBJECT_SPEC
        assert get_provide_specs_and_objects(category)[IDENTIFIED_OBJECT_SPEC] == IdentifiedObject

    # Test the context manager clears things correctly
    assert empty_iterable(get_provide_objects(category))
    assert empty_iterable(get_provide_specs_and_objects(category))
    assert empty_iterable(get_identifier_to_object_map(category))
    assert empty_iterable(get_identifier_to_spec_map(category))
Beispiel #5
0
    def load(cls, identifier):
        """
        Get a plugin class based on the identifier from the `xtheme_plugin` provides registry.

        :param identifier: Plugin class identifier
        :type identifier: str
        :return: A plugin class, or None
        :rtype: class[Plugin]|None
        """
        return get_identifier_to_object_map("xtheme_plugin").get(identifier)
Beispiel #6
0
def get_current_theme(request=None):
    """
    Get the currently active theme object.

    :param request: Request, if available
    :type request: HttpRequest|None
    :return: Theme object or None
    :rtype: Theme
    """
    if _current_theme_class is not _not_set:
        if _current_theme_class:
            return _current_theme_class()
        return None  # No theme (usually for testing)

    if request and hasattr(request, "_current_xtheme"):
        return request._current_xtheme
    theme = None

    try:
        # Ensure this module can be imported from anywhere by lazily importing the model
        from shoop.xtheme.models import ThemeSettings
        ts = ThemeSettings.objects.filter(active=True).first()
    except Exception as exc:
        # This is unfortunate and weird, but I don't want other tests to depend
        # on Xtheme's state or require the `djangodb` mark for every test.
        # So we silence exceptions with pytest-django's "Database access not allowed"
        # message here and let everything else pass.
        if "Database access not allowed" not in str(exc):
            raise
        ts = None

    if ts:
        theme_cls = get_identifier_to_object_map("xtheme").get(
            ts.theme_identifier)
        if theme_cls is not None:
            theme = theme_cls(settings_obj=ts)
        else:
            log.warn("The active theme %r is not currently installed",
                     ts.theme_identifier)

    if request:
        request._current_xtheme = theme
    return theme
Beispiel #7
0
def get_current_theme(request=None):
    """
    Get the currently active theme object.

    :param request: Request, if available
    :type request: HttpRequest|None
    :return: Theme object or None
    :rtype: Theme
    """
    if _current_theme_class is not _not_set:
        if _current_theme_class:
            return _current_theme_class()
        return None  # No theme (usually for testing)

    if request and hasattr(request, "_current_xtheme"):
        return request._current_xtheme
    theme = None

    try:
        # Ensure this module can be imported from anywhere by lazily importing the model
        from shoop.xtheme.models import ThemeSettings
        ts = ThemeSettings.objects.filter(active=True).first()
    except Exception as exc:
        # This is unfortunate and weird, but I don't want other tests to depend
        # on Xtheme's state or require the `djangodb` mark for every test.
        # So we silence exceptions with pytest-django's "Database access not allowed"
        # message here and let everything else pass.
        if "Database access not allowed" not in str(exc):
            raise
        ts = None

    if ts:
        theme_cls = get_identifier_to_object_map("xtheme").get(ts.theme_identifier)
        if theme_cls is not None:
            theme = theme_cls(settings_obj=ts)
        else:
            log.warn("The active theme %r is not currently installed", ts.theme_identifier)

    if request:
        request._current_xtheme = theme
    return theme
Beispiel #8
0
 def class_for_identifier(cls, identifier):
     return get_identifier_to_object_map(cls.provide_category).get(identifier)