def testGetBestLocationUtil(self): location_string = 'Bloomington, IN, USA' best = get_best_location(location_string) self.assertEqual(best, (u'Bloomington, IN, USA', (39.164287000000002, -86.526904000000002))) location_string = '' try: best = get_best_location(location_string) self.fail('the empty string as the location should return a CouldNotFindLocation error') except CouldNotFindLocation: #this is expected pass
def testGetBestLocationUtil(self): location_string = 'Bloomington, IN, USA' best = get_best_location(location_string) EXPECTED_BEST = (u'Bloomington, IN, USA', (39.164287000000002, -86.526904000000002)) self.assertTrue(best[0] == EXPECTED_BEST[0]) self.assertTrue(reduce(lambda p, q: p and q, [closeEnough(x1, x2) for x1, x2 in zip(best[1], EXPECTED_BEST[1])])) location_string = '' try: best = get_best_location(location_string) self.fail('The empty string as the location should return a CouldNotFindLocation error') except GeocoderResultError: #this is expected pass
def geoloc_get_best_location(request): responseData = {} # Get the location string out of the post data, or send back a failure message try: location_string = request.POST["location_string"] except: responseData["failure"] = "No location_string given" json = simplejson.dumps(responseData) return HttpResponse(json, mimetype="application/json") # Get the best location and return it and a success message or return a failure try: # TODO: does the location_string need to be cleaned somehow? best_location = get_best_location(location_string) lng = str(best_location[1][1]) lat = str(best_location[1][0]) canonical_name = best_location[0] responseData["lng"] = lng responseData["lat"] = lat responseData["canonical_name"] = canonical_name responseData["success"] = "A marker was added for %s" % canonical_name json = simplejson.dumps(responseData) return HttpResponse(json, mimetype="application/json") except CouldNotFindLocation: responseData["failure"] = "%s could not be resolved to a location" % location_string json = simplejson.dumps(responseData) return HttpResponse(json, mimetype="application/json")
def geoloc_get_best_location(request): responseData = {} if 'location_string' not in request.POST: responseData['failure'] = "No location_string given" json = simplejson.dumps(responseData) return HttpResponse(json, mimetype='application/json') location_string = request.POST['location_string'] # Get the best location and return it and a success message or return a failure try: # TODO: does the location_string need to be cleaned somehow? canonical_name, (lat, lng) = get_best_location(location_string) responseData['lng'] = lng responseData['lat'] = lat responseData['canonical_name'] = canonical_name responseData['success'] = 'A marker was added for %s' % canonical_name json = simplejson.dumps(responseData) return HttpResponse(json, mimetype='application/json') except (GeocoderResultError, CouldNotFindLocation): responseData['failure'] = "'%s' could not be resolved to a location" % location_string json = simplejson.dumps(responseData) return HttpResponse(json, mimetype='application/json') except GeocoderError: # TODO This represents a library problem and should be logged. responseData['failure'] = "Problem resolving '%s' to a location" % location_string json = simplejson.dumps(responseData) return HttpResponse(json, mimetype='application/json') except Exception, e: # TODO This represents a library error and should be logged. responseData['failure'] = "Error resolving '%s' to a location" % location_string json = simplejson.dumps(responseData) return HttpResponse(json, mimetype='application/json')