Conditional login decorator

I have a decorator @ms_identity_web.login_required on almost all methods in my views.

How do I something like this ?

if not settings.TEST_WITHOUT_LOGIN:
    @ms_identity_web.login_required

Without seeing more specific implementation details, it’s tough to say what the “best” solution might be.

But the most generic solution that I can think of right off-hand would be to create your own decorator that checks for settings.TEST_WITHOUT_LOGIN and only applies the login_required test if that setting is falsy.

You would then replace all occurances of @ms_identity_web.login_required with this new decorator.

I have this :

def conditional_login_required(view_func):
    def _wrapped_view(request, *args, **kwargs):
        if not settings.TEST_WITHOUT_LOGIN:
            return ms_identity_web.login_required(view_func)(request, *args, **kwargs)
        return view_func(request, *args, **kwargs)
    return wraps(view_func)(_wrapped_view)

But I get

AttributeError at /orders/list
module 'ms_identity_web' has no attribute 'login_required'
Request Method:	GET
Request URL:	http://localhost:8000/orders/list
Django Version:	4.2.2
Exception Type:	AttributeError
Exception Value:	
module 'ms_identity_web' has no attribute 'login_required'
ms_identity_web = settings.MS_IDENTITY_WEB

@conditional_login_required
def index(request):
    return render(request, "orders/list.html")

Apologies, in the file where the custom decorator is defined :

ms_identity_web = settings.MS_IDENTITY_WEB