import json

from django.core.serializers.json import DjangoJSONEncoder
from django.http import HttpResponse
from django.template.response import TemplateResponse

from us_ignite.maps.models import Location


def location_list(request):
    """Shows a list of locations in a map."""
    object_list = Location.published.select_related('category').all()
    context = {
        'object_list': object_list,
    }
    return TemplateResponse(request, 'maps/object_list.html', context)


def _get_content(name, website):
    if not website:
        return name
    return u'<div><h2><a href="%s">%s</a></h2></div>' % (website, name)


def _get_location_data(location):
    return {
        'latitude': location.position.latitude,
        'longitude': location.position.longitude,
        'name': location.name,
        'website': location.website,
        'category': location.category.name,
        'image': location.get_image_url(),
        'content': _get_content(location.name, location.website),
    }


def location_list_json(request):
    """Returns the locations in JSON format"""
    object_list = Location.published.select_related('category').all()
    dict_list = [_get_location_data(l) for l in object_list]
    response = 'map.render(%s)' % json.dumps(dict_list, cls=DjangoJSONEncoder)
    return HttpResponse(response, content_type='application/javascript')

from django.template.response import TemplateResponse

from us_ignite.common.response import json_response
from us_ignite.maps.models import Location


def location_list(request):
    """Shows a list of locations in a map."""
    object_list = Location.published.select_related('category').all()
    context = {
        'object_list': object_list,
    }
    return TemplateResponse(request, 'maps/object_list.html', context)


def _get_content(name, website):
    if not website:
        return name
    return u'<div><h2><a href="%s">%s</a></h2></div>' % (website, name)


def _get_location_data(location):
    return {
        'latitude': location.position.latitude,
        'longitude': location.position.longitude,
        'name': location.name,
        'website': location.website,
        'category': location.category.name,
        'image': location.get_image_url(),
        'content': _get_content(location.name, location.website),
    }


def location_list_json(request):
    """Returns the locations in JSON format"""
    object_list = Location.published.select_related('category').all()
    dict_list = [_get_location_data(l) for l in object_list]
    return json_response(dict_list, callback='map.render')

Update ``maps`` to use the ``json_response`` function.
Update ``maps`` to use the ``json_response`` function.

- import json
- 
- from django.core.serializers.json import DjangoJSONEncoder
- from django.http import HttpResponse
  from django.template.response import TemplateResponse
  
+ from us_ignite.common.response import json_response
  from us_ignite.maps.models import Location
  
  
  def location_list(request):
      """Shows a list of locations in a map."""
      object_list = Location.published.select_related('category').all()
      context = {
          'object_list': object_list,
      }
      return TemplateResponse(request, 'maps/object_list.html', context)
  
  
  def _get_content(name, website):
      if not website:
          return name
      return u'<div><h2><a href="%s">%s</a></h2></div>' % (website, name)
  
  
  def _get_location_data(location):
      return {
          'latitude': location.position.latitude,
          'longitude': location.position.longitude,
          'name': location.name,
          'website': location.website,
          'category': location.category.name,
          'image': location.get_image_url(),
          'content': _get_content(location.name, location.website),
      }
  
  
  def location_list_json(request):
      """Returns the locations in JSON format"""
      object_list = Location.published.select_related('category').all()
      dict_list = [_get_location_data(l) for l in object_list]
+     return json_response(dict_list, callback='map.render')
-     response = 'map.render(%s)' % json.dumps(dict_list, cls=DjangoJSONEncoder)
-     return HttpResponse(response, content_type='application/javascript')
  
Update ``maps`` to use the ``json_response`` function.
## Code Before:
import json

from django.core.serializers.json import DjangoJSONEncoder
from django.http import HttpResponse
from django.template.response import TemplateResponse

from us_ignite.maps.models import Location


def location_list(request):
    """Shows a list of locations in a map."""
    object_list = Location.published.select_related('category').all()
    context = {
        'object_list': object_list,
    }
    return TemplateResponse(request, 'maps/object_list.html', context)


def _get_content(name, website):
    if not website:
        return name
    return u'<div><h2><a href="%s">%s</a></h2></div>' % (website, name)


def _get_location_data(location):
    return {
        'latitude': location.position.latitude,
        'longitude': location.position.longitude,
        'name': location.name,
        'website': location.website,
        'category': location.category.name,
        'image': location.get_image_url(),
        'content': _get_content(location.name, location.website),
    }


def location_list_json(request):
    """Returns the locations in JSON format"""
    object_list = Location.published.select_related('category').all()
    dict_list = [_get_location_data(l) for l in object_list]
    response = 'map.render(%s)' % json.dumps(dict_list, cls=DjangoJSONEncoder)
    return HttpResponse(response, content_type='application/javascript')

## Instruction:
Update ``maps`` to use the ``json_response`` function.
## Code After:
from django.template.response import TemplateResponse

from us_ignite.common.response import json_response
from us_ignite.maps.models import Location


def location_list(request):
    """Shows a list of locations in a map."""
    object_list = Location.published.select_related('category').all()
    context = {
        'object_list': object_list,
    }
    return TemplateResponse(request, 'maps/object_list.html', context)


def _get_content(name, website):
    if not website:
        return name
    return u'<div><h2><a href="%s">%s</a></h2></div>' % (website, name)


def _get_location_data(location):
    return {
        'latitude': location.position.latitude,
        'longitude': location.position.longitude,
        'name': location.name,
        'website': location.website,
        'category': location.category.name,
        'image': location.get_image_url(),
        'content': _get_content(location.name, location.website),
    }


def location_list_json(request):
    """Returns the locations in JSON format"""
    object_list = Location.published.select_related('category').all()
    dict_list = [_get_location_data(l) for l in object_list]
    return json_response(dict_list, callback='map.render')
