fix: fix request.is_ajax() deprecation warning (#33055)

* fix: fix request.is_ajax() deprecation warning
This commit is contained in:
Usama Sadiq
2023-08-21 15:59:00 +05:00
committed by GitHub
parent a61e7dc524
commit 3949c73b35
8 changed files with 26 additions and 20 deletions

View File

@@ -18,7 +18,7 @@ def jsonable_error(status=500, message="The Studio servers encountered an error"
def outer(func):
@functools.wraps(func)
def inner(request, *args, **kwargs):
if request.is_ajax():
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
content = dump_js_escaped_json({"error": message})
return HttpResponse(content, content_type="application/json", # lint-amnesty, pylint: disable=http-response-with-content-type-json
status=status)

View File

@@ -94,7 +94,7 @@ class MaintenanceBaseView(View):
"""
A short method to render_to_response that renders response.
"""
if self.request.is_ajax():
if self.request.headers.get('x-requested-with') == 'XMLHttpRequest':
return JsonResponse(self.context)
return render_to_response(self.template, self.context)

View File

@@ -102,7 +102,7 @@ def jsonable_server_error(request, template_name='500.html'):
500 error handler that serves JSON on an AJAX request, and proxies
to the Django default `server_error` view otherwise.
"""
if request.is_ajax():
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
msg = {"error": "The edX servers encountered an error"}
return HttpResponseServerError(json.dumps(msg))
else:

View File

@@ -531,7 +531,7 @@ def create_thread(request, course_id, commentable_id):
track_thread_created_event(request, course, thread, follow)
if request.is_ajax():
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
return ajax_content_response(request, course_key, data)
else:
return JsonResponse(prepare_content(data, course_key))
@@ -573,7 +573,7 @@ def update_thread(request, course_id, thread_id):
thread_edited.send(sender=None, user=user, post=thread)
track_thread_edited_event(request, course, thread, None)
if request.is_ajax():
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
return ajax_content_response(request, course_key, thread.to_dict())
else:
return JsonResponse(prepare_content(thread.to_dict(), course_key))
@@ -623,7 +623,7 @@ def _create_comment(request, course_key, thread_id=None, parent_id=None):
track_comment_created_event(request, course, comment, comment.thread.commentable_id, followed)
if request.is_ajax():
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
return ajax_content_response(request, course_key, comment.to_dict())
else:
return JsonResponse(prepare_content(comment.to_dict(), course.id))
@@ -679,7 +679,7 @@ def update_comment(request, course_id, comment_id):
comment_edited.send(sender=None, user=request.user, post=comment)
track_comment_edited_event(request, course, comment, None)
if request.is_ajax():
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
return ajax_content_response(request, course_key, comment.to_dict())
else:
return JsonResponse(prepare_content(comment.to_dict(), course_key))

View File

@@ -20,7 +20,8 @@ class AjaxExceptionMiddleware(MiddlewareMixin):
Processes CommentClientRequestErrors in ajax requests. If the request is an ajax request,
returns a http response that encodes the error as json
"""
if isinstance(exception, CommentClientRequestError) and request.is_ajax():
if isinstance(exception, CommentClientRequestError)\
and request.headers.get('x-requested-with') == 'XMLHttpRequest':
try:
return JsonError(json.loads(str(exception)), exception.status_code)
except ValueError:

View File

@@ -1635,7 +1635,8 @@ class ForumFormDiscussionUnicodeTestCase(ForumsEnableMixin, SharedModuleStoreTes
mock_request.side_effect = make_mock_request_impl(course=self.course, text=text)
request = RequestFactory().get("dummy_url")
request.user = self.student
request.META["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest" # so request.is_ajax() == True
# so (request.headers.get('x-requested-with') == 'XMLHttpRequest') == True
request.META["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest"
response = views.forum_form_discussion(request, str(self.course.id))
assert response.status_code == 200
@@ -1723,7 +1724,8 @@ class ForumDiscussionSearchUnicodeTestCase(ForumsEnableMixin, SharedModuleStoreT
}
request = RequestFactory().get("dummy_url", data)
request.user = self.student
request.META["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest" # so request.is_ajax() == True
# so (request.headers.get('x-requested-with') == 'XMLHttpRequest') == True
request.META["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest"
response = views.forum_form_discussion(request, str(self.course.id))
assert response.status_code == 200
@@ -1753,7 +1755,8 @@ class SingleThreadUnicodeTestCase(ForumsEnableMixin, SharedModuleStoreTestCase,
mock_request.side_effect = make_mock_request_impl(course=self.course, text=text, thread_id=thread_id)
request = RequestFactory().get("dummy_url")
request.user = self.student
request.META["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest" # so request.is_ajax() == True
# so (request.headers.get('x-requested-with') == 'XMLHttpRequest') == True
request.META["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest"
response = views.single_thread(request, str(self.course.id), "dummy_discussion_id", thread_id)
assert response.status_code == 200
@@ -1782,7 +1785,8 @@ class UserProfileUnicodeTestCase(ForumsEnableMixin, SharedModuleStoreTestCase, U
mock_request.side_effect = make_mock_request_impl(course=self.course, text=text)
request = RequestFactory().get("dummy_url")
request.user = self.student
request.META["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest" # so request.is_ajax() == True
# so (request.headers.get('x-requested-with') == 'XMLHttpRequest') == True
request.META["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest"
response = views.user_profile(request, str(self.course.id), str(self.student.id))
assert response.status_code == 200
@@ -1811,7 +1815,8 @@ class FollowedThreadsUnicodeTestCase(ForumsEnableMixin, SharedModuleStoreTestCas
mock_request.side_effect = make_mock_request_impl(course=self.course, text=text)
request = RequestFactory().get("dummy_url")
request.user = self.student
request.META["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest" # so request.is_ajax() == True
# so (request.headers.get('x-requested-with') == 'XMLHttpRequest') == True
request.META["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest"
response = views.followed_threads(request, str(self.course.id), str(self.student.id))
assert response.status_code == 200

View File

@@ -288,7 +288,7 @@ def forum_form_discussion(request, course_key):
"""
course = get_course_with_access(request.user, 'load', course_key, check_if_enrolled=True)
request.user.is_community_ta = utils.is_user_community_ta(request.user, course.id)
if request.is_ajax():
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
user = cc.User.from_django_user(request.user)
user_info = user.to_dict()
@@ -354,7 +354,7 @@ def single_thread(request, course_key, discussion_id, thread_id):
course = get_course_with_access(request.user, 'load', course_key, check_if_enrolled=True)
request.user.is_community_ta = utils.is_user_community_ta(request.user, course.id)
if request.is_ajax():
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
cc_user = cc.User.from_django_user(request.user)
user_info = cc_user.to_dict()
is_staff = has_permission(request.user, 'openclose_thread', course.id)
@@ -413,8 +413,8 @@ def _find_thread(request, course, discussion_id, thread_id):
"""
try:
thread = cc.Thread.find(thread_id).retrieve(
with_responses=request.is_ajax(),
recursive=request.is_ajax(),
with_responses=request.headers.get('x-requested-with') == 'XMLHttpRequest',
recursive=request.headers.get('x-requested-with') == 'XMLHttpRequest',
user_id=request.user.id,
response_skip=request.GET.get("resp_skip"),
response_limit=request.GET.get("resp_limit")
@@ -644,7 +644,7 @@ def user_profile(request, course_key, user_id):
"""
try:
context = create_user_profile_context(request, course_key, user_id)
if request.is_ajax():
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
return utils.JsonResponse({
'discussion_data': context['threads'],
'page': context['page'],
@@ -721,7 +721,7 @@ def followed_threads(request, course_key, user_id):
paginated_results.collection,
request.user, user_info
)
if request.is_ajax():
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
is_staff = has_permission(request.user, 'openclose_thread', course.id)
is_community_ta = utils.is_user_community_ta(request.user, course.id)
return utils.JsonResponse({

View File

@@ -152,7 +152,7 @@ def common_exceptions_400(func):
"""
def wrapped(request, *args, **kwargs):
use_json = (request.is_ajax() or
use_json = (request.headers.get('x-requested-with') == 'XMLHttpRequest' or
request.META.get("HTTP_ACCEPT", "").startswith("application/json"))
try:
return func(request, *args, **kwargs)