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',)









share|improve this question

























    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',)









    share|improve this question























      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',)









      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 2:40









      Dave Merwin

      5261026




      5261026






















          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)





          share|improve this answer




















          • 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











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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

























          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)





          share|improve this answer




















          • 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















          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)





          share|improve this answer




















          • 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













          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)





          share|improve this answer












          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)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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

















          • 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


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          ReactJS Fetched API data displays live - need Data displayed static

          政党