2FA: Use patch.object for monkey patching.

Ref #9521
This commit is contained in:
Umair Khan
2018-06-05 13:25:41 +05:00
committed by Tim Abbott
parent e9312b921c
commit 9b698dec08

View File

@@ -1,4 +1,4 @@
from mock import patch
from django.forms import Form from django.forms import Form
from django.conf import settings from django.conf import settings
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
@@ -581,14 +581,9 @@ class TwoFactorLoginView(BaseTwoFactorLoginView):
We need to override this function so that we can redirect to We need to override this function so that we can redirect to
realm.uri instead of '/'. realm.uri instead of '/'.
""" """
old_redirect_url = settings.LOGIN_REDIRECT_URL realm_uri = self.get_user().realm.uri
try: with patch.object(settings, 'LOGIN_REDIRECT_URL', realm_uri):
# TODO: Get django-two-factor to support this being an option. return super().done(form_list, **kwargs)
settings.LOGIN_REDIRECT_URL = self.get_user().realm.uri
redirect_response = super().done(form_list, **kwargs)
finally:
settings.LOGIN_REDIRECT_URL = old_redirect_url
return redirect_response
def login_page(request: HttpRequest, **kwargs: Any) -> HttpResponse: def login_page(request: HttpRequest, **kwargs: Any) -> HttpResponse:
if settings.TWO_FACTOR_AUTHENTICATION_ENABLED: if settings.TWO_FACTOR_AUTHENTICATION_ENABLED: