Using Django UserPassesTestMixin with LoginRequiredMixin to go to an authorized URL
up vote
0
down vote
favorite
I'm trying to write a mixin that will protect views by first checking if someone is logged in and then if they have been onboarded. It seems to work, by blocking views it's attached to, but it the URLjust goes to a 403 forbidden. Any ideas on how to get it to go to the named url?
from django.contrib.auth.mixins import UserPassesTestMixin
from django.http import HttpResponseRedirect
from django.shortcuts import redirect
from django.contrib.auth.mixins import LoginRequiredMixin
class OnboardedMixin(LoginRequiredMixin, UserPassesTestMixin):
"""
a custom mixin that checks to see if the user has been onboarded yet
"""
def test_func(self):
if self.request.user.onboarded and self.request.user.is_active:
return True
def get_login_url(self):
return redirect('onboarding',)
django django-views django-authentication
add a comment |
up vote
0
down vote
favorite
I'm trying to write a mixin that will protect views by first checking if someone is logged in and then if they have been onboarded. It seems to work, by blocking views it's attached to, but it the URLjust goes to a 403 forbidden. Any ideas on how to get it to go to the named url?
from django.contrib.auth.mixins import UserPassesTestMixin
from django.http import HttpResponseRedirect
from django.shortcuts import redirect
from django.contrib.auth.mixins import LoginRequiredMixin
class OnboardedMixin(LoginRequiredMixin, UserPassesTestMixin):
"""
a custom mixin that checks to see if the user has been onboarded yet
"""
def test_func(self):
if self.request.user.onboarded and self.request.user.is_active:
return True
def get_login_url(self):
return redirect('onboarding',)
django django-views django-authentication
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to write a mixin that will protect views by first checking if someone is logged in and then if they have been onboarded. It seems to work, by blocking views it's attached to, but it the URLjust goes to a 403 forbidden. Any ideas on how to get it to go to the named url?
from django.contrib.auth.mixins import UserPassesTestMixin
from django.http import HttpResponseRedirect
from django.shortcuts import redirect
from django.contrib.auth.mixins import LoginRequiredMixin
class OnboardedMixin(LoginRequiredMixin, UserPassesTestMixin):
"""
a custom mixin that checks to see if the user has been onboarded yet
"""
def test_func(self):
if self.request.user.onboarded and self.request.user.is_active:
return True
def get_login_url(self):
return redirect('onboarding',)
django django-views django-authentication
I'm trying to write a mixin that will protect views by first checking if someone is logged in and then if they have been onboarded. It seems to work, by blocking views it's attached to, but it the URLjust goes to a 403 forbidden. Any ideas on how to get it to go to the named url?
from django.contrib.auth.mixins import UserPassesTestMixin
from django.http import HttpResponseRedirect
from django.shortcuts import redirect
from django.contrib.auth.mixins import LoginRequiredMixin
class OnboardedMixin(LoginRequiredMixin, UserPassesTestMixin):
"""
a custom mixin that checks to see if the user has been onboarded yet
"""
def test_func(self):
if self.request.user.onboarded and self.request.user.is_active:
return True
def get_login_url(self):
return redirect('onboarding',)
django django-views django-authentication
django django-views django-authentication
asked Nov 11 at 2:40
Dave Merwin
5261026
5261026
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Rather than taking this approach, maybe its best to use a decorator instead. For example:
from django.contrib.auth.decorators import login_required
def my_login_required(function):
def wrapper(obj, request, *args, **kw):
decorated_view_func = login_required(request)
if not decorated_view_func.user.is_authenticated:
return decorated_view_func(request) # restricts without login and sends to signin view
if request.user.onboarded and request.user.is_active:
return function(obj, request, *args, **kw)
return HttpResponseRedirect("/onboarding/")
return wrapper
And use this decorator in desired views:
class SomeView(DetailView):
...
@my_login_requried
def dispatch(self, *args, **kwargs):
return super(SomeView, self).dispatch(*args, **kwargs)
thanks for the tip. Why use this method instead of a UserPassesTestMixin? I'm not seeing an obvious reason outside the simplicity that you proposed. And... well, it is simpler. Thoughts?
– Dave Merwin
Nov 13 at 2:44
That doesn't seemt o work with Generic Class Based Views. It triggers a .as_view() error on each view that uses it
– Dave Merwin
Nov 13 at 2:51
@DaveMerwin I have tested with Generic Class Based Views, for example TemplateView, and its working. Maybe your error is occurring for this: stackoverflow.com/questions/36396930/… . I have suggested this idea, because it is a cleaner and simpler solution than overriding 2 mix-ins.
– ruddra
Nov 13 at 4:20
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Rather than taking this approach, maybe its best to use a decorator instead. For example:
from django.contrib.auth.decorators import login_required
def my_login_required(function):
def wrapper(obj, request, *args, **kw):
decorated_view_func = login_required(request)
if not decorated_view_func.user.is_authenticated:
return decorated_view_func(request) # restricts without login and sends to signin view
if request.user.onboarded and request.user.is_active:
return function(obj, request, *args, **kw)
return HttpResponseRedirect("/onboarding/")
return wrapper
And use this decorator in desired views:
class SomeView(DetailView):
...
@my_login_requried
def dispatch(self, *args, **kwargs):
return super(SomeView, self).dispatch(*args, **kwargs)
thanks for the tip. Why use this method instead of a UserPassesTestMixin? I'm not seeing an obvious reason outside the simplicity that you proposed. And... well, it is simpler. Thoughts?
– Dave Merwin
Nov 13 at 2:44
That doesn't seemt o work with Generic Class Based Views. It triggers a .as_view() error on each view that uses it
– Dave Merwin
Nov 13 at 2:51
@DaveMerwin I have tested with Generic Class Based Views, for example TemplateView, and its working. Maybe your error is occurring for this: stackoverflow.com/questions/36396930/… . I have suggested this idea, because it is a cleaner and simpler solution than overriding 2 mix-ins.
– ruddra
Nov 13 at 4:20
add a comment |
up vote
0
down vote
Rather than taking this approach, maybe its best to use a decorator instead. For example:
from django.contrib.auth.decorators import login_required
def my_login_required(function):
def wrapper(obj, request, *args, **kw):
decorated_view_func = login_required(request)
if not decorated_view_func.user.is_authenticated:
return decorated_view_func(request) # restricts without login and sends to signin view
if request.user.onboarded and request.user.is_active:
return function(obj, request, *args, **kw)
return HttpResponseRedirect("/onboarding/")
return wrapper
And use this decorator in desired views:
class SomeView(DetailView):
...
@my_login_requried
def dispatch(self, *args, **kwargs):
return super(SomeView, self).dispatch(*args, **kwargs)
thanks for the tip. Why use this method instead of a UserPassesTestMixin? I'm not seeing an obvious reason outside the simplicity that you proposed. And... well, it is simpler. Thoughts?
– Dave Merwin
Nov 13 at 2:44
That doesn't seemt o work with Generic Class Based Views. It triggers a .as_view() error on each view that uses it
– Dave Merwin
Nov 13 at 2:51
@DaveMerwin I have tested with Generic Class Based Views, for example TemplateView, and its working. Maybe your error is occurring for this: stackoverflow.com/questions/36396930/… . I have suggested this idea, because it is a cleaner and simpler solution than overriding 2 mix-ins.
– ruddra
Nov 13 at 4:20
add a comment |
up vote
0
down vote
up vote
0
down vote
Rather than taking this approach, maybe its best to use a decorator instead. For example:
from django.contrib.auth.decorators import login_required
def my_login_required(function):
def wrapper(obj, request, *args, **kw):
decorated_view_func = login_required(request)
if not decorated_view_func.user.is_authenticated:
return decorated_view_func(request) # restricts without login and sends to signin view
if request.user.onboarded and request.user.is_active:
return function(obj, request, *args, **kw)
return HttpResponseRedirect("/onboarding/")
return wrapper
And use this decorator in desired views:
class SomeView(DetailView):
...
@my_login_requried
def dispatch(self, *args, **kwargs):
return super(SomeView, self).dispatch(*args, **kwargs)
Rather than taking this approach, maybe its best to use a decorator instead. For example:
from django.contrib.auth.decorators import login_required
def my_login_required(function):
def wrapper(obj, request, *args, **kw):
decorated_view_func = login_required(request)
if not decorated_view_func.user.is_authenticated:
return decorated_view_func(request) # restricts without login and sends to signin view
if request.user.onboarded and request.user.is_active:
return function(obj, request, *args, **kw)
return HttpResponseRedirect("/onboarding/")
return wrapper
And use this decorator in desired views:
class SomeView(DetailView):
...
@my_login_requried
def dispatch(self, *args, **kwargs):
return super(SomeView, self).dispatch(*args, **kwargs)
answered Nov 11 at 8:52
ruddra
9,04832547
9,04832547
thanks for the tip. Why use this method instead of a UserPassesTestMixin? I'm not seeing an obvious reason outside the simplicity that you proposed. And... well, it is simpler. Thoughts?
– Dave Merwin
Nov 13 at 2:44
That doesn't seemt o work with Generic Class Based Views. It triggers a .as_view() error on each view that uses it
– Dave Merwin
Nov 13 at 2:51
@DaveMerwin I have tested with Generic Class Based Views, for example TemplateView, and its working. Maybe your error is occurring for this: stackoverflow.com/questions/36396930/… . I have suggested this idea, because it is a cleaner and simpler solution than overriding 2 mix-ins.
– ruddra
Nov 13 at 4:20
add a comment |
thanks for the tip. Why use this method instead of a UserPassesTestMixin? I'm not seeing an obvious reason outside the simplicity that you proposed. And... well, it is simpler. Thoughts?
– Dave Merwin
Nov 13 at 2:44
That doesn't seemt o work with Generic Class Based Views. It triggers a .as_view() error on each view that uses it
– Dave Merwin
Nov 13 at 2:51
@DaveMerwin I have tested with Generic Class Based Views, for example TemplateView, and its working. Maybe your error is occurring for this: stackoverflow.com/questions/36396930/… . I have suggested this idea, because it is a cleaner and simpler solution than overriding 2 mix-ins.
– ruddra
Nov 13 at 4:20
thanks for the tip. Why use this method instead of a UserPassesTestMixin? I'm not seeing an obvious reason outside the simplicity that you proposed. And... well, it is simpler. Thoughts?
– Dave Merwin
Nov 13 at 2:44
thanks for the tip. Why use this method instead of a UserPassesTestMixin? I'm not seeing an obvious reason outside the simplicity that you proposed. And... well, it is simpler. Thoughts?
– Dave Merwin
Nov 13 at 2:44
That doesn't seemt o work with Generic Class Based Views. It triggers a .as_view() error on each view that uses it
– Dave Merwin
Nov 13 at 2:51
That doesn't seemt o work with Generic Class Based Views. It triggers a .as_view() error on each view that uses it
– Dave Merwin
Nov 13 at 2:51
@DaveMerwin I have tested with Generic Class Based Views, for example TemplateView, and its working. Maybe your error is occurring for this: stackoverflow.com/questions/36396930/… . I have suggested this idea, because it is a cleaner and simpler solution than overriding 2 mix-ins.
– ruddra
Nov 13 at 4:20
@DaveMerwin I have tested with Generic Class Based Views, for example TemplateView, and its working. Maybe your error is occurring for this: stackoverflow.com/questions/36396930/… . I have suggested this idea, because it is a cleaner and simpler solution than overriding 2 mix-ins.
– ruddra
Nov 13 at 4:20
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245386%2fusing-django-userpassestestmixin-with-loginrequiredmixin-to-go-to-an-authorized%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown