import json

from django.conf import settings
from django.views.decorators.http import require_http_methods
from django.http import HttpResponse, HttpResponseServerError

from healthcheck.healthcheck import (
    DjangoDBsHealthCheck, FilesDontExistHealthCheck, HealthChecker)


@require_http_methods(['GET'])
def status(request):
    checks = []

    if getattr(settings, 'STATUS_CHECK_DBS', True):
        checks.append(DjangoDBsHealthCheck())

    files_to_check = getattr(
        settings, 'STATUS_CHECK_FILES')
    if files_to_check:
        checks.append(
            FilesDontExistHealthCheck(
                files_to_check, check_id="quiesce file doesn't exist"))

    ok, details = HealthChecker(checks)()

    if not ok:
        return HttpResponseServerError((json.dumps(details)))

    return HttpResponse(json.dumps(details))

import json

from django.conf import settings
from django.views.decorators.http import require_http_methods
from django.http import HttpResponse

from healthcheck.healthcheck import (
    DjangoDBsHealthCheck, FilesDontExistHealthCheck, HealthChecker)


class JsonResponse(HttpResponse):
    def __init__(self, data, **kwargs):
        kwargs.setdefault('content_type', 'application/json')
        data = json.dumps(data)
        super(JsonResponse, self).__init__(content=data, **kwargs)


class JsonResponseServerError(JsonResponse):
    status_code = 500


@require_http_methods(['GET'])
def status(request):
    checks = []

    if getattr(settings, 'STATUS_CHECK_DBS', True):
        checks.append(DjangoDBsHealthCheck())

    files_to_check = getattr(settings, 'STATUS_CHECK_FILES')
    if files_to_check:
        checks.append(FilesDontExistHealthCheck(
            files_to_check, check_id="quiesce file doesn't exist"))

    ok, details = HealthChecker(checks)()

    if not ok:
        return JsonResponseServerError(json.dumps(details))

    return JsonResponse(details)

Fix content_type for JSON responses
Fix content_type for JSON responses

  import json
  
  from django.conf import settings
  from django.views.decorators.http import require_http_methods
- from django.http import HttpResponse, HttpResponseServerError
+ from django.http import HttpResponse
  
  from healthcheck.healthcheck import (
      DjangoDBsHealthCheck, FilesDontExistHealthCheck, HealthChecker)
+ 
+ 
+ class JsonResponse(HttpResponse):
+     def __init__(self, data, **kwargs):
+         kwargs.setdefault('content_type', 'application/json')
+         data = json.dumps(data)
+         super(JsonResponse, self).__init__(content=data, **kwargs)
+ 
+ 
+ class JsonResponseServerError(JsonResponse):
+     status_code = 500
  
  
  @require_http_methods(['GET'])
  def status(request):
      checks = []
  
      if getattr(settings, 'STATUS_CHECK_DBS', True):
          checks.append(DjangoDBsHealthCheck())
  
+     files_to_check = getattr(settings, 'STATUS_CHECK_FILES')
-     files_to_check = getattr(
-         settings, 'STATUS_CHECK_FILES')
      if files_to_check:
-         checks.append(
-             FilesDontExistHealthCheck(
+         checks.append(FilesDontExistHealthCheck(
-                 files_to_check, check_id="quiesce file doesn't exist"))
+             files_to_check, check_id="quiesce file doesn't exist"))
  
      ok, details = HealthChecker(checks)()
  
      if not ok:
-         return HttpResponseServerError((json.dumps(details)))
+         return JsonResponseServerError(json.dumps(details))
  
-     return HttpResponse(json.dumps(details))
+     return JsonResponse(details)
  
Fix content_type for JSON responses
## Code Before:
import json

from django.conf import settings
from django.views.decorators.http import require_http_methods
from django.http import HttpResponse, HttpResponseServerError

from healthcheck.healthcheck import (
    DjangoDBsHealthCheck, FilesDontExistHealthCheck, HealthChecker)


@require_http_methods(['GET'])
def status(request):
    checks = []

    if getattr(settings, 'STATUS_CHECK_DBS', True):
        checks.append(DjangoDBsHealthCheck())

    files_to_check = getattr(
        settings, 'STATUS_CHECK_FILES')
    if files_to_check:
        checks.append(
            FilesDontExistHealthCheck(
                files_to_check, check_id="quiesce file doesn't exist"))

    ok, details = HealthChecker(checks)()

    if not ok:
        return HttpResponseServerError((json.dumps(details)))

    return HttpResponse(json.dumps(details))

## Instruction:
Fix content_type for JSON responses
## Code After:
import json

from django.conf import settings
from django.views.decorators.http import require_http_methods
from django.http import HttpResponse

from healthcheck.healthcheck import (
    DjangoDBsHealthCheck, FilesDontExistHealthCheck, HealthChecker)


class JsonResponse(HttpResponse):
    def __init__(self, data, **kwargs):
        kwargs.setdefault('content_type', 'application/json')
        data = json.dumps(data)
        super(JsonResponse, self).__init__(content=data, **kwargs)


class JsonResponseServerError(JsonResponse):
    status_code = 500


@require_http_methods(['GET'])
def status(request):
    checks = []

    if getattr(settings, 'STATUS_CHECK_DBS', True):
        checks.append(DjangoDBsHealthCheck())

    files_to_check = getattr(settings, 'STATUS_CHECK_FILES')
    if files_to_check:
        checks.append(FilesDontExistHealthCheck(
            files_to_check, check_id="quiesce file doesn't exist"))

    ok, details = HealthChecker(checks)()

    if not ok:
        return JsonResponseServerError(json.dumps(details))

    return JsonResponse(details)
