Skip to content

Instantly share code, notes, and snippets.

@montylounge
Created March 2, 2012 15:50
Show Gist options
  • Save montylounge/1959270 to your computer and use it in GitHub Desktop.
Save montylounge/1959270 to your computer and use it in GitHub Desktop.
Reusable Django Mixins from brack3t
# "Our Custom Mixins"
# http://brack3t.com/our-custom-mixins.html
class LoginRequiredMixin(object):
"""
View mixin which verifies that the user has authenticated.
NOTE:
This should be the left-most mixin of a view.
"""
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)
class PermissionRequiredMixin(object):
"""
View mixin which verifies that the logged in user has the specified
permission.
Class Settings
`permission_required` - the permission to check for.
`login_url` - the login url of site
`redirect_field_name` - defaults to "next"
`raise_exception` - defaults to False - raise 403 if set to True
Example Usage
class SomeView(PermissionRequiredMixin, ListView):
...
# required
permission_required = "app.permission"
# optional
login_url = "/signup/"
redirect_field_name = "hollaback"
raise_exception = True
...
"""
login_url = settings.LOGIN_URL
permission_required = None
raise_exception = False
redirect_field_name = REDIRECT_FIELD_NAME
def dispatch(self, request, *args, **kwargs):
# Verify class settings
if self.permission_required == None or len(
self.permission_required.split(".")) != 2:
raise ImproperlyConfigured("'PermissionRequiredMixin' requires "
"'permission_required' attribute to be set.")
has_permission = request.user.has_perm(self.permission_required)
if not has_permission:
if self.raise_exception:
return HttpResponseForbidden()
else:
path = urlquote(request.get_full_path())
tup = self.login_url, self.redirect_field_name, path
return HttpResponseRedirect("%s?%s=%s" % tup)
return super(PermissionRequiredMixin, self).dispatch(
request, *args, **kwargs)
class SuperuserRequiredMixin(object):
login_url = settings.LOGIN_URL
raise_exception = False
redirect_field_name = REDIRECT_FIELD_NAME
def dispatch(self, request, *args, **kwargs):
if not request.user.is_superuser:
if self.raise_exception:
return HttpResponseForbidden()
else:
path = urlquote(request.get_full_path())
tup = self.login_url, self.redirect_field_name, path
return HttpResponseRedirect("%s?%s=%s" % tup)
return super(SuperuserRequiredMixin, self).dispatch(
request, *args, **kwargs)
class UserFormKwargsMixin(object):
"""
CBV mixin which puts the user from the request into the form kwargs.
Note: Using this mixin requires you to pop the `user` kwarg
out of the dict in the super of your form's `__init__`.
"""
def get_form_kwargs(self, **kwargs):
kwargs = super(UserFormKwargsMixin, self).get_form_kwargs(**kwargs)
kwargs.update({"user": self.request.user})
return kwargs
class UserKwargModelFormMixin(object):
"""
Generic model form mixin for popping user out of the kwargs and
attaching it to the instance.
This mixin must precede forms.ModelForm/forms.Form. The form is not
expecting these kwargs to be passed in, so they must be poppped off before
anything else is done.
"""
def __init__(self, *args, **kwargs):
self.user = kwargs.pop("user", None)
super(UserKwargModelFormMixin, self).__init__(*args, **kwargs)
class SuccessURLRedirectListMixin(object):
"""
Simple CBV mixin which sets the success url to the list view of
a given app. Set success_list_url as a class attribute of your
CBV and don't worry about overloading the get_success_url.
This is only to be used for redirecting to a list page. If you need
to reverse the url with kwargs, this is not the mixin to use.
"""
success_list_url = None
def get_success_url(self):
return reverse(self.success_list_url)
class SetHeadlineMixin(object):
"""
Mixin allows you to set a static headline through a static property on the
class or programmatically by overloading the get_headline method.
"""
headline = None
def get_context_data(self, **kwargs):
kwargs = super(SetHeadlineMixin, self).get_context_data(**kwargs)
kwargs.update({"headline": self.get_headline()})
return kwargs
def get_headline(self):
if self.headline is None:
raise ImproperlyConfigured(u"%(cls)s is missing a headline. Define "
u"%(cls)s.headline, or override "
u"%(cls)s.get_headline()." % {"cls": self.__class__.__name__
})
return self.headline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment