def extra_permissions(permission_classes):
    """Decorate a CBV method to add specific permissions on the instance."""
    def decorator(func):
        def decorated_func(self, *args, **kwargs):
            for permission_class in permission_classes:
                # Check the extra view level permissions.
                permission = permission_class()
                if not permission.has_permission(self.request, self):
                    self.permission_denied(
                        self.request,
                        message=getattr(permission, "message", None),
                        code=getattr(permission, "code", None)
                    )
                # Add them to permission classes for obj level checks.
                # Reassignment is for global tests as they call the same CBV 
intances.
                self.permission_classes = self.permission_classes + 
[permission_class]
            return func(self, *args, **kwargs)
        return decorated_func
    return decorator

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/08541759-4D69-4147-8009-0188C3CEED0D%40vingtcinq.io.
  • [no... Thomas Ménès
    • ... 'Adam Johnson' via Django developers (Contributions to Django itself)

Reply via email to