decorators.py 805 Bytes
Newer Older
Will Daly committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
"""Decorators for model-based configuration. """
from functools import wraps
from django.http import HttpResponseNotFound


def require_config(config_model):
    """View decorator that enables/disables a view based on configuration.

    Arguments:
        config_model (ConfigurationModel subclass): The class of the configuration
            model to check.

    Returns:
        HttpResponse: 404 if the configuration model is disabled,
            otherwise returns the response from the decorated view.

    """
    def _decorator(func):
        @wraps(func)
        def _inner(*args, **kwargs):
            if not config_model.current().enabled:
                return HttpResponseNotFound()
            else:
                return func(*args, **kwargs)
        return _inner
    return _decorator