Merge ~suligap/canonical-identity-provider:new-style-middleware into canonical-identity-provider:master

Proposed by Przemysław Suliga
Status: Merged
Approved by: Przemysław Suliga
Approved revision: 0036cd5864416b032f552063818e5e0d6fe2f82d
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~suligap/canonical-identity-provider:new-style-middleware
Merge into: canonical-identity-provider:master
Diff against target: 153 lines (+18/-11)
9 files modified
django_project/settings_base.py (+1/-1)
django_project/settings_test.py (+1/-1)
requirements.txt (+1/-1)
src/api/v20/utils.py (+2/-1)
src/identityprovider/middleware/http.py (+2/-1)
src/identityprovider/middleware/oopsplease.py (+3/-1)
src/identityprovider/middleware/readonly.py (+4/-2)
src/identityprovider/middleware/useraccount.py (+2/-1)
src/identityprovider/tests/test_middleware.py (+2/-2)
Reviewer Review Type Date Requested Status
Hasan Ammar (community) Approve
Review via email: mp+384550@code.launchpad.net

Commit message

Switch to Django 1.10 style middleware

Description of the change

https://docs.djangoproject.com/en/1.11/topics/http/middleware/#upgrading-middleware

In most cases this is adding the MiddlewareMixin to our custom
middleware classes. Plus a version bump for django-honeypot that brings
the new style middleware compatibility.

Other middleware used (django and other 3rd party middleware) was already
compatible.

Requires
https://code.launchpad.net/~suligap/canonical-identity-provider/dependencies-django-honeypot-0.7.0/+merge/384546

To post a comment you must log in.
Revision history for this message
Hasan Ammar (hasanammar) wrote :

