Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
✨ Set up automatic patching/unpatching based on setting change
Browse files Browse the repository at this point in the history
This makes it possible to use @override_settings in tests to control 2FA behaviour.
  • Loading branch information
sergei-maertens committed Aug 18, 2021
1 parent 08de3df commit c11a4b7
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions two_factor/apps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from django.apps import AppConfig
from django.conf import settings

from django.dispatch import receiver
from django.core.signals import setting_changed


class TwoFactorConfig(AppConfig):
name = 'two_factor'
Expand All @@ -10,3 +13,21 @@ def ready(self):
if getattr(settings, 'TWO_FACTOR_PATCH_ADMIN', True):
from .admin import patch_admin
patch_admin()


@receiver(setting_changed, dispatch_uid="two_factor.handle_setting_changed")
def handle_setting_changed(sender, setting: str, value, **kwargs):
from .admin import patch_admin, unpatch_admin, __default_admin_site__

is_patched = __default_admin_site__ is not None

if setting == "TWO_FACTOR_PATCH_ADMIN":
if value is True and not is_patched:
patch_admin()
elif value is False and is_patched:
unpatch_admin()

elif setting == "TWO_FACTOR_FORCE_OTP_ADMIN":
if is_patched:
unpatch_admin()
patch_admin()

0 comments on commit c11a4b7

Please sign in to comment.