Skip to content

Commit 3bebcd7

Browse files
committed
feat: remove monkey patching
1 parent 2cf0994 commit 3bebcd7

File tree

5 files changed

+24
-61
lines changed

5 files changed

+24
-61
lines changed

CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,32 @@
1717
library was only containing phone-related filters, the library was renamed to
1818
`phonenumber`.
1919

20+
2021
### Removed
2122

2223
- Python 3.5 and 3.6 support
24+
- Admin Monkey Patching
25+
26+
The Admin UI will not longer be automatically patched. The `TwoFactorSiteAdmin` will need to be explicitly
27+
configured in urls.py.
28+
29+
```py
30+
# urls.py
31+
from django.urls import path
32+
from two_factor.admin import TwoFactorAdminSite
33+
url_patterns = [
34+
path('admin/', TwoFactorAdminSite().urls),
35+
]
36+
```
37+
38+
Custom admin sites can extend `TwoFactorSiteAdmin` or `TwoFactorSideAdminMixin` to inherit the behavior.
39+
40+
```py
41+
# admin.py
42+
class MyCustomAdminSite(TwoFactorSiteAdminMixin, AdminSite):
43+
# implement your customizations here.
44+
pass
45+
```
2346

2447
## 1.13.2
2548

docs/configuration.rst

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@ Configuration
44
General Settings
55
----------------
66

7-
``TWO_FACTOR_PATCH_ADMIN`` (default: ``True``)
8-
Whether the Django admin is patched to use the default login view.
9-
10-
.. warning::
11-
The admin currently does not enforce one-time passwords being set for
12-
admin users.
13-
147
``LOGIN_URL``
158
Should point to the login view provided by this application as described in
169
setup. This login view handles password authentication followed by a one-time
@@ -111,7 +104,7 @@ Next, add additional urls to your config:
111104
112105
# urls.py
113106
from two_factor.gateways.twilio.urls import urlpatterns as tf_twilio_urls
114-
107+
115108
urlpatterns = [
116109
path('', include(tf_twilio_urls)),
117110
...

tests/test_admin.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,11 @@
55
from django.test import TestCase
66
from django.test.utils import override_settings
77

8-
from two_factor.admin import patch_admin, unpatch_admin
9-
108
from .utils import UserMixin
119

1210

1311
@override_settings(ROOT_URLCONF='tests.urls_admin')
1412
class TwoFactorAdminSiteTest(UserMixin, TestCase):
15-
16-
def setUp(self):
17-
patch_admin()
18-
19-
def tearDown(self):
20-
unpatch_admin()
21-
22-
def test(self):
23-
response = self.client.get('/admin/', follow=True)
24-
redirect_to = '%s?next=/admin/' % reverse('admin:login')
25-
self.assertRedirects(response, redirect_to)
26-
27-
28-
@override_settings(ROOT_URLCONF='tests.urls_admin')
29-
class AdminPatchTest(TestCase):
3013
"""
3114
otp_admin is admin console that needs OTP for access.
3215
Only admin users (is_staff and is_active)
@@ -55,7 +38,6 @@ def test_anonymous_get_admin_login(self):
5538
response = self.client.get(login_url, follow=True)
5639
self.assertEqual(response.status_code, 200)
5740

58-
5941
def test_is_staff_not_verified_not_setup_get_admin_index_redirects_to_setup(self):
6042
"""
6143
admins without MFA setup should be redirected to the setup page.

two_factor/admin.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -159,32 +159,3 @@ class AdminSiteOTPRequired(TwoFactorAdminSite):
159159
warnings.warn('AdminSiteOTPRequired is deprecated by TwoFactorAdminSite, please update.',
160160
category=DeprecationWarning)
161161
pass
162-
163-
164-
def patch_admin():
165-
warnings.warn('two-factor admin patching will be removed, use TwoFactorAdminSite or TwoFactorAdminSiteMixin.',
166-
category=DeprecationWarning)
167-
# overrides
168-
setattr(AdminSite, 'login', TwoFactorAdminSiteMixin.login)
169-
setattr(AdminSite, 'admin_view', TwoFactorAdminSiteMixin.admin_view)
170-
setattr(AdminSite, 'has_permission', TwoFactorAdminSiteMixin.has_permission)
171-
# additions
172-
setattr(AdminSite, 'has_admin_permission', original_has_permission)
173-
setattr(AdminSite, 'has_mfa_setup', TwoFactorAdminSiteMixin.has_mfa_setup)
174-
setattr(AdminSite, 'redirect_to_mfa_setup', TwoFactorAdminSiteMixin.redirect_to_mfa_setup)
175-
176-
177-
def unpatch_admin():
178-
warnings.warn('django-two-factor admin patching is deprecated, use TwoFactorAdminSite or TwoFactorAdminSiteMixin.',
179-
category=DeprecationWarning)
180-
# we really only need unpatching in our tests so this can be a noop.
181-
# overrides
182-
setattr(AdminSite, 'login', original_login)
183-
setattr(AdminSite, 'admin_view', original_admin_view)
184-
setattr(AdminSite, 'has_permission', original_has_permission)
185-
# NOTE: this unpatching doesn't really work, but becuase it just patches in our mixin it isn't harmful.
186-
187-
188-
original_login = AdminSite.login
189-
original_admin_view = AdminSite.admin_view
190-
original_has_permission = AdminSite.has_permission

two_factor/apps.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
from django.apps import AppConfig
2-
from django.conf import settings
32

43

54
class TwoFactorConfig(AppConfig):
65
name = 'two_factor'
76
verbose_name = "Django Two Factor Authentication"
8-
9-
def ready(self):
10-
if getattr(settings, 'TWO_FACTOR_PATCH_ADMIN', True):
11-
from .admin import patch_admin
12-
patch_admin()

0 commit comments

Comments
 (0)