50 views
Skip to first unread message

Thomas Ménès

unread,
Feb 13, 2023, 3:54:30 PM2/13/23
to django-d...@googlegroups.com
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

Adam Johnson

unread,
Feb 15, 2023, 12:09:21 PM2/15/23
to django-d...@googlegroups.com
Please don’t email random code snippets to this list.

--
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-develop...@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.
Reply all
Reply to author
Forward
0 new messages