Skip to content

Commit

Permalink
Merge pull request #5 from dmpayton/master
Browse files Browse the repository at this point in the history
SudoMixin for class-based views
  • Loading branch information
mattrobenolt committed Sep 22, 2015
2 parents c4212cc + 437bad1 commit 48ddaf7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
20 changes: 19 additions & 1 deletion docs/usage/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ Once we have ``django-sudo`` :doc:`installed </getting-started/index>` and
redirected to a page and prompted for their password. After entering their password, they'll be
redirected back to this page to continue on what they were trying to do.

.. class:: sudo.mixins.SudoMixin

``SudoMixin`` provides an easy way to sudo a class-based view. Any view
that inherits from this mixin is automatically wrapped by the
``@sudo_required`` decorator.

This works well with the ``LoginRequiredMixin`` from
`django-braces <https://django-braces.rtfd.org/>`_:

.. code-block:: python
from django.views import generic
from braces.views import LoginRequiredMixin
from sudo.mixins import SudoMixin
class SuperSecretView(LoginRequiredMixin, SudoMixin, generic.TemplateView):
template_name = 'secret/super-secret.html'
.. method:: request.is_sudo()

Returns a boolean to indicate if the current request is in sudo mode or not. This gets added on by
Expand All @@ -36,7 +54,7 @@ the :class:`~sudo.middleware.SudoMiddleware`. This is an shortcut for calling
By default, you just need to add this into your ``MIDDLEWARE_CLASSES`` list.

.. method:: has_sudo_privileges(self, request)

Subclass and override :func:`~sudo.middleware.SudoMiddleware.has_sudo_privileges` if you'd like
to override the default behavior of :func:`request.is_sudo() <request.is_sudo()>`.

Expand Down
8 changes: 8 additions & 0 deletions sudo/mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from sudo.decorators import sudo_required


class SudoMixin(object):
@classmethod
def as_view(cls, **initkwargs):
view = super(SudoMixin, cls).as_view(**initkwargs)
return sudo_required(view)
24 changes: 24 additions & 0 deletions tests/mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from .base import BaseTestCase
from django.http import HttpResponse
from django.views import generic
from sudo.mixins import SudoMixin


class FooView(SudoMixin, generic.View):
def get(self, request):
return HttpResponse()

foo = FooView.as_view()


class SudoMixinTestCase(BaseTestCase):
def test_is_sudo(self):
self.request.is_sudo = lambda: True
response = foo(self.request)
self.assertEqual(response.status_code, 200)

def test_is_not_sudo(self):
self.request.is_sudo = lambda: False
response = foo(self.request)
self.assertEqual(response.status_code, 302)
self.assertEqual(response['Location'], '/sudo/?next=/foo')

0 comments on commit 48ddaf7

Please sign in to comment.