Skip to content

Add support for Django 2.0 to 3.0 #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 6, 2020
5 changes: 5 additions & 0 deletions aws_xray_sdk/ext/django/templates.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

from django.template import Template
from django.utils.safestring import SafeString

from aws_xray_sdk.core import xray_recorder

Expand All @@ -22,6 +23,10 @@ def xray_render(self, context):
template_name = self.name or getattr(context, 'template_name', None)
if template_name:
name = str(template_name)
# SafeString are not properly serialized by jsonpickle,
# turn them back to str by adding a non-safe str.
if isinstance(name, SafeString):
name += ''
subsegment = xray_recorder.current_subsegment()
if subsegment:
subsegment.name = name
Expand Down
1 change: 0 additions & 1 deletion tests/ext/django/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
Expand Down
1 change: 1 addition & 0 deletions tests/ext/django/app/templates/block.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Hello World</p>
10 changes: 10 additions & 0 deletions tests/ext/django/app/templates/block_user.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<body>

<h1>Django Test App</h1>

{% include "block.html" %}

</body>
</html>
5 changes: 5 additions & 0 deletions tests/ext/django/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class IndexView(TemplateView):
template_name = 'index.html'


class TemplateBlockView(TemplateView):
template_name = 'block_user.html'


def ok(request):
return HttpResponse(status=200)

Expand All @@ -32,4 +36,5 @@ def call_db(request):
url(r'^500fault/$', fault, name='500fault'),
url(r'^call_db/$', call_db, name='call_db'),
url(r'^template/$', IndexView.as_view(), name='template'),
url(r'^template_block/$', TemplateBlockView.as_view(), name='template_block'),
]
13 changes: 12 additions & 1 deletion tests/ext/django/test_middleware.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import django
from aws_xray_sdk import global_sdk_config
from django.core.urlresolvers import reverse
from django.urls import reverse
from django.test import TestCase

from aws_xray_sdk.core import xray_recorder, lambda_launcher
Expand Down Expand Up @@ -90,6 +90,17 @@ def test_template(self):
assert not subsegment.in_progress
assert subsegment.namespace == 'local'

def test_template_block(self):
url = reverse('template_block')
self.client.get(url)
segment = xray_recorder.emitter.pop()
assert len(segment.subsegments) == 1

subsegment = segment.subsegments[0]
assert subsegment.name == 'block_user.html'
assert not subsegment.in_progress
assert subsegment.namespace == 'local'

def test_trace_header_data_perservation(self):
url = reverse('200ok')
self.client.get(url, HTTP_X_AMZN_TRACE_ID='k1=v1')
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ deps =
Flask-SQLAlchemy
future
# the sdk doesn't support earlier version of django
django >= 1.10, <2.0
django >= 1.10
django-fake-model
pynamodb >= 3.3.1
psycopg2
Expand Down