Skip to content

Commit

Permalink
Merge pull request #61 from yceruto/force_str
Browse files Browse the repository at this point in the history
Fixed undefined force_text function
  • Loading branch information
yceruto authored Apr 18, 2022
2 parents f5aef9d + 9c19530 commit 1322370
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 17 deletions.
10 changes: 6 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
language: python

python:
- 3.5
- 3.6
- 3.7
- 3.8
- 3.9

env:
- DJANGO_VERSION=2.*
- DJANGO_VERSION=3.*
- DJANGO_VERSION=4.*

matrix:
exclude:
- python: 3.5
env: DJANGO_VERSION=3.*
- python: 3.6
env: DJANGO_VERSION=4.*
- python: 3.8
env: DJANGO_VERSION=2.*
- python: 3.9
env: DJANGO_VERSION=2.*

install:
- pip install django==$DJANGO_VERSION
Expand Down
4 changes: 2 additions & 2 deletions django_ajax/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ def my_view(request):
def decorator(func):
@wraps(func, assigned=WRAPPER_ASSIGNMENTS)
def inner(request, *args, **kwargs):
if mandatory and not request.is_ajax():
if mandatory and not request.headers.get('x-requested-with') == 'XMLHttpRequest':
return HttpResponseBadRequest()

if request.is_ajax():
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
# return json response
try:
return render_to_json(func(request, *args, **kwargs), request, **ajax_kwargs)
Expand Down
6 changes: 3 additions & 3 deletions django_ajax/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from datetime import date
from django.http.response import HttpResponseRedirectBase, HttpResponse
from django.template.response import TemplateResponse
from django.utils.encoding import force_text
from django.utils.encoding import force_str
from django.db.models.base import ModelBase
from decimal import Decimal

Expand All @@ -28,7 +28,7 @@ def default(self, obj):
elif issubclass(type(obj), HttpResponse):
return obj.content
elif issubclass(type(obj), Exception) or isinstance(obj, bytes):
return force_text(obj)
return force_str(obj)

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

# this handlers Models
if isinstance(obj.__class__, ModelBase):
return force_text(obj)
return force_str(obj)

if isinstance(obj, Decimal):
return float(obj)
Expand Down
4 changes: 2 additions & 2 deletions django_ajax/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def process_response(self, request, response):
If the request was made by AJAX then convert response to JSON,
otherwise return the original response.
"""
if request.is_ajax():
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
return render_to_json(response)
return response

Expand All @@ -26,5 +26,5 @@ def process_exception(self, request, exception):
Catch exception if the request was made by AJAX,
after will become up on JSON.
"""
if request.is_ajax():
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
return exception
18 changes: 15 additions & 3 deletions tests/ajaxdecorator/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import django

try:
from django.conf.urls import patterns, include, url
try:
from django.conf.urls import patterns, include, url
except ImportError:
from django.conf.urls import include, url
except ImportError:
from django.conf.urls import include, url
from django.urls import include, re_path

if django.VERSION < (1, 8):
urlpatterns = patterns('',
Expand All @@ -16,7 +19,7 @@
url(r'^ajax/exception$', views.exception_view, name='exception'),
url(r'^ajax/raise-exception$', views.raise_exception_view, name='raise_exception'),
)
else:
elif django.VERSION < (4, 0):
urlpatterns = [
url(r'^ajax/foo$', views.foo_view, name='foo'),
url(r'^ajax/login-required$', views.login_required_view, name='login_required'),
Expand All @@ -25,3 +28,12 @@
url(r'^ajax/exception$', views.exception_view, name='exception'),
url(r'^ajax/raise-exception$', views.raise_exception_view, name='raise_exception'),
]
else:
urlpatterns = [
re_path(r'^ajax/foo$', views.foo_view, name='foo'),
re_path(r'^ajax/login-required$', views.login_required_view, name='login_required'),
re_path(r'^ajax/render$', views.render_view, name='render'),
re_path(r'^ajax/render-class-based-view$', views.SimpleView.as_view(), name='render_class_based_view'),
re_path(r'^ajax/exception$', views.exception_view, name='exception'),
re_path(r'^ajax/raise-exception$', views.raise_exception_view, name='raise_exception'),
]
18 changes: 15 additions & 3 deletions tests/ajaxmiddleware/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import django

try:
from django.conf.urls import patterns, include, url
try:
from django.conf.urls import patterns, include, url
except ImportError:
from django.conf.urls import include, url
except ImportError:
from django.conf.urls import include, url
from django.urls import include, re_path

if django.VERSION < (1, 8):
urlpatterns = patterns('',
Expand All @@ -16,7 +19,7 @@
url(r'^ajax/exception$', views.exception_view, name='exception'),
url(r'^ajax/raise-exception$', views.raise_exception_view, name='raise_exception'),
)
else:
elif django.VERSION < (4, 0):
urlpatterns = [
url(r'^ajax/foo$', views.foo_view, name='foo'),
url(r'^ajax/login-required$', views.login_required_view, name='login_required'),
Expand All @@ -25,3 +28,12 @@
url(r'^ajax/exception$', views.exception_view, name='exception'),
url(r'^ajax/raise-exception$', views.raise_exception_view, name='raise_exception'),
]
else:
urlpatterns = [
re_path(r'^ajax/foo$', views.foo_view, name='foo'),
re_path(r'^ajax/login-required$', views.login_required_view, name='login_required'),
re_path(r'^ajax/render$', views.render_view, name='render'),
re_path(r'^ajax/render-class-based-view$', views.SimpleView.as_view(), name='render_class_based_view'),
re_path(r'^ajax/exception$', views.exception_view, name='exception'),
re_path(r'^ajax/raise-exception$', views.raise_exception_view, name='raise_exception'),
]

0 comments on commit 1322370

Please sign in to comment.