Example #1
0
    def render(self, context):
        """Render the node.

        This will determine if the feature is enabled or disabled, and render
        the appropriate list of nodes to a string.

        Args:
            context (django.template.Context):
                The context provided by the template.

        Returns:
            unicode:
            The rendered content as a string.
        """
        feature_id = self.feature_id.resolve(context, True)
        extra_kwargs = {
            key: value.resolve(context)
            for key, value in six.iteritems(self.extra_kwargs)
        }

        feature = get_features_registry().get_feature(feature_id)

        if feature:
            enabled = feature.is_enabled(request=context.get('request'),
                                         **extra_kwargs)
        else:
            enabled = False

        if enabled:
            return self.nodelist_enabled.render(context)
        else:
            return self.nodelist_disabled.render(context)
Example #2
0
    def render(self, context):
        """Render the node.

        This will determine if the feature is enabled or disabled, and render
        the appropriate list of nodes to a string.

        Args:
            context (django.template.Context):
                The context provided by the template.

        Returns:
            unicode:
            The rendered content as a string.
        """
        feature_id = self.feature_id.resolve(context, True)
        extra_kwargs = {
            key: value.resolve(context)
            for key, value in six.iteritems(self.extra_kwargs)
        }

        feature = get_features_registry().get_feature(feature_id)

        if feature:
            enabled = feature.is_enabled(request=context.get('request'),
                                         **extra_kwargs)
        else:
            enabled = False

        if enabled:
            return self.nodelist_enabled.render(context)
        else:
            return self.nodelist_disabled.render(context)
Example #3
0
    def __init__(self, register=True):
        """Initialize the feature.

        Subclasses that wish to provide special initialization should instead
        override :py:meth:`initialize`.

        Args:
            register (bool, optional):
                Whether to register this feature instance. This should
                generally be ``True`` for all callers, except in special cases
                (like unit tests).

        Raises:
            djblets.features.errors.FeatureConflictError:
                The feature ID on this class conflicts with another feature.
        """
        if register:
            get_features_registry().register(self)
Example #4
0
    def __init__(self, register=True):
        """Initialize the feature.

        Subclasses that wish to provide special initialization should instead
        override :py:meth:`initialize`.

        Args:
            register (bool, optional):
                Whether to register this feature instance. This should
                generally be ``True`` for all callers, except in special cases
                (like unit tests).

        Raises:
            djblets.features.errors.FeatureConflictError:
                The feature ID on this class conflicts with another feature.
        """
        if register:
            get_features_registry().register(self)
Example #5
0
def override_feature_checks(feature_states):
    """Override multiple features for a test.

    Unit tests can make use of this context manager to ensure that one or more
    features have particular enabled/disabled states before executing any code
    dependent on those features.

    Only the provided features will be modified, with all other feature logic
    falling back to the default behavior for the configured feature checker.

    Version Change:
        1.0.13:
        ``feature_states`` now accepts a
        :py:class:`~djblets.features.feature.Feature` instance as a key.

    Args:
        feature_states (dict):
            A dictionary of feature IDs or instances to booleans (representing
            whether the feature is enabled).

    Example:
        .. code-block:: python

           from myproject.features import my_feature_3

           feature_states = {
               'my-feature-1': True,
               'my-feature-2': False,
               my_feature_3: True,
           }

           with override_feature_checks(feature_states):
               # Your test code here.
    """
    registry = get_features_registry()
    old_state = []

    for feature, enabled in six.iteritems(feature_states):
        if not isinstance(feature, Feature):
            feature = registry.get_feature(feature)

        old_state.append({
            'feature': feature,
            'func': feature.is_enabled,
        })

        feature.is_enabled = \
            lambda _is_enabled=enabled, **kwargs: _is_enabled

    try:
        yield
    finally:
        for feature_info in old_state:
            feature_info['feature'].is_enabled = feature_info['func']
Example #6
0
def override_feature_checks(feature_states):
    """Override multiple features for a test.

    Unit tests can make use of this context manager to ensure that one or more
    features have particular enabled/disabled states before executing any code
    dependent on those features.

    Only the provided features will be modified, with all other feature logic
    falling back to the default behavior for the configured feature checker.

    Args:
        feature_states (dict):
            A dictionary of feature IDs to booleans (representing whether the
            feature is enabled).

    Example:
        .. code-block:: python

           feature_states = {
               'my-feature-1': True,
               'my-feature-2': False,
           }

           with override_feature_checks(feature_states):
               # Your test code here.
    """
    registry = get_features_registry()
    old_state = []

    for feature_id, enabled in six.iteritems(feature_states):
        feature = registry.get_feature(feature_id)

        old_state.append({
            'feature': feature,
            'func': feature.is_enabled,
        })

        feature.is_enabled = \
            lambda _is_enabled=enabled, **kwargs: _is_enabled

    yield

    for feature_info in old_state:
        feature_info['feature'].is_enabled = feature_info['func']