0

Hi I have a project with many methods in view. Each method mapped to different urls.

views.py

def method1(request):
    print 'hello world'

def method2(request):
    print 'hello world2'

def method3(request):
    print 'hello world3'

What i want is to do something related to permission so that the user can access only one method. e.g That is if user has permission for method2(), he/she can only access this method. He/she mustn't access method1() or method2().

Like described here https://djangosnippets.org/snippets/1703/, I can use user_pass_test() function but in this way i can restrict only one.

If I want to restrict all except one i have to apply decorators in all existing methods and there are alot of methods in my project.

Here I want to restrict all except one.

Please help.

wrufesh
  • 1,379
  • 3
  • 18
  • 36

1 Answers1

1

Django's permissions might or not be the right way to tell if a given user can access a given view, but you don't tell enough about the context.

But Anyway... There are mainly two solutions here: either decorate each and every view with the appropriate user_passes_test(yourtesthere) or permission_required(perm) decorator, or use a custom middleware with the process_view method.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118