Skip to content

Commit

Permalink
Merge branch 'em/set-correct-content-type' into autostaging
Browse files Browse the repository at this point in the history
  • Loading branch information
nospame committed Sep 11, 2024
2 parents 98592e1 + 0005991 commit 88b064e
Show file tree
Hide file tree
Showing 31 changed files with 99 additions and 100 deletions.
8 changes: 3 additions & 5 deletions corehq/apps/accounting/decorators.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
from functools import wraps

from django.http import HttpResponse
from django.http import HttpResponse, JsonResponse

from django_prbac.decorators import requires_privilege
from django_prbac.exceptions import PermissionDenied
Expand Down Expand Up @@ -65,7 +64,7 @@ def requires_privilege_plaintext_response(slug,
"""
A version of the requires_privilege decorator which returns an
HttpResponse object with HTTP Status Code of 412 by default and
content_type of tex/plain if the privilege is not found.
content_type of text/plain if the privilege is not found.
"""
def decorate(fn):
@wraps(fn)
Expand Down Expand Up @@ -114,8 +113,7 @@ def wrapped(request, *args, **kwargs):
except PermissionDenied:
error_message = "You have lost access to this feature."
response = get_response(error_message, http_status_code)
return HttpResponse(json.dumps(response),
content_type="application/json", status=401)
return JsonResponse(response, status=401)
return wrapped
return decorate

Expand Down
2 changes: 1 addition & 1 deletion corehq/apps/api/object_fetch_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def get(self, request, domain, case_id=None, attachment_id=None):
if attachment_meta is not None:
mime_type = attachment_meta['content_type']
else:
mime_type = "plain/text"
mime_type = "text/plain"

return StreamingHttpResponse(streaming_content=FileWrapper(attachment_stream),
content_type=mime_type)
Expand Down
13 changes: 5 additions & 8 deletions corehq/apps/api/resources/auth.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import json
from functools import wraps

from django.core.exceptions import PermissionDenied
from django.http import Http404, HttpResponse, HttpResponseForbidden
from django.http import Http404, HttpResponseForbidden, JsonResponse

from attrs import define, field
from tastypie.authentication import Authentication
Expand All @@ -28,11 +27,9 @@ def _inner(req, *args, **kwargs):
return view_func(req, *args, **kwargs)
except Http404 as e:
if str(e):
return HttpResponse(json.dumps({"error": str(e)}),
content_type="application/json",
status=404)
return HttpResponse(json.dumps({"error": "not authorized"}),
content_type="application/json",
return JsonResponse({"error": str(e)},
status=404)
return JsonResponse({"error": "not authorized"},
status=401)
return _inner

Expand Down Expand Up @@ -192,7 +189,7 @@ def _get_auth_decorator(self, request):
class DomainAdminAuthentication(LoginAndDomainAuthentication):

def is_authenticated(self, request, **kwargs):
permission_check = lambda couch_user, domain: couch_user.is_domain_admin(domain)
permission_check = lambda couch_user, domain: couch_user.is_domain_admin(domain) # noqa: E731
wrappers = [
require_permission_raw(permission_check, login_decorator=self._get_auth_decorator(request)),
wrap_4xx_errors_for_apis,
Expand Down
3 changes: 2 additions & 1 deletion corehq/apps/app_manager/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ def _safe_cached_download(request, *args, **kwargs):
if not username:
content_response = dict(error="app.update.not.allowed.user.logged_out",
default_response=_("Please log in to the app to check for an update."))
return HttpResponse(status=406, content=json.dumps(content_response))
return HttpResponse(status=406, content=json.dumps(content_response),
content_type='application/json')
if latest and not target:
latest_enabled_build = _get_latest_enabled_build(domain, username, app_id, request.GET.get('profile'),
location_flag_enabled)
Expand Down
6 changes: 3 additions & 3 deletions corehq/apps/app_manager/views/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ def require_remote_app():
if should_edit("custom_suite"):
app.set_custom_suite(hq_settings['custom_suite'])

return HttpResponse(json.dumps(resp))
return JsonResponse(resp)


@no_conflict_require_POST
Expand All @@ -959,7 +959,7 @@ def edit_add_ons(request, domain, app_id):
if slug in current:
app.add_ons[slug] = value == 'on'
app.save()
return HttpResponse(json.dumps({'success': True}))
return JsonResponse({'success': True})


@no_conflict_require_POST
Expand Down Expand Up @@ -1003,7 +1003,7 @@ def rearrange(request, domain, app_id, key):
return back_to_main(request, domain, app_id=app_id, module_id=module_id)
app.save(resp)
if ajax:
return HttpResponse(json.dumps(resp))
return JsonResponse(resp)
else:
return back_to_main(request, domain, app_id=app_id, module_id=module_id)

Expand Down
2 changes: 1 addition & 1 deletion corehq/apps/app_manager/views/formdesigner.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def get_form_data_schema(request, domain, app_id, form_unique_id):
kw = {}
if "pretty" in request.GET:
kw["indent"] = 2
return HttpResponse(json.dumps(data, **kw))
return HttpResponse(json.dumps(data, **kw), content_type='application/json')


@require_GET
Expand Down
5 changes: 2 additions & 3 deletions corehq/apps/app_manager/views/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ def should_edit(attribute):
app.save(resp)
notify_form_changed(domain, request.couch_user, app_id, form_unique_id)
if ajax:
return HttpResponse(json.dumps(resp))
return JsonResponse(resp)
else:
return back_to_main(request, domain, app_id=app_id, form_unique_id=form_unique_id)

Expand Down Expand Up @@ -588,8 +588,7 @@ def get_xform_source(request, domain, app_id, form_unique_id):

lang = request.COOKIES.get('lang', app.langs[0])
source = form.source
response = HttpResponse(source)
response['Content-Type'] = "application/xml"
response = HttpResponse(source, content_type='application/xml')
filename = form.default_name()
for lc in [lang] + app.langs:
if lc in form.name:
Expand Down
2 changes: 1 addition & 1 deletion corehq/apps/app_manager/views/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ def should_edit(attribute):

app.save(resp)
resp['case_list-show'] = module.requires_case_details()
return HttpResponse(json.dumps(resp))
return JsonResponse(resp)


def _new_advanced_module(request, domain, app, name, lang):
Expand Down
4 changes: 2 additions & 2 deletions corehq/apps/app_manager/views/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

from django.contrib import messages
from django.http import (
HttpResponse,
HttpResponseBadRequest,
HttpResponseRedirect,
JsonResponse,
)
from django.urls import reverse
from django.utils.decorators import method_decorator
Expand All @@ -30,7 +30,7 @@
@login_and_domain_required
def commcare_profile(request, domain, app_id):
app = get_app(domain, app_id)
return HttpResponse(json.dumps(app.profile))
return JsonResponse(app.profile, safe=False)


@no_conflict_require_POST
Expand Down
2 changes: 1 addition & 1 deletion corehq/apps/auditcare/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def export_all(request):
else:
return HttpResponseBadRequest(f'[start] {e}')

response = HttpResponse()
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="AuditAll.csv"'
write_export_from_all_log_events(response, start=start, end=end)
return response
5 changes: 1 addition & 4 deletions corehq/apps/domain/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,10 +830,7 @@ def get_custom_logo(self):
if not self.has_custom_logo:
return None

return (
self.fetch_attachment(LOGO_ATTACHMENT),
self.blobs[LOGO_ATTACHMENT].content_type
)
return self.fetch_attachment(LOGO_ATTACHMENT)

def get_odata_feed_limit(self):
return self.odata_feed_limit or settings.DEFAULT_ODATA_FEED_LIMIT
Expand Down
8 changes: 2 additions & 6 deletions corehq/apps/domain/views/feedback.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import json

from django.conf import settings
from django.http import HttpResponse
from django.http import JsonResponse
from django.views.decorators.http import require_POST

from corehq.apps.domain.decorators import login_and_domain_required
Expand Down Expand Up @@ -37,6 +35,4 @@ def submit_feedback(request, domain):
message,
[settings.FEEDBACK_EMAIL],
)
return HttpResponse(json.dumps({
'success': True,
}), content_type="application/json")
return JsonResponse({'success': True})
2 changes: 1 addition & 1 deletion corehq/apps/domain/views/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def logo(request, domain):
if logo is None:
raise Http404()

return HttpResponse(logo[0], content_type=logo[1])
return HttpResponse(logo, content_type='image/png')


class EditPrivacySecurityView(BaseAdminProjectSettingsView):
Expand Down
2 changes: 1 addition & 1 deletion corehq/apps/geospatial/static/geospatial/js/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ hqDefine('geospatial/js/models', [

self.exportSelectedPolygonGeoJson = function (data, event) {
if (self.activeSavedPolygon()) {
const convertedData = 'text/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(self.activeSavedPolygon().geoJson));
const convertedData = 'application/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(self.activeSavedPolygon().geoJson));
$(event.target).attr('href', 'data:' + convertedData);
$(event.target).attr('download',self.activeSavedPolygon().text + '.geojson');
return true;
Expand Down
6 changes: 3 additions & 3 deletions corehq/apps/hqadmin/views/data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json

from django.http import Http404, HttpResponse
from django.http import Http404, HttpResponse, JsonResponse
from django.shortcuts import render
from django.utils.translation import gettext as _

Expand Down Expand Up @@ -66,8 +66,8 @@ def raw_doc(request):
if 'doc' in context:
return HttpResponse(context['doc'], content_type="application/json")
else:
return HttpResponse(json.dumps({"status": "missing"}),
content_type="application/json", status=404)
return JsonResponse({"status": "missing"},
status=404)

context['all_databases'] = [db for db in get_databases()]
return render(request, "hqadmin/raw_doc.html", context)
Expand Down
5 changes: 2 additions & 3 deletions corehq/apps/hqadmin/views/system.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import socket
from collections import defaultdict, namedtuple

Expand Down Expand Up @@ -146,8 +145,8 @@ def system_ajax(request):
traw['name'] = None
ret.append(traw)
ret = sorted(ret, key=lambda x: x['succeeded'], reverse=True)
return HttpResponse(json.dumps(ret), content_type='application/json')
return HttpResponse('{}', content_type='application/json')
return JsonResponse(ret, safe=False)
return JsonResponse({})


@require_superuser_or_contractor
Expand Down
2 changes: 1 addition & 1 deletion corehq/apps/hqmedia/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ def post(self, request, *args, **kwargs):
response = fake_status.get_response()
else:
response = status.get_response()
return HttpResponse(json.dumps(response))
return JsonResponse(response)


class ViewMultimediaFile(View):
Expand Down
2 changes: 1 addition & 1 deletion corehq/apps/hqwebapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ def paginate_crud_response(self):
Return this in the post method of your view class.
"""
response = getattr(self, '%s_response' % self.action)
return HttpResponse(json.dumps(response, cls=LazyEncoder))
return HttpResponse(json.dumps(response, cls=LazyEncoder), content_type='application/json')

@property
def create_response(self):
Expand Down
11 changes: 8 additions & 3 deletions corehq/apps/products/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
from io import BytesIO

from django.contrib import messages
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.http import (
Http404,
HttpResponse,
HttpResponseRedirect,
JsonResponse,
)
from django.http.response import HttpResponseServerError
from django.shortcuts import render
from django.urls import reverse
Expand Down Expand Up @@ -207,11 +212,11 @@ def get_archive_config(self):
}

def get(self, request, *args, **kwargs):
return HttpResponse(json.dumps({
return JsonResponse({
'success': True,
'current_page': int(self.page),
'data_list': self.product_data,
}), 'text/json')
})


@method_decorator(use_bootstrap5, name='dispatch')
Expand Down
16 changes: 9 additions & 7 deletions corehq/apps/programs/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import json

from django.contrib import messages
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.http import (
Http404,
HttpResponseRedirect,
JsonResponse,
)
from django.urls import reverse
from django.utils.decorators import method_decorator
from django.utils.translation import gettext as _
Expand Down Expand Up @@ -68,10 +70,10 @@ def program_data(self):
return data

def get(self, request, *args, **kwargs):
return HttpResponse(json.dumps({
return JsonResponse({
'success': True,
'data_list': self.program_data,
}), 'text/json')
})


@method_decorator(use_bootstrap5, name='dispatch')
Expand Down Expand Up @@ -186,8 +188,8 @@ def get_product_data(self):
}

def get(self, request, *args, **kwargs):
return HttpResponse(json.dumps({
return JsonResponse({
'success': True,
'current_page': self.page,
'data_list': list(self.get_product_data()),
}), 'text/json')
})
5 changes: 2 additions & 3 deletions corehq/apps/reports/generic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import datetime
import io
import json
import re
from itertools import chain

Expand Down Expand Up @@ -658,11 +657,11 @@ def filters_response(self):
rendered_filters = render_to_string(
self.template_filters, self.context, request=self.request
)
return HttpResponse(json.dumps(dict(
return JsonResponse(dict(
filters=rendered_filters,
slug=self.slug,
url_root=self.url_root
)))
))

@property
@request_cache()
Expand Down
9 changes: 5 additions & 4 deletions corehq/apps/sms/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
HttpResponse,
HttpResponseBadRequest,
HttpResponseRedirect,
JsonResponse,
)
from django.shortcuts import render
from django.urls import reverse
Expand Down Expand Up @@ -765,7 +766,7 @@ def chat_contact_list(request, domain):
'iTotalDisplayRecords': filtered_records,
}

return HttpResponse(json.dumps(result))
return JsonResponse(result)


def get_contact_name_for_chat(contact, domain_obj):
Expand Down Expand Up @@ -963,7 +964,7 @@ def get(self, request, *args, **kwargs):
except Exception:
notify_exception(request, "Error updating last read message for %s" % last_sms.pk)

return HttpResponse(json.dumps(data))
return JsonResponse(data, safe=False)


class ChatLastReadMessage(View, DomainViewMixin):
Expand All @@ -989,9 +990,9 @@ def get(self, request, *args, **kwargs):

if lrm:
lrm_timestamp = json_format_datetime(lrm.message_timestamp)
return HttpResponse(json.dumps({
return JsonResponse({
'message_timestamp': lrm_timestamp,
}))
})


class DomainSmsGatewayListView(CRUDPaginatedViewMixin, BaseMessagingSectionView):
Expand Down
Loading

0 comments on commit 88b064e

Please sign in to comment.