Skip to content

Commit 0b1bc8e

Browse files
author
Emanuele Palazzetti
authored
Merge pull request #415 from sciyoshi/master
Django 2.0 compatibility: User.is_authenticated is now a property
2 parents d95068d + d6ecb34 commit 0b1bc8e

File tree

9 files changed

+77
-8
lines changed

9 files changed

+77
-8
lines changed

.circleci/config.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,18 @@ jobs:
299299
- run: tox -e '{py27,py34,py35,py36}-django{18,19,110,111}-djangopylibmc06-djangoredis45-pylibmc-redis{210}-memcached' --result-json /tmp/django.1.results
300300
- run: tox -e '{py27,py34,py35,py36}-django-autopatch{18,19,110,111}-djangopylibmc06-djangoredis45-pylibmc-redis{210}-memcached' --result-json /tmp/django.2.results
301301
- run: tox -e '{py27,py34,py35,py36}-django-drf{110,111}-djangorestframework{34,35,36,37}' --result-json /tmp/django.3.results
302+
- run: tox -e '{py34,py35,py36}-django{200}-djangopylibmc06-djangoredis45-pylibmc-redis{210}-memcached' --result-json /tmp/django.4.results
303+
- run: tox -e '{py34,py35,py36}-django-autopatch{200}-djangopylibmc06-djangoredis45-pylibmc-redis{210}-memcached' --result-json /tmp/django.5.results
304+
- run: tox -e '{py34,py35,py36}-django-drf{200}-djangorestframework{37}' --result-json /tmp/django.6.results
302305
- persist_to_workspace:
303306
root: /tmp
304307
paths:
305308
- django.1.results
306309
- django.2.results
307310
- django.3.results
311+
- django.4.results
312+
- django.5.results
313+
- django.6.results
308314
- save_cache:
309315
key: tox-cache-django-{{ checksum "tox.ini" }}
310316
paths:

ddtrace/contrib/django/compat.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import django
2+
3+
4+
if django.VERSION >= (2,):
5+
def user_is_authenticated(user):
6+
return user.is_authenticated
7+
else:
8+
def user_is_authenticated(user):
9+
return user.is_authenticated()

ddtrace/contrib/django/middleware.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# project
44
from .conf import settings
5+
from .compat import user_is_authenticated
56

67
from ...ext import http
78
from ...contrib import func_name
@@ -134,7 +135,7 @@ def _set_auth_tags(span, request):
134135
return span
135136

136137
if hasattr(user, 'is_authenticated'):
137-
span.set_tag('django.user.is_authenticated', user.is_authenticated())
138+
span.set_tag('django.user.is_authenticated', user_is_authenticated(user))
138139

139140
uid = getattr(user, 'pk', None)
140141
if uid:

tests/contrib/django/app/settings.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
},
7070
]
7171

72-
if django.VERSION >= (1, 10):
72+
if (1, 10) <= django.VERSION < (2, 0):
7373
MIDDLEWARE = [
7474
'django.contrib.sessions.middleware.SessionMiddleware',
7575
'django.middleware.common.CommonMiddleware',
@@ -82,6 +82,21 @@
8282

8383
'tests.contrib.django.app.middlewares.CatchExceptionMiddleware',
8484
]
85+
86+
# Django 2.0 has different defaults
87+
if django.VERSION >= (2, 0):
88+
MIDDLEWARE = [
89+
'django.contrib.sessions.middleware.SessionMiddleware',
90+
'django.middleware.common.CommonMiddleware',
91+
'django.middleware.csrf.CsrfViewMiddleware',
92+
'django.contrib.auth.middleware.AuthenticationMiddleware',
93+
'django.contrib.messages.middleware.MessageMiddleware',
94+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
95+
'django.middleware.security.SecurityMiddleware',
96+
97+
'tests.contrib.django.app.middlewares.CatchExceptionMiddleware',
98+
]
99+
85100
# Always add the legacy conf to make sure we handle it properly
86101
# Pre 1.10 style
87102
MIDDLEWARE_CLASSES = [

tests/contrib/django/compat.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
try:
2+
from django.core.urlresolvers import reverse
3+
except ImportError:
4+
from django.urls import reverse

tests/contrib/django/test_cache_views.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
# 3rd party
44
from nose.tools import eq_, ok_
55

6-
from django.core.urlresolvers import reverse
7-
86
# testing
7+
from .compat import reverse
98
from .utils import DjangoTraceTestCase
109

1110

tests/contrib/django/test_middleware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
from nose.tools import eq_
33

44
from django.test import modify_settings
5-
from django.core.urlresolvers import reverse
65

76
# project
87
from ddtrace.constants import SAMPLING_PRIORITY_KEY
98
from ddtrace.contrib.django.conf import settings
109
from ddtrace.contrib.django import TraceMiddleware
1110

1211
# testing
12+
from .compat import reverse
1313
from .utils import DjangoTraceTestCase, override_ddtrace_settings
1414

1515

tests/contrib/djangorestframework/app/settings.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@
5555
'tests.contrib.django.app.middlewares.CatchExceptionMiddleware',
5656
]
5757

