Exemple #1
0
def lookup(request):
    """ Simple view to return a JSON array with content data via a
    GET or POST request """

    if request.POST:
        lat_long, radius, fields = get_params(request)
    elif request.GET:
        lat_long, radius, fields = get_params(request, 'GET')
    else:
        return HttpResponse(json.dumps({'error': 'BadRequest'}))

    if lat_long and radius and fields:
        z = GeoEntry.within_radius(lat_long, radius, fields)
    elif lat_long and radius:
        z = GeoEntry.within_radius(lat_long, radius)
    elif lat_long:
        z = GeoEntry.within_radius(lat_long)
    else:
        return HttpResponse(json.dumps({'error': 'NoRequest'}))

    return HttpResponse(json.dumps(z, sort_keys=False, indent=4))
        try:
            primary_city = City.objects.get(pk=city['city__pk'], country=country)
            primary_city.primary_city = primary_city
            primary_city.save()
        except:
            "Could not find city matching %d and country id %d" % (city['city__pk'], country.pk)
            continue

        try:
            city_geoentry = GeoEntry.objects.get(content_type__pk=CTYPES['city'],
                object_id= city['city__pk'])
        except GeoEntry.DoesNotExist:
            print "No geoentry for %s" % city['city__name']
            continue

        radial_results = GeoEntry.within_radius(
            [city_geoentry.latitude, city_geoentry.longitude],
            BOUNDRY_RADIUS
        )

        # Trim off our source point, reverse and update our source cities
        # (we need to reverse it to have it assign by most populated last)
        for point in radial_results[1:][::-1]:
            if point['content_type'] == CTYPES['city']:
                source_city = City.objects.get(pk=point['object_id'])
                source_city.primary_city = primary_city
                source_city.save()
                print "Set %s as primary city for %s" % (primary_city, source_city)