Created
July 11, 2012 19:25
-
-
Save robgolding/3092600 to your computer and use it in GitHub Desktop.
Django Class-Based View Mixins: Part 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LoginRequiredMixin(object): | |
""" | |
View mixin which requires that the user is authenticated. | |
""" | |
@method_decorator(login_required) | |
def dispatch(self, request, *args, **kwargs): | |
return super(LoginRequiredMixin, self).dispatch( | |
self, request, *args, **kwargs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PermissionsRequiredMixin(object): | |
""" | |
View mixin which verifies that the logged in user has the specified | |
permissions. | |
Settings: | |
`required_permissions` - list/tuple of required permissions | |
Example Usage: | |
class SomeView(PermissionsRequiredMixin, ListView): | |
... | |
required_permissions = ( | |
'app1.permission_a', | |
'app2.permission_b', | |
) | |
... | |
""" | |
required_permissions = () | |
@method_decorator(login_required) | |
def dispatch(self, request, *args, **kwargs): | |
if not request.user.has_perms(self.required_permissions): | |
messages.error( | |
request, | |
'You do not have the permission required to perform the ' | |
'requested operation.') | |
return redirect(settings.LOGIN_URL) | |
return super(PermissionsRequiredMixin, self).dispatch( | |
request, *args, **kwargs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StaffRequiredMixin(object): | |
""" | |
View mixin which requires that the authenticated user is a staff member | |
(i.e. `is_staff` is True). | |
""" | |
@method_decorator(login_required) | |
def dispatch(self, request, *args, **kwargs): | |
if not request.user.is_staff: | |
messages.error( | |
request, | |
'You do not have the permission required to perform the ' | |
'requested operation.') | |
return redirect(settings.LOGIN_URL) | |
return super(StaffRequiredMixin, self).dispatch(request, | |
*args, **kwargs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SuperUserRequiredMixin(object): | |
""" | |
View mixin which requires that the authenticated user is a super user | |
(i.e. `is_superuser` is True). | |
""" | |
@method_decorator(login_required) | |
def dispatch(self, request, *args, **kwargs): | |
if not request.user.is_superuser: | |
messages.error( | |
request, | |
'You do not have the permission required to perform the ' | |
'requested operation.') | |
return redirect(settings.LOGIN_URL) | |
return super(SuperUserRequiredMixin, self).dispatch(request, | |
*args, **kwargs) |
Rob, there is an easier way to do this - by re-writing the as_view() method. Checkout the comments here here: https://code.djangoproject.com/ticket/16626#no1
Relevant code:
class LoginRequiredMixin(object):
def as_view(cls):
return login_required(super(LoginRequiredMixin, cls).as_view())
This was much easier for me to implement the LoginRequired Mixin.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Really nice mixins. =)