def locate(request): """ Implement the "/locate" API call. """ if request.method == "GET": params = request.GET elif request.method == "POST": params = request.POST else: params = {} code,response = geolocator.geolocate(params) if code == 200: # We got a normal response -> bundle it up and return it to the caller. response = json.dumps(response) if "callback" in params: # Wrap the response in a JSONP callback function. response = params['callback'] + "(" + response + ")" return HttpResponse(response, mimetype="application/json") else: # We got an error response -> return the error back to the caller. return error_response(code, response, params.get("callback"))
def main(request): """ Implement the "/" URL. This is the main entry point for the geolocator_api_test application. """ if not request.user.is_authenticated: return HttpResponseRedirect(reverse(settings.ADMIN_HOME_VIEW)) if request.method == "GET": # We're displaying the page for the first time -> set up our default # values. req_type = "unstructured" req_text = "" req_country = "" req_state = "" req_metro = "" req_region = "" req_county = "" req_city = "" req_locality = "" req_zipCode = "" req_latitude = "" req_longitude = "" req_source = "" results = [] log = [] elif request.method == "POST": # Respond to the user submitting our form. # Extract the submitted request. req_type = request.POST['req_type'] req_text = request.POST['req_text'] req_country = request.POST['req_country'] req_state = request.POST['req_state'] req_metro = request.POST['req_metro'] req_region = request.POST['req_region'] req_county = request.POST['req_county'] req_city = request.POST['req_city'] req_locality = request.POST['req_locality'] req_zipCode = request.POST['req_zipCode'] req_latitude = request.POST['req_latitude'] req_longitude = request.POST['req_longitude'] req_source = request.POST['req_source'] # Build the request to send to the Geolocator API. params = {} if req_type == "unstructured": params['text'] = req_text elif req_type == "structured": if req_country != "": params['country'] = req_country if req_state != "": params['state'] = req_state if req_metro != "": params['metro'] = req_metro if req_region != "": params['region'] = req_region if req_county != "": params['county'] = req_county if req_city != "": params['city'] = req_city if req_locality != "": params['locality'] = req_locality if req_zipCode != "": params['zipCode'] = req_zipCode elif req_type == "coordinate": params['lat'] = req_latitude params['long'] = req_longitude if req_source != "": params['source'] = req_source params['verbose'] = "1" # Send off the request to the Geolocator API. code,response = geolocator.geolocate(params) if code != 200: # OK. log = [response] results = [] else: if "locations" not in response: results = [] log = [repr(response)] else: results = [] log = [] for key,value in sorted(response['locations'][0].items()): if key == "log": log = value else: if isinstance(value, dict): parts = [] for field in sorted(value.keys()): parts.append(field + "=" + str(value[field])) value = ", ".join(parts) results.append((key, value)) for i in range(len(log)): log[i] = log[i].replace(" ", " ") # If we get here, display our form with the supplied parameters. menu_html = menus.generate(request, "Gelocator API Test", "geolocator_api_test", "main") return render_to_response("geolocator_api_test/templates/testgeo.html", {'menu_html' : menu_html, 'req_type' : req_type, 'req_text' : req_text, 'req_country' : req_country, 'req_state' : req_state, 'req_metro' : req_metro, 'req_region' : req_region, 'req_county' : req_county, 'req_city' : req_city, 'req_locality' : req_locality, 'req_zipCode' : req_zipCode, 'req_latitude' : req_latitude, 'req_longitude' : req_longitude, 'req_source' : req_source, 'results' : results, 'log' : log, }, context_instance=RequestContext(request))