Python django.contrib.auth.decorators.permission_required() Examples

The following are 11 code examples of django.contrib.auth.decorators.permission_required(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.contrib.auth.decorators , or try the search function .
Example #1
Source File: views.py    From sensu_drive with MIT License 6 votes vote down vote up
def rules(request):
    
    mimetype = 'application/json'
    data = {}
    
    return HttpResponse(json.dumps(data), mimetype)
    

#@permission_required('is_staff', login_url=reverse_lazy('login'))
#@login_required(login_url=reverse_lazy('login')) 
Example #2
Source File: test_decorators.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_many_permissions_pass(self):

        @permission_required(['auth_tests.add_customuser', 'auth_tests.change_customuser'])
        def a_view(request):
            return HttpResponse()
        request = self.factory.get('/rand')
        request.user = self.user
        resp = a_view(request)
        self.assertEqual(resp.status_code, 200) 
Example #3
Source File: test_decorators.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_many_permissions_in_set_pass(self):

        @permission_required({'auth_tests.add_customuser', 'auth_tests.change_customuser'})
        def a_view(request):
            return HttpResponse()
        request = self.factory.get('/rand')
        request.user = self.user
        resp = a_view(request)
        self.assertEqual(resp.status_code, 200) 
Example #4
Source File: test_decorators.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_single_permission_pass(self):

        @permission_required('auth_tests.add_customuser')
        def a_view(request):
            return HttpResponse()
        request = self.factory.get('/rand')
        request.user = self.user
        resp = a_view(request)
        self.assertEqual(resp.status_code, 200) 
Example #5
Source File: test_decorators.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_permissioned_denied_redirect(self):

        @permission_required(['auth_tests.add_customuser', 'auth_tests.change_customuser', 'nonexistent-permission'])
        def a_view(request):
            return HttpResponse()
        request = self.factory.get('/rand')
        request.user = self.user
        resp = a_view(request)
        self.assertEqual(resp.status_code, 302) 
Example #6
Source File: test_decorators.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_permissioned_denied_exception_raised(self):

        @permission_required([
            'auth_tests.add_customuser', 'auth_tests.change_customuser', 'nonexistent-permission'
        ], raise_exception=True)
        def a_view(request):
            return HttpResponse()
        request = self.factory.get('/rand')
        request.user = self.user
        with self.assertRaises(PermissionDenied):
            a_view(request) 
Example #7
Source File: test_decorators.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_many_permissions_in_set_pass(self):

        @permission_required({'auth_tests.add_customuser', 'auth_tests.change_customuser'})
        def a_view(request):
            return HttpResponse()
        request = self.factory.get('/rand')
        request.user = self.user
        resp = a_view(request)
        self.assertEqual(resp.status_code, 200) 
Example #8
Source File: test_decorators.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_single_permission_pass(self):

        @permission_required('auth_tests.add_customuser')
        def a_view(request):
            return HttpResponse()
        request = self.factory.get('/rand')
        request.user = self.user
        resp = a_view(request)
        self.assertEqual(resp.status_code, 200) 
Example #9
Source File: test_decorators.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_permissioned_denied_redirect(self):

        @permission_required(['auth_tests.add_customuser', 'auth_tests.change_customuser', 'nonexistent-permission'])
        def a_view(request):
            return HttpResponse()
        request = self.factory.get('/rand')
        request.user = self.user
        resp = a_view(request)
        self.assertEqual(resp.status_code, 302) 
Example #10
Source File: test_decorators.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_permissioned_denied_exception_raised(self):

        @permission_required([
            'auth_tests.add_customuser', 'auth_tests.change_customuser', 'nonexistent-permission'
        ], raise_exception=True)
        def a_view(request):
            return HttpResponse()
        request = self.factory.get('/rand')
        request.user = self.user
        with self.assertRaises(PermissionDenied):
            a_view(request) 
Example #11
Source File: extension.py    From django-bananas with MIT License 5 votes vote down vote up
def admin_view(self, view, perm=None):
        if perm is not None:
            perm = self.get_permission(perm)
        else:
            perm = self.access_permission

        admin_login_url = compat.reverse_lazy("admin:login")
        view = user_passes_test(
            lambda u: u.is_active and u.is_staff, login_url=admin_login_url
        )(view)
        view = permission_required(perm, login_url=admin_login_url)(view)
        return view