2
Your decorator should be like this
def user_passes_test(old_fuction):
def new_function(request, class_id, *args, **kwargs):
try:
class_ = UserClasses.objects.get(user=request.user, class_id=class_id)
except Exception as e:
return HttpResponse('ERROR: User not present in the class')
return old_fuction(request, class_id, *args, **kwargs)
return new_function
If the UserClasses
contains row with both user
and class_id
(assumes that user
is unique
), the view function will be executed.Otherwise it will return an Error response(ERROR: User not present in the class).
And you view function should be
@user_passes_test
@login_required
def view(request, class_id):
if you want the class_
object in the view function, you can do it by simple changes. modify your decorator like
def user_passes_test(old_fuction):
def new_function(request, class_id, *args, **kwargs):
try:
class_ = UserClasses.objects.get(user=request.user, class_id=class_id)
except Exception as e:
return HttpResponse('ERROR: User not present in the class')
return old_fuction(request, class_id, class_, *args, **kwargs)
return new_function
And the view function should be
@user_passes_test
@login_required
def view(request, class_id, class_obj):
where class_obj
contains the class_
object
Source:stackexchange.com