58+
# Django 2.0 has different defaults
59+
if django.VERSION >= (2, 0):
60+
MIDDLEWARE = [
61+
'django.contrib.sessions.middleware.SessionMiddleware',
62+
'django.middleware.common.CommonMiddleware',
63+
'django.middleware.csrf.CsrfViewMiddleware',
64+
'django.contrib.auth.middleware.AuthenticationMiddleware',
65+
'django.contrib.messages.middleware.MessageMiddleware',
66+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
67+
'django.middleware.security.SecurityMiddleware',
68+
69+
'tests.contrib.django.app.middlewares.CatchExceptionMiddleware',
70+
]
71+
5872
# Always add the legacy conf to make sure we handle it properly
5973
# Pre 1.10 style
6074
MIDDLEWARE_CLASSES = [

tox.ini

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ envlist =
3939
{py27,py34,py35,py36}-falcon{10,11,12}
4040
{py27,py34,py35,py36}-falcon-autopatch{10,11,12}
4141
{py27,py34,py35,py36}-django{18,19,110,111}-djangopylibmc06-djangoredis45-pylibmc-redis{210}-memcached
42+
{py34,py35,py36}-django{200}-djangopylibmc06-djangoredis45-pylibmc-redis{210}-memcached
4243
{py27,py34,py35,py36}-django-autopatch{18,19,110,111}-djangopylibmc06-djangoredis45-pylibmc-redis{210}-memcached
44+
{py34,py35,py36}-django-autopatch{200}-djangopylibmc06-djangoredis45-pylibmc-redis{210}-memcached
4345
{py27,py34,py35,py36}-django-drf{110,111}-djangorestframework{34,35,36,37}
46+
{py34,py35,py36}-django-drf{200}-djangorestframework{37}
4447
{py27,py34,py35,py36}-flask{010,011,012}-blinker
4548
{py27,py34,py35,py36}-flask-autopatch{010,011,012}-blinker
4649
{py27,py34,py35,py36}-flask{010,011,012}-flaskcache{013}-memcached-redis{210}-blinker
@@ -137,12 +140,15 @@ deps =
137140
django19: django>=1.9,<1.10
138141
django110: django>=1.10,<1.11
139142
django111: django>=1.11,<1.12
143+
django200: django>=2.0,<2.1
140144
django-autopatch18: django>=1.8,<1.9
141145
django-autopatch19: django>=1.9,<1.10
142146
django-autopatch110: django>=1.10,<1.11
143147
django-autopatch111: django>=1.11,<1.12
148+
django-autopatch200: django>=2.0,<2.1
144149
django-drf110: django>=1.10,<1.11
145150
django-drf111: django>=1.11,<1.12
151+
django-drf200: django>=2.0,<2.1
146152
djangopylibmc06: django-pylibmc>=0.6,<0.7
147153
djangoredis45: django-redis>=4.5,<4.6
148154
djangorestframework34: djangorestframework>=3.4,<3.5
@@ -240,9 +246,9 @@ commands =
240246
cassandra{35,36,37,38}: nosetests {posargs} tests/contrib/cassandra
241247
celery{31,40}: nosetests {posargs} tests/contrib/celery
242248
elasticsearch{16,17,18,23,24,25,51,52,53,54}: nosetests {posargs} tests/contrib/elasticsearch
243-
django{18,19,110,111}: python tests/contrib/django/runtests.py {posargs}
244-
django-autopatch{18,19,110,111}: ddtrace-run python tests/contrib/django/runtests.py {posargs}
245-
django-drf{110,111}: python tests/contrib/djangorestframework/runtests.py {posargs}
249+
django{18,19,110,111,200}: python tests/contrib/django/runtests.py {posargs}
250+
django-autopatch{18,19,110,111,200}: ddtrace-run python tests/contrib/django/runtests.py {posargs}
251+
django-drf{110,111,200}: python tests/contrib/djangorestframework/runtests.py {posargs}
246252
flaskcache{012,013}: nosetests {posargs} tests/contrib/flask_cache
247253
flask{010,011,012}: nosetests {posargs} tests/contrib/flask
248254
flask-autopatch{010,011,012}: ddtrace-run nosetests {posargs} tests/contrib/flask_autopatch
@@ -399,6 +405,9 @@ setenv =
399405
setenv =
400406
{[django_autopatch]setenv}
401407
[testenv:py27-django-autopatch110-djangopylibmc06-djangoredis45-pylibmc-redis-memcached]
408+
setenv =
409+
{[django_autopatch]setenv}
410+
[testenv:py27-django-autopatch111-djangopylibmc06-djangoredis45-pylibmc-redis-memcached]
402411
setenv =
403412
{[django_autopatch]setenv}
404413
[testenv:py34-django-autopatch18-djangopylibmc06-djangoredis45-pylibmc-redis-memcached]
@@ -408,6 +417,12 @@ setenv =
408417
setenv =
409418
{[django_autopatch]setenv}
410419
[testenv:py34-django-autopatch110-djangopylibmc06-djangoredis45-pylibmc-redis-memcached]
420+
setenv =
421+
{[django_autopatch]setenv}
422+
[testenv:py34-django-autopatch111-djangopylibmc06-djangoredis45-pylibmc-redis-memcached]
423+
setenv =
424+
{[django_autopatch]setenv}
425+
[testenv:py34-django-autopatch200-djangopylibmc06-djangoredis45-pylibmc-redis-memcached]
411426
setenv =
412427
{[django_autopatch]setenv}
413428
[testenv:py35-django-autopatch18-djangopylibmc06-djangoredis45-pylibmc-redis-memcached]
@@ -420,6 +435,9 @@ setenv =
420435
setenv =
421436
{[django_autopatch]setenv}
422437
[testenv:py35-django-autopatch111-djangopylibmc06-djangoredis45-pylibmc-redis-memcached]
438+
setenv =
439+
{[django_autopatch]setenv}
440+
[testenv:py35-django-autopatch200-djangopylibmc06-djangoredis45-pylibmc-redis-memcached]
423441
setenv =
424442
{[django_autopatch]setenv}
425443
[testenv:py36-django-autopatch18-djangopylibmc06-djangoredis45-pylibmc-redis-memcached]
@@ -432,6 +450,9 @@ setenv =
432450
setenv =
433451
{[django_autopatch]setenv}
434452
[testenv:py36-django-autopatch111-djangopylibmc06-djangoredis45-pylibmc-redis-memcached]
453+
setenv =
454+
{[django_autopatch]setenv}
455+
[testenv:py36-django-autopatch200-djangopylibmc06-djangoredis45-pylibmc-redis-memcached]
435456
setenv =
436457
{[django_autopatch]setenv}
437458

0 commit comments

Comments
 (0)