Skip to content

Instantly share code, notes, and snippets.

@lightstrike
Created November 3, 2014 18:38
Show Gist options
  • Save lightstrike/9cda4cba2b2efb6862d4 to your computer and use it in GitHub Desktop.
Save lightstrike/9cda4cba2b2efb6862d4 to your computer and use it in GitHub Desktop.
User View Mixins
class UserDetailAccessMixin(UserPassesTestMixin):
def test_func(self, user):
"""
Tests if user should be allowed access to DetailView if one of two conditions:
1. Has staff permissions
2. Is the customer associated with the object in a DetailView
"""
if user.is_staff:
return True
else:
detail_object = self.get_object()
return user == detail_object.customer
class UserViewMixin(object):
def get_object(self, queryset=None):
return self.request.user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment