Skip to content

Commit

Permalink
Merge pull request #206 from kmichel-sereema/django-20-21-22-30
Browse files Browse the repository at this point in the history
Add support for Django 2.0 to 3.0
  • Loading branch information
srprash authored Mar 6, 2020
2 parents a839d7f + 16a00d7 commit ae3f213
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 3 deletions.
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

0 comments on commit ae3f213

Please sign in to comment.