Skip to content

Commit

Permalink
fix: fix request.is_ajax() deprecation warning (openedx#33055)
Browse files Browse the repository at this point in the history
* fix: fix request.is_ajax() deprecation warning
  • Loading branch information
UsamaSadiq committed Aug 21, 2023
1 parent a61e7dc commit 3949c73
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cms/djangoapps/contentstore/views/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cms/djangoapps/maintenance/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion common/djangoapps/util/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions lms/djangoapps/discussion/django_comment_client/base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 10 additions & 5 deletions lms/djangoapps/discussion/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions lms/djangoapps/discussion/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion lms/djangoapps/instructor/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3949c73

Please sign in to comment.