Skip to content

Commit fb3c8f4

Browse files
committed
handle Django 1.10/1.11 with MIDDLEWARE_CLASSES correctly (#189)
In the case of Django 1.10/1.11, the `MIDDLEWARE` attribute is there, but may be set to `None` if the old `MIDDLEWARE_CLASSES` setting is still used. This change will also ensure that the correct setting name is used in the error message of the management command. fixes #188 closes #189
1 parent e74bd1a commit fb3c8f4

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

elasticapm/contrib/django/management/commands/elasticapm.py

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -204,37 +204,24 @@ def handle_check(self, command, **options):
204204
self.write('')
205205

206206
# check if middleware is set, and if it is at the first position
207-
middleware = list(getattr(settings, 'MIDDLEWARE', getattr(settings, 'MIDDLEWARE_CLASSES', [])))
207+
middleware_attr = 'MIDDLEWARE' if getattr(settings, 'MIDDLEWARE', None) is not None else 'MIDDLEWARE_CLASSES'
208+
middleware = list(getattr(settings, middleware_attr))
208209
try:
209-
pos = middleware.index(
210-
'elasticapm.contrib.django.middleware.TracingMiddleware'
211-
)
210+
pos = middleware.index('elasticapm.contrib.django.middleware.TracingMiddleware')
212211
if pos == 0:
213-
self.write(
214-
'Tracing middleware is configured! Awesome!',
215-
green
216-
)
212+
self.write('Tracing middleware is configured! Awesome!', green)
217213
else:
218-
self.write(
219-
'Tracing middleware is configured, but not at the first '
220-
'position\n',
221-
yellow
222-
)
223-
self.write(
224-
'ElasticAPM works best if you add it at the top of your '
225-
'MIDDLEWARE_CLASSES'
226-
)
214+
self.write('Tracing middleware is configured, but not at the first position\n', yellow)
215+
self.write('ElasticAPM works best if you add it at the top of your %s setting' % middleware_attr)
227216
except ValueError:
228-
self.write(
229-
'Tracing middleware not configured!', red
230-
)
217+
self.write('Tracing middleware not configured!', red)
231218
self.write(
232219
'\n'
233-
'Add it to your MIDDLEWARE_CLASSES like this:\n\n'
234-
' MIDDLEWARE_CLASSES = (\n'
220+
'Add it to your %(name)s setting like this:\n\n'
221+
' %(name)s = (\n'
235222
' "elasticapm.contrib.django.middleware.TracingMiddleware",\n'
236223
' # your other middleware classes\n'
237-
' )\n'
224+
' )\n' % {'name': middleware_attr}
238225
)
239226
self.write('')
240227
if passed:

tests/contrib/django/django_tests.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,10 @@ def test_middleware_not_set():
11431143
call_command('elasticapm', 'check', stdout=stdout)
11441144
output = stdout.getvalue()
11451145
assert 'Tracing middleware not configured!' in output
1146+
if django.VERSION < (1, 10):
1147+
assert 'MIDDLEWARE_CLASSES' in output
1148+
else:
1149+
assert 'MIDDLEWARE setting' in output
11461150

11471151

11481152
def test_middleware_not_first():
@@ -1154,6 +1158,26 @@ def test_middleware_not_first():
11541158
call_command('elasticapm', 'check', stdout=stdout)
11551159
output = stdout.getvalue()
11561160
assert 'not at the first position' in output
1161+
if django.VERSION < (1, 10):
1162+
assert 'MIDDLEWARE_CLASSES' in output
1163+
else:
1164+
assert 'MIDDLEWARE setting' in output
1165+
1166+
1167+
@pytest.mark.skipif(not ((1, 10) <= django.VERSION < (2, 0)),
1168+
reason='only needed in 1.10 and 1.11 when both middleware settings are valid')
1169+
def test_django_1_10_uses_deprecated_MIDDLEWARE_CLASSES():
1170+
stdout = compat.StringIO()
1171+
with override_settings(
1172+
MIDDLEWARE=None,
1173+
MIDDLEWARE_CLASSES=[
1174+
'foo',
1175+
'elasticapm.contrib.django.middleware.TracingMiddleware'
1176+
],
1177+
):
1178+
call_command('elasticapm', 'check', stdout=stdout)
1179+
output = stdout.getvalue()
1180+
assert 'not at the first position' in output
11571181

11581182

11591183
@mock.patch('elasticapm.transport.http.urllib3.PoolManager.urlopen')

0 commit comments

Comments
 (0)