@locations_access_required
def child_locations_for_select2(request, domain):
    id = request.GET.get('id')
    ids = request.GET.get('ids')
    query = request.GET.get('name', '').lower()
    user = request.couch_user

    def loc_to_payload(loc):
        return {'id': loc.location_id, 'name': loc.display_name}

    if id:
        try:
            loc = SQLLocation.objects.get(location_id=id)
            if loc.domain != domain:
                raise SQLLocation.DoesNotExist()
        except SQLLocation.DoesNotExist:
            return json_response(
                {'message': 'no location with id %s found' % id},
                status_code=404,
            )
        else:
            return json_response(loc_to_payload(loc))
    elif ids:
        from corehq.apps.locations.util import get_locations_from_ids
        ids = json.loads(ids)
        try:
            locations = get_locations_from_ids(ids, domain)
        except SQLLocation.DoesNotExist:
            return json_response(
                {'message': 'one or more locations not found'},
                status_code=404,
            )
        return json_response([loc_to_payload(loc) for loc in locations])
    else:
        locs = []
        user_loc = user.get_sql_location(domain)

        if user_can_edit_any_location(user, request.project):
            locs = SQLLocation.objects.filter(domain=domain, is_archived=False)
        elif user_loc:
            locs = user_loc.get_descendants(include_self=True)

        if locs != [] and query:
            locs = locs.filter(name__icontains=query)

        return json_response(map(loc_to_payload, locs[:10]))
dataset/ETHPy150Open dimagi/commcare-hq/corehq/apps/locations/views.py/child_locations_for_select2/original