Skip to content

Commit d555320

Browse files
umaannamalaiTimPansinolrafeei
committed
Add support for Django v4. (#451)
* Add support for Django v4 Co-authored-by: Tim Pansino <[email protected]> Co-authored-by: Lalleh Rafeei <[email protected]> * Update django and tastypie tests. * Update tastypie test matrix. * Fix typos. * Install different versions of Django depending on tastypie/ Python version. Co-authored-by: Tim Pansino <[email protected]> Co-authored-by: Lalleh Rafeei <[email protected]>
1 parent d144f18 commit d555320

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

newrelic/hooks/framework_django.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,10 @@ def wrap_view_handler(wrapped, priority=3):
547547
if hasattr(wrapped, '_nr_django_view_handler'):
548548
return wrapped
549549

550-
name = callable_name(wrapped)
550+
if hasattr(wrapped, "view_class"):
551+
name = callable_name(wrapped.view_class)
552+
else:
553+
name = callable_name(wrapped)
551554

552555
def wrapper(wrapped, instance, args, kwargs):
553556
transaction = current_transaction()
@@ -896,6 +899,20 @@ def instrument_django_views_debug(module):
896899
module.technical_500_response, priority=1)
897900

898901

902+
def resolve_view_handler(view, request):
903+
# We can't intercept the delegated view handler when it
904+
# is looked up by the dispatch() method so we need to
905+
# duplicate the lookup mechanism.
906+
907+
if request.method.lower() in view.http_method_names:
908+
handler = getattr(view, request.method.lower(),
909+
view.http_method_not_allowed)
910+
else:
911+
handler = view.http_method_not_allowed
912+
913+
return handler
914+
915+
899916
def wrap_view_dispatch(wrapped):
900917

901918
# Wrapper to be applied to dispatcher for class based views.
@@ -912,15 +929,7 @@ def _args(request, *args, **kwargs):
912929
view = instance
913930
request = _args(*args, **kwargs)
914931

915-
# We can't intercept the delegated view handler when it
916-
# is looked up by the dispatch() method so we need to
917-
# duplicate the lookup mechanism.
918-
919-
if request.method.lower() in view.http_method_names:
920-
handler = getattr(view, request.method.lower(),
921-
view.http_method_not_allowed)
922-
else:
923-
handler = view.http_method_not_allowed
932+
handler = resolve_view_handler(view, request)
924933

925934
name = callable_name(handler)
926935

tests/component_tastypie/test_application.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import six
1717
import webtest
1818

19+
from tastypie import VERSION
20+
1921
from newrelic.api.background_task import background_task
2022
from newrelic.api.transaction import end_of_transaction
2123

@@ -64,7 +66,6 @@ def __exit__(self, *args, **kwargs):
6466

6567
_test_api_base_scoped_metrics = [
6668
('Function/django.core.handlers.wsgi:WSGIHandler.__call__', 1),
67-
('Function/django.urls.resolvers:RegexURLResolver.resolve', 1),
6869
('Python/WSGI/Application', 1),
6970
('Python/WSGI/Response', 1),
7071
('Python/WSGI/Finalize', 1),
@@ -77,6 +78,13 @@ def __exit__(self, *args, **kwargs):
7778
_test_api_base_scoped_metrics.append(
7879
('Function/tastypie.resources:wrapper', 1))
7980

81+
# django < 1.12 used the RegexURLResolver class and this was updated to URLResolver in later versions
82+
if VERSION <= (0, 14, 3) and not six.PY3:
83+
_test_api_base_scoped_metrics.append(('Function/django.urls.resolvers:RegexURLResolver.resolve', 1))
84+
else:
85+
_test_api_base_scoped_metrics.append(('Function/django.urls.resolvers:URLResolver.resolve', 1))
86+
87+
8088
_test_application_not_found_scoped_metrics = list(
8189
_test_api_base_scoped_metrics)
8290

tox.ini

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ envlist =
6060
python-component_djangorestframework-py27-djangorestframework0300,
6161
python-component_djangorestframework-{py36,py37,py38,py39,py310}-djangorestframeworklatest,
6262
python-component_flask_rest-{py27,py36,py37,py38,py39,pypy,pypy3},
63-
python-component_tastypie-{py27,py36,py37,py38,py39,pypy,pypy3},
63+
python-component_tastypie-{py27,pypy}-tastypie0143,
64+
python-component_tastypie-{py36,py37,py38,py39,pypy3}-tastypie{0143,latest},
6465
python-coroutines_asyncio-{py36,py37,py38,py39,py310,pypy3},
6566
python-cross_agent-{py27,py36,py37,py38,py39,py310}-{with,without}_extensions,
6667
python-cross_agent-pypy-without_extensions,
@@ -107,9 +108,7 @@ envlist =
107108
python-framework_cherrypy-pypy3-CherryPy0303,
108109
python-framework_django-{pypy,py27}-Django0103,
109110
python-framework_django-{pypy,py27,py37}-Django0108,
110-
;temporarily disabling Django latest tests until v4 support is added to agent
111-
; python-framework_django-{py39}-Django{0200,0201,0202,0300,0301,latest},
112-
python-framework_django-{py39}-Django{0200,0201,0202,0300,0301},
111+
python-framework_django-{py39}-Django{0200,0201,0202,0300,0301,latest},
113112
python-framework_django-{py36,py37,py38,py39,py310}-Django0302,
114113
python-framework_falcon-{py27,py36,py37,py38,py39,pypy,pypy3}-falcon0103,
115114
python-framework_falcon-{py36,py37,py38,py39,py310,pypy3}-falcon{0200,master},
@@ -181,8 +180,11 @@ deps =
181180
component_flask_rest: flask-restful
182181
component_flask_rest: flask-restplus
183182
component_flask_rest: flask-restx
184-
component_tastypie: django-tastypie<0.15
185-
component_tastypie: django<1.12
183+
component_tastypie-tastypie0143: django-tastypie<0.14.4
184+
component_tastypie-{py27,pypy}-tastypie0143: django<1.12
185+
component_tastypie-{py36,py37,py38,py39,pypy3}-tastypie0143: django<3.0.1
186+
component_tastypie-tastypielatest: django-tastypie
187+
component_tastypie-tastypielatest: django
186188
coroutines_asyncio: uvloop
187189
cross_agent: mock==1.0.1
188190
cross_agent: requests

0 commit comments

Comments
 (0)