from __future__ import unicode_literals

# Authentication and Authorisation
from functools import wraps

from . import http


def permit(test_func):
    '''Decorate a handler to control access'''
    def decorator(view_func):
        @wraps(view_func)
        def _wrapped_view(self, *args, **kwargs):
            if test_func(self, *args, **kwargs):
                return view_func(self, *args, **kwargs)
            return http.Forbidden()
        return _wrapped_view
    return decorator

permit_logged_in = permit(
    lambda self, *args, **kwargs: self.request.user.is_authenticated()
)

permit_staff = permit(
    lambda self, *args, **kwargs: self.request.user.is_staff
)


def permit_groups(*groups):
    def in_groups(self, *args, **kwargs):
        return self.request.user.groups.filter(name__in=groups).exists()
    return permit(in_groups)

from __future__ import unicode_literals

# Authentication and Authorisation
from functools import wraps

from . import http


def permit(test_func, response_class=http.Forbidden):
    '''Decorate a handler to control access'''
    def decorator(view_func):
        @wraps(view_func)
        def _wrapped_view(self, *args, **kwargs):
            if test_func(self, *args, **kwargs):
                return view_func(self, *args, **kwargs)
            return response_class()
        return _wrapped_view
    return decorator

permit_logged_in = permit(
    lambda self, *args, **kwargs: self.request.user.is_authenticated()
)

permit_staff = permit(
    lambda self, *args, **kwargs: self.request.user.is_staff
)


def permit_groups(*groups):
    def in_groups(self, *args, **kwargs):
        return self.request.user.groups.filter(name__in=groups).exists()
    return permit(in_groups)

Allow control of response type for failing permit check
Allow control of response type for failing permit check

  from __future__ import unicode_literals
  
  # Authentication and Authorisation
  from functools import wraps
  
  from . import http
  
  
- def permit(test_func):
+ def permit(test_func, response_class=http.Forbidden):
      '''Decorate a handler to control access'''
      def decorator(view_func):
          @wraps(view_func)
          def _wrapped_view(self, *args, **kwargs):
              if test_func(self, *args, **kwargs):
                  return view_func(self, *args, **kwargs)
-             return http.Forbidden()
+             return response_class()
          return _wrapped_view
      return decorator
  
  permit_logged_in = permit(
      lambda self, *args, **kwargs: self.request.user.is_authenticated()
  )
  
  permit_staff = permit(
      lambda self, *args, **kwargs: self.request.user.is_staff
  )
  
  
  def permit_groups(*groups):
      def in_groups(self, *args, **kwargs):
          return self.request.user.groups.filter(name__in=groups).exists()
      return permit(in_groups)
  
Allow control of response type for failing permit check
## Code Before:
from __future__ import unicode_literals

# Authentication and Authorisation
from functools import wraps

from . import http


def permit(test_func):
    '''Decorate a handler to control access'''
    def decorator(view_func):
        @wraps(view_func)
        def _wrapped_view(self, *args, **kwargs):
            if test_func(self, *args, **kwargs):
                return view_func(self, *args, **kwargs)
            return http.Forbidden()
        return _wrapped_view
    return decorator

permit_logged_in = permit(
    lambda self, *args, **kwargs: self.request.user.is_authenticated()
)

permit_staff = permit(
    lambda self, *args, **kwargs: self.request.user.is_staff
)


def permit_groups(*groups):
    def in_groups(self, *args, **kwargs):
        return self.request.user.groups.filter(name__in=groups).exists()
    return permit(in_groups)

## Instruction:
Allow control of response type for failing permit check
## Code After:
from __future__ import unicode_literals

# Authentication and Authorisation
from functools import wraps

from . import http


def permit(test_func, response_class=http.Forbidden):
    '''Decorate a handler to control access'''
    def decorator(view_func):
        @wraps(view_func)
        def _wrapped_view(self, *args, **kwargs):
            if test_func(self, *args, **kwargs):
                return view_func(self, *args, **kwargs)
            return response_class()
        return _wrapped_view
    return decorator

permit_logged_in = permit(
    lambda self, *args, **kwargs: self.request.user.is_authenticated()
)

permit_staff = permit(
    lambda self, *args, **kwargs: self.request.user.is_staff
)


def permit_groups(*groups):
    def in_groups(self, *args, **kwargs):
        return self.request.user.groups.filter(name__in=groups).exists()
    return permit(in_groups)