LGTM!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/django_project/settings_base.py b/django_project/settings_base.py
2index 512b4d6..d131316 100644
3--- a/django_project/settings_base.py
4+++ b/django_project/settings_base.py
5@@ -384,7 +384,7 @@ MAX_PASSWORD_RESET_TOKENS = 5
6 MEDIA_ROOT = ''
7 MEDIA_URL = ''
8 MESSAGE_STORAGE = 'django.contrib.messages.storage.fallback.FallbackStorage'
9-MIDDLEWARE_CLASSES = [
10+MIDDLEWARE = [
11 'django.middleware.security.SecurityMiddleware',
12 ('raven.contrib.django.raven_compat.middleware'
13 '.SentryResponseErrorIdMiddleware'),
14diff --git a/django_project/settings_test.py b/django_project/settings_test.py
15index b27eed7..2c57225 100644
16--- a/django_project/settings_test.py
17+++ b/django_project/settings_test.py
18@@ -5,5 +5,5 @@ from settings_devel import * # noqa
19 # https://github.com/sunlightlabs/django-honeypot/issues/12
20 # until django-honeypot provides a way of using the middleware in tests,
21 # we'll remove the middleware
22-MIDDLEWARE_CLASSES.remove( # noqa
23+MIDDLEWARE.remove( # noqa
24 'identityprovider.middleware.honeypot.HoneypotMiddleware')
25diff --git a/requirements.txt b/requirements.txt
26index 8c192ec..60cc1e6 100644
27--- a/requirements.txt
28+++ b/requirements.txt
29@@ -5,7 +5,7 @@ convoy==0.4.1
30 cryptography==1.3.2
31 django==1.11.27
32 django-adminaudit==0.5
33-django-honeypot==0.6.0
34+django-honeypot==0.7.0
35 django-jsonfield==1.0.1
36 django-modeldict-yplan==1.5.2
37 django-openid-auth==0.14
38diff --git a/src/api/v20/utils.py b/src/api/v20/utils.py
39index a90f767..1b31bf8 100644
40--- a/src/api/v20/utils.py
41+++ b/src/api/v20/utils.py
42@@ -6,6 +6,7 @@ from operator import attrgetter
43
44 from django.http import HttpResponse
45 from django.urls import reverse
46+from django.utils.deprecation import MiddlewareMixin
47 from django.utils.translation import ugettext_lazy as _
48
49 from identityprovider.models.account import Account
50@@ -15,7 +16,7 @@ from identityprovider.models.const import (
51 )
52
53
54-class EnsureJSONResponseOnAPIErrorMiddleware(object):
55+class EnsureJSONResponseOnAPIErrorMiddleware(MiddlewareMixin):
56
57 def process_exception(self, request, exception):
58 response = None
59diff --git a/src/identityprovider/middleware/http.py b/src/identityprovider/middleware/http.py
60index 60df00b..0f7fcb8 100644
61--- a/src/identityprovider/middleware/http.py
62+++ b/src/identityprovider/middleware/http.py
63@@ -3,9 +3,10 @@
64
65 from django.core.exceptions import ValidationError
66 from django.core.validators import validate_ipv46_address
67+from django.utils.deprecation import MiddlewareMixin
68
69
70-class SetRemoteAddrFromForwardedFor(object):
71+class SetRemoteAddrFromForwardedFor(MiddlewareMixin):
72 """Set REMOTE_ADDR based on HTTP_X_FORWARDED_FOR, if the latter is present.
73
74 This is useful if you're sitting behind a reverse proxy that causes each
75diff --git a/src/identityprovider/middleware/oopsplease.py b/src/identityprovider/middleware/oopsplease.py
76index f1eb56e..e8c7a1a 100644
77--- a/src/identityprovider/middleware/oopsplease.py
78+++ b/src/identityprovider/middleware/oopsplease.py
79@@ -2,13 +2,15 @@
80 # GNU Affero General Public License version 3 (see the file LICENSE).
81 import sys
82
83+from django.utils.deprecation import MiddlewareMixin
84+
85 from identityprovider.oops_config import (
86 ManuallyGeneratedOops,
87 global_error_reporter,
88 )
89
90
91-class OopsPleaseMiddleware(object):
92+class OopsPleaseMiddleware(MiddlewareMixin):
93
94 QUERY_STRING_KEY = 'oopsplease'
95
96diff --git a/src/identityprovider/middleware/readonly.py b/src/identityprovider/middleware/readonly.py
97index 34edf99..d17712c 100644
98--- a/src/identityprovider/middleware/readonly.py
99+++ b/src/identityprovider/middleware/readonly.py
100@@ -1,7 +1,9 @@
101+from django.utils.deprecation import MiddlewareMixin
102+
103 from identityprovider.readonly import ReadOnlyManager
104
105
106-class ReadOnlyMiddleware(object):
107- """Check for the existance of readonly flag on each requst"""
108+class ReadOnlyMiddleware(MiddlewareMixin):
109+ """Check for the existance of readonly flag on each request"""
110 def process_request(self, request):
111 ReadOnlyManager().check_readonly()
112diff --git a/src/identityprovider/middleware/useraccount.py b/src/identityprovider/middleware/useraccount.py
113index 61480b0..03c63a2 100644
114--- a/src/identityprovider/middleware/useraccount.py
115+++ b/src/identityprovider/middleware/useraccount.py
116@@ -4,13 +4,14 @@
117 from django.conf import settings
118 from django.contrib.auth import logout
119 from django.contrib.auth.models import User
120+from django.utils.deprecation import MiddlewareMixin
121 from django.utils.translation import LANGUAGE_SESSION_KEY
122
123 from identityprovider.const import SESSION_TOKEN_KEY
124 from identityprovider.models import Account
125
126
127-class UserAccountConversionMiddleware(object):
128+class UserAccountConversionMiddleware(MiddlewareMixin):
129 """Try to convert request.user to the corresponding Account objects.
130
131 If it's a User object, and the request is outside of the admin, and vice
132diff --git a/src/identityprovider/tests/test_middleware.py b/src/identityprovider/tests/test_middleware.py
133index 3d3775f..6a5a6fa 100644
134--- a/src/identityprovider/tests/test_middleware.py
135+++ b/src/identityprovider/tests/test_middleware.py
136@@ -470,7 +470,7 @@ urlpatterns = original_patterns + [
137 ]
138
139
140-@modify_settings(MIDDLEWARE_CLASSES={
141+@modify_settings(MIDDLEWARE={
142 'append': 'identityprovider.middleware.honeypot.HoneypotMiddleware'
143 })
144 @override_settings(ROOT_URLCONF='identityprovider.tests.test_middleware')
145@@ -519,7 +519,7 @@ class HoneypotEnabledTestCase(TestCase):
146 self.assertContains(response, "<!DOCTYPE html>")
147
148
149-@modify_settings(MIDDLEWARE_CLASSES={
150+@modify_settings(MIDDLEWARE={
151 'append': 'identityprovider.middleware.honeypot.HoneypotMiddleware'
152 })
153 class HoneypotWhitelistedTestCase(SSOBaseTestCase):

Subscribers

People subscribed via source and target branches