diff --git a/cms/djangoapps/contentstore/views/error.py b/cms/djangoapps/contentstore/views/error.py index 4506ba51f528..5a9c23490ba6 100644 --- a/cms/djangoapps/contentstore/views/error.py +++ b/cms/djangoapps/contentstore/views/error.py @@ -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) diff --git a/cms/djangoapps/maintenance/views.py b/cms/djangoapps/maintenance/views.py index 423e3745c142..357f64e90ebd 100644 --- a/cms/djangoapps/maintenance/views.py +++ b/cms/djangoapps/maintenance/views.py @@ -94,7 +94,7 @@ def render_response(self): """ 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) diff --git a/common/djangoapps/util/views.py b/common/djangoapps/util/views.py index 4bc4fd552295..8a7901b2fccd 100644 --- a/common/djangoapps/util/views.py +++ b/common/djangoapps/util/views.py @@ -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: diff --git a/lms/djangoapps/discussion/django_comment_client/base/views.py b/lms/djangoapps/discussion/django_comment_client/base/views.py index 54f8e493b52b..5ad3dae8992b 100644 --- a/lms/djangoapps/discussion/django_comment_client/base/views.py +++ b/lms/djangoapps/discussion/django_comment_client/base/views.py @@ -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)) diff --git a/lms/djangoapps/discussion/django_comment_client/middleware.py b/lms/djangoapps/discussion/django_comment_client/middleware.py index b3133f710b15..e0095512cbb6 100644 --- a/lms/djangoapps/discussion/django_comment_client/middleware.py +++ b/lms/djangoapps/discussion/django_comment_client/middleware.py @@ -20,7 +20,8 @@ def process_exception(self, request, exception): 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: diff --git a/lms/djangoapps/discussion/tests/test_views.py b/lms/djangoapps/discussion/tests/test_views.py index 6b7f2de3ee5f..ada3db7792e2 100644 --- a/lms/djangoapps/discussion/tests/test_views.py +++ b/lms/djangoapps/discussion/tests/test_views.py @@ -1635,7 +1635,8 @@ def _test_unicode_data(self, text, mock_request): # lint-amnesty, pylint: disab 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 @@ def _test_unicode_data(self, text, mock_request): # lint-amnesty, pylint: disab } 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 @@ def _test_unicode_data(self, text, mock_request): # lint-amnesty, pylint: disab 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 @@ def _test_unicode_data(self, text, mock_request): # lint-amnesty, pylint: disab 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 @@ def _test_unicode_data(self, text, mock_request): # lint-amnesty, pylint: disab 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 diff --git a/lms/djangoapps/discussion/views.py b/lms/djangoapps/discussion/views.py index 74a365bcdf8e..bfa511a575bd 100644 --- a/lms/djangoapps/discussion/views.py +++ b/lms/djangoapps/discussion/views.py @@ -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({ diff --git a/lms/djangoapps/instructor/views/api.py b/lms/djangoapps/instructor/views/api.py index ed3a733a0e5c..778cbe5c8b3e 100644 --- a/lms/djangoapps/instructor/views/api.py +++ b/lms/djangoapps/instructor/views/api.py @@ -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)