def _from_googlemaps(self, **params): """ Use google maps API to lookup a location. This is intended to be a fallback for geolite look ups """ assumed_address = geolocation.get_best_guess_address(**params) google_maps_entry = geolocation.get_entry(assumed_address) if not google_maps_entry: return lat = float(google_maps_entry["results"][0]["geometry"]["location"]["lat"]) lon = float(google_maps_entry["results"][0]["geometry"]["location"]["lng"]) components = google_maps_entry["results"][0]["address_components"] city = components[0]["long_name"].lower() state = None if len(components) > 2: for comp in components: if "administrative_area_level_1" in comp: state = comp["short_name"].lower() country = components[len(components) - 1]["long_name"].lower() return { "city": params.get("city").lower(), "state": state, "country": country, "loc": { "lat": lat, "lon": lon }, "postal_code": None, "area_code": None }
def put(listing, request): """ Updates listing with same parameters as creating one """ params = request.params #Get contact info to update as well contact_info = request.db["contactInfo"].find_one({"_id": ObjectId(listing.data["contact"]["_id"])}) print #Extract parameters, all of them options, updating along the way if "title" in params: listing.data["title"] = params["title"] if "description" in params: listing.data["description"] = params["description"] if "image" in params: listing.data["image"] = params["image"] #Now do an update for contact information. Will need to call out to google maps if "phone_number" in params: contact_info["phone_number"] = params["phone_number"] if "email" in params: contact_info["email"] = params["email"] name = params.get("place_name") if name: contact_info["name"] = name country = params.get("country") if country: contact_info["country"] = country state = params.get("state") if state: contact_info["state"] = state address = params.get("address") if address: contact_info["address"] = address zipcode = params.get("zipcode") if zipcode: contact_info["zipcode"] = zipcode city = params.get("city") if city: contact_info["city"] = city lat, lon = params.get("lat"), params.get("lon") try: lat, lon = float(lat), float(lon) except: lat, lon = None, None if not lat or not lon: assumed_address = geolocation.get_best_guess_address(country=country, city=city, address=address, state=state, zipcode=zipcode) lat, lon = geolocation.decode_to_latlon(assumed_address) request.db["contactInfo"].save(contact_info) listing.collection.save(listing.data) listing.data["contact"] = contact_info contact_info["_id"] = str(contact_info["_id"]) request.response.status = http_statuses.ACCEPTED return listing.data