Skip to content

Commit 1322370

Browse files
authored
Merge pull request #61 from yceruto/force_str
Fixed undefined force_text function
2 parents f5aef9d + 9c19530 commit 1322370

File tree

6 files changed

+43
-17
lines changed

6 files changed

+43
-17
lines changed

.travis.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
language: python
22

33
python:
4-
- 3.5
54
- 3.6
6-
- 3.7
75
- 3.8
6+
- 3.9
87

98
env:
109
- DJANGO_VERSION=2.*
1110
- DJANGO_VERSION=3.*
11+
- DJANGO_VERSION=4.*
1212

1313
matrix:
1414
exclude:
15-
- python: 3.5
16-
env: DJANGO_VERSION=3.*
15+
- python: 3.6
16+
env: DJANGO_VERSION=4.*
1717
- python: 3.8
1818
env: DJANGO_VERSION=2.*
19+
- python: 3.9
20+
env: DJANGO_VERSION=2.*
1921

2022
install:
2123
- pip install django==$DJANGO_VERSION

django_ajax/decorators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ def my_view(request):
5858
def decorator(func):
5959
@wraps(func, assigned=WRAPPER_ASSIGNMENTS)
6060
def inner(request, *args, **kwargs):
61-
if mandatory and not request.is_ajax():
61+
if mandatory and not request.headers.get('x-requested-with') == 'XMLHttpRequest':
6262
return HttpResponseBadRequest()
6363

64-
if request.is_ajax():
64+
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
6565
# return json response
6666
try:
6767
return render_to_json(func(request, *args, **kwargs), request, **ajax_kwargs)

django_ajax/encoder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from datetime import date
88
from django.http.response import HttpResponseRedirectBase, HttpResponse
99
from django.template.response import TemplateResponse
10-
from django.utils.encoding import force_text
10+
from django.utils.encoding import force_str
1111
from django.db.models.base import ModelBase
1212
from decimal import Decimal
1313

@@ -28,7 +28,7 @@ def default(self, obj):
2828
elif issubclass(type(obj), HttpResponse):
2929
return obj.content
3030
elif issubclass(type(obj), Exception) or isinstance(obj, bytes):
31-
return force_text(obj)
31+
return force_str(obj)
3232

3333
# this handles querysets and other iterable types
3434
try:
@@ -40,7 +40,7 @@ def default(self, obj):
4040

4141
# this handlers Models
4242
if isinstance(obj.__class__, ModelBase):
43-
return force_text(obj)
43+
return force_str(obj)
4444

4545
if isinstance(obj, Decimal):
4646
return float(obj)

django_ajax/middleware.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def process_response(self, request, response):
1717
If the request was made by AJAX then convert response to JSON,
1818
otherwise return the original response.
1919
"""
20-
if request.is_ajax():
20+
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
2121
return render_to_json(response)
2222
return response
2323

@@ -26,5 +26,5 @@ def process_exception(self, request, exception):
2626
Catch exception if the request was made by AJAX,
2727
after will become up on JSON.
2828
"""
29-
if request.is_ajax():
29+
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
3030
return exception

tests/ajaxdecorator/urls.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import django
44

55
try:
6-
from django.conf.urls import patterns, include, url
6+
try:
7+
from django.conf.urls import patterns, include, url
8+
except ImportError:
9+
from django.conf.urls import include, url
710
except ImportError:
8-
from django.conf.urls import include, url
11+
from django.urls import include, re_path
912

1013
if django.VERSION < (1, 8):
1114
urlpatterns = patterns('',
@@ -16,7 +19,7 @@
1619
url(r'^ajax/exception$', views.exception_view, name='exception'),
1720
url(r'^ajax/raise-exception$', views.raise_exception_view, name='raise_exception'),
1821
)
19-
else:
22+
elif django.VERSION < (4, 0):
2023
urlpatterns = [
2124
url(r'^ajax/foo$', views.foo_view, name='foo'),
2225
url(r'^ajax/login-required$', views.login_required_view, name='login_required'),
@@ -25,3 +28,12 @@
2528
url(r'^ajax/exception$', views.exception_view, name='exception'),
2629
url(r'^ajax/raise-exception$', views.raise_exception_view, name='raise_exception'),
2730
]
31+
else:
32+
urlpatterns = [
33+
re_path(r'^ajax/foo$', views.foo_view, name='foo'),
34+
re_path(r'^ajax/login-required$', views.login_required_view, name='login_required'),
35+
re_path(r'^ajax/render$', views.render_view, name='render'),
36+
re_path(r'^ajax/render-class-based-view$', views.SimpleView.as_view(), name='render_class_based_view'),
37+
re_path(r'^ajax/exception$', views.exception_view, name='exception'),
38+
re_path(r'^ajax/raise-exception$', views.raise_exception_view, name='raise_exception'),
39+
]

tests/ajaxmiddleware/urls.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import django
44

55
try:
6-
from django.conf.urls import patterns, include, url
6+
try:
7+
from django.conf.urls import patterns, include, url
8+
except ImportError:
9+
from django.conf.urls import include, url
710
except ImportError:
8-
from django.conf.urls import include, url
11+
from django.urls import include, re_path
912

1013
if django.VERSION < (1, 8):
1114
urlpatterns = patterns('',
@@ -16,7 +19,7 @@
1619
url(r'^ajax/exception$', views.exception_view, name='exception'),
1720
url(r'^ajax/raise-exception$', views.raise_exception_view, name='raise_exception'),
1821
)
19-
else:
22+
elif django.VERSION < (4, 0):
2023
urlpatterns = [
2124
url(r'^ajax/foo$', views.foo_view, name='foo'),
2225
url(r'^ajax/login-required$', views.login_required_view, name='login_required'),
@@ -25,3 +28,12 @@
2528
url(r'^ajax/exception$', views.exception_view, name='exception'),
2629
url(r'^ajax/raise-exception$', views.raise_exception_view, name='raise_exception'),
2730
]
31+
else:
32+
urlpatterns = [
33+
re_path(r'^ajax/foo$', views.foo_view, name='foo'),
34+
re_path(r'^ajax/login-required$', views.login_required_view, name='login_required'),
35+
re_path(r'^ajax/render$', views.render_view, name='render'),
36+
re_path(r'^ajax/render-class-based-view$', views.SimpleView.as_view(), name='render_class_based_view'),
37+
re_path(r'^ajax/exception$', views.exception_view, name='exception'),
38+
re_path(r'^ajax/raise-exception$', views.raise_exception_view, name='raise_exception'),
39+
]

0 commit comments

Comments
 (0)