"""This module holds the decorators for the ``api`` app"""

from rest_framework_extensions.cache.decorators import CacheResponse


class ConditionalCacheResponse(CacheResponse):
    """Decorator to use to conditionally apply cache decorator"""

    def process_cache_response(self, view_instance, view_method, request, *args, **kwargs):
        """Conditionally check if we should cache the response

        For the parameters, see ``CacheResponse.process_cache_response``

        """

        # The ``should_be_cached`` method on the view instance returned True,
        # Call the appropriate method on ``CacheResponse``
        if view_instance.should_be_cached():
            return super().process_cache_response(
                view_instance,
                view_method,
                request,
                args,
                kwargs
            )

        # The ``should_be_cached`` method on the view instance returned False,
        # Just return the view without caching the response
        response = view_method(view_instance, request, *args, **kwargs)
        response = view_instance.finalize_response(request, response, *args, **kwargs)

        return response


conditional_cache_response = ConditionalCacheResponse  # pylint: disable=invalid-name
