Thursday, April 27, 2017

Decorator help

I need some views that the login_required is controlled via a setting, some deployments of the code are open and some are closed (Different sites).

So I figured I could just write a quick decorator of my own and have it check the setting before then calling login_required. After trouble with that, I just took the entire login_required decorator code put it in my app/decoratores.py and called it, login_required_by_setting just to see that work.

def login_required_by_setting(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None):      """      Decorator for views that checks that the user is logged in, redirecting      to the log-in page if necessary.      """      actual_decorator = user_passes_test(          lambda u: u.is_authenticated,          login_url=login_url,          redirect_field_name=redirect_field_name      )      if function:          return actual_decorator(function)      return actual_decorator

But when I add that decorator to my view it lets me in when I'm not logged in, 
but once I change it back to @
login_required it works as expected.

Any ideas where to look, im at a loss.
Dylan

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHtg44Dvvj0%2BkrzRDKTeu%2B2OE1VaYerr-1osubEgS2zsgMNZEA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment