From dd2afc1cbf0bb31e3b81ccc7982c8c7a32f19a46 Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Mon, 18 Apr 2022 15:08:18 -0400 Subject: [PATCH 1/2] Fixed undefined function --- django_ajax/encoder.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/django_ajax/encoder.py b/django_ajax/encoder.py index 82ff2e2..bfda291 100644 --- a/django_ajax/encoder.py +++ b/django_ajax/encoder.py @@ -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 @@ -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: @@ -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) From 9c195309af47c9476b7b89d1543b501f86e7e806 Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Mon, 18 Apr 2022 15:13:19 -0400 Subject: [PATCH 2/2] Testing django 4 --- .travis.yml | 10 ++++++---- django_ajax/decorators.py | 4 ++-- django_ajax/middleware.py | 4 ++-- tests/ajaxdecorator/urls.py | 18 +++++++++++++++--- tests/ajaxmiddleware/urls.py | 18 +++++++++++++++--- 5 files changed, 40 insertions(+), 14 deletions(-) diff --git a/.travis.yml b/.travis.yml index 171af1b..c17ee01 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/django_ajax/decorators.py b/django_ajax/decorators.py index 03a9dc3..35e99ab 100644 --- a/django_ajax/decorators.py +++ b/django_ajax/decorators.py @@ -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) diff --git a/django_ajax/middleware.py b/django_ajax/middleware.py index 1c6ab98..d424393 100644 --- a/django_ajax/middleware.py +++ b/django_ajax/middleware.py @@ -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 @@ -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 diff --git a/tests/ajaxdecorator/urls.py b/tests/ajaxdecorator/urls.py index e3b9bd6..c3cba19 100644 --- a/tests/ajaxdecorator/urls.py +++ b/tests/ajaxdecorator/urls.py @@ -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('', @@ -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'), @@ -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'), + ] diff --git a/tests/ajaxmiddleware/urls.py b/tests/ajaxmiddleware/urls.py index e3b9bd6..c3cba19 100644 --- a/tests/ajaxmiddleware/urls.py +++ b/tests/ajaxmiddleware/urls.py @@ -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('', @@ -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'), @@ -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'), + ]