diff --git a/swh/web/admin/deposit.py b/swh/web/admin/deposit.py --- a/swh/web/admin/deposit.py +++ b/swh/web/admin/deposit.py @@ -3,16 +3,14 @@ # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information -import json -import requests - from django.core.cache import cache from django.conf import settings from django.contrib.admin.views.decorators import staff_member_required from django.core.paginator import Paginator -from django.http import HttpResponse +from django.http import JsonResponse from django.shortcuts import render +import requests from requests.auth import HTTPBasicAuth import sentry_sdk @@ -108,4 +106,4 @@ "An error occurred while retrieving " "the list of deposits !" ) - return HttpResponse(json.dumps(table_data), content_type="application/json") + return JsonResponse(table_data) diff --git a/swh/web/admin/origin_save.py b/swh/web/admin/origin_save.py --- a/swh/web/admin/origin_save.py +++ b/swh/web/admin/origin_save.py @@ -3,13 +3,11 @@ # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information -import json - from django.conf import settings from django.contrib.admin.views.decorators import staff_member_required from django.core.exceptions import ObjectDoesNotExist from django.core.paginator import Paginator -from django.http import HttpResponse +from django.http import HttpResponse, JsonResponse from django.shortcuts import render from django.views.decorators.http import require_POST @@ -56,8 +54,7 @@ paginator = Paginator(urls_query_set, length) urls_query_set = paginator.page(page).object_list table_data["data"] = [{"url": u.url} for u in urls_query_set] - table_data_json = json.dumps(table_data, separators=(",", ": ")) - return HttpResponse(table_data_json, content_type="application/json") + return JsonResponse(table_data) @admin_route( diff --git a/swh/web/browse/views/content.py b/swh/web/browse/views/content.py --- a/swh/web/browse/views/content.py +++ b/swh/web/browse/views/content.py @@ -4,11 +4,10 @@ # See top-level LICENSE file for more information import difflib -import json from distutils.util import strtobool -from django.http import HttpResponse +from django.http import HttpResponse, JsonResponse from django.shortcuts import render from django.template.defaultfilters import filesizeformat import sentry_sdk @@ -174,8 +173,7 @@ diff_data["diff_str"] = diff_str diff_data["language"] = language - diff_data_json = json.dumps(diff_data, separators=(",", ": ")) - return HttpResponse(diff_data_json, content_type="application/json") + return JsonResponse(diff_data) @browse_route( diff --git a/swh/web/browse/views/revision.py b/swh/web/browse/views/revision.py --- a/swh/web/browse/views/revision.py +++ b/swh/web/browse/views/revision.py @@ -7,7 +7,7 @@ import json import textwrap -from django.http import HttpResponse +from django.http import JsonResponse from django.shortcuts import render from django.template.defaultfilters import filesizeformat from django.utils.safestring import mark_safe @@ -187,8 +187,7 @@ "changes": changes[:_max_displayed_file_diffs], "changes_msg": changes_msg, } - diff_data_json = json.dumps(diff_data, separators=(",", ": ")) - return HttpResponse(diff_data_json, content_type="application/json") + return JsonResponse(diff_data) NB_LOG_ENTRIES = 100 diff --git a/swh/web/misc/coverage.py b/swh/web/misc/coverage.py --- a/swh/web/misc/coverage.py +++ b/swh/web/misc/coverage.py @@ -3,11 +3,9 @@ # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information -import json - from django.conf.urls import url from django.core.cache import caches -from django.http import HttpResponse +from django.http import JsonResponse from django.shortcuts import render from django.views.decorators.cache import never_cache from django.views.decorators.clickjacking import xframe_options_exempt @@ -203,11 +201,10 @@ "origin_types": code_provider["origin_types"], } ) - results = json.dumps(results) except Exception as exc: return handle_view_exception(request, exc, html_response=False) - return HttpResponse(results, content_type="application/json") + return JsonResponse(results) urlpatterns = [ diff --git a/swh/web/misc/origin_save.py b/swh/web/misc/origin_save.py --- a/swh/web/misc/origin_save.py +++ b/swh/web/misc/origin_save.py @@ -3,11 +3,9 @@ # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information -import json - from django.conf.urls import url from django.core.paginator import Paginator -from django.http import HttpResponse, HttpResponseForbidden, HttpResponseServerError +from django.http import JsonResponse from django.shortcuts import render from rest_framework.decorators import api_view, authentication_classes @@ -42,23 +40,17 @@ per user to avoid being possibly flooded by bots. """ try: - response = json.dumps( - create_save_origin_request(visit_type, origin_url), separators=(",", ": ") - ) - return HttpResponse(response, content_type="application/json") + response = create_save_origin_request(visit_type, origin_url) + return JsonResponse(response) except ForbiddenExc as exc: - return HttpResponseForbidden( - json.dumps({"detail": str(exc)}), content_type="application/json" - ) + return JsonResponse({"detail": str(exc)}, status=403) except Exception as exc: - return HttpResponseServerError( - json.dumps({"detail": str(exc)}), content_type="application/json" - ) + return JsonResponse({"detail": str(exc)}, status=500) def _visit_save_types_list(request): - visit_types = json.dumps(get_savable_visit_types(), separators=(",", ": ")) - return HttpResponse(visit_types, content_type="application/json") + visit_types = get_savable_visit_types() + return JsonResponse(visit_types, safe=False) def _origin_save_requests_list(request, status): @@ -98,8 +90,7 @@ table_data["recordsFiltered"] = len(save_requests) paginator = Paginator(save_requests, length) table_data["data"] = paginator.page(page).object_list - table_data_json = json.dumps(table_data, separators=(",", ": ")) - return HttpResponse(table_data_json, content_type="application/json") + return JsonResponse(table_data) def _save_origin_task_info(request, save_request_id): @@ -109,7 +100,7 @@ for date_field in ("scheduled", "started", "ended"): if date_field in request_info and request_info[date_field] is not None: request_info[date_field] = request_info[date_field].isoformat() - return HttpResponse(json.dumps(request_info), content_type="application/json") + return JsonResponse(request_info) urlpatterns = [ diff --git a/swh/web/misc/urls.py b/swh/web/misc/urls.py --- a/swh/web/misc/urls.py +++ b/swh/web/misc/urls.py @@ -10,7 +10,7 @@ from django.conf.urls import url, include from django.contrib.staticfiles import finders -from django.http import HttpResponse +from django.http import JsonResponse from django.shortcuts import render from swh.web.common import service @@ -28,7 +28,7 @@ def _stat_counters(request): - stat = service.stat_counters() + stat_counters = service.stat_counters() url = get_config()["history_counters_url"] stat_counters_history = "null" if url: @@ -37,11 +37,11 @@ stat_counters_history = response.text except Exception as exc: sentry_sdk.capture_exception(exc) - json_data = '{"stat_counters": %s, "stat_counters_history": %s}' % ( - json.dumps(stat), - stat_counters_history, - ) - return HttpResponse(json_data, content_type="application/json") + counters = { + "stat_counters": stat_counters, + "stat_counters_history": stat_counters_history, + } + return JsonResponse(counters) urlpatterns = [