Search code examples
djangodjango-sessions

Django- Session Variables in user_passes_test


I'd like to run method decorator @user_passes_test and use a session variable in that test:

def has_correct_search(user):
    source = request.session['source']
    if ApiSearch.objects.filter(user=user, source=source).exists():
        return True
    else:
        return False

The problem is that I don't know how to pass the request object:

@method_decorator(user_passes_test(has_correct_search, redirect_field_name=search_url_name))
    def dispatch(self, request, *args, **kwargs):
    (...)

Is there any trick to getting ahold of session variables in this case? What are my options?


Solution

  • I think I found my answer....

    https://docs.djangoproject.com/en/1.9/topics/auth/default/#django.contrib.auth.mixins.UserPassesTestMixin