def __init__(self, user_name, email, password, default_loc, contact_pref): self.user_name = user_name self.email = email.lower() self.set_password(password) self.contact_pref = contact_pref # Create the Well Known Text format of the location coordinate and the search polygon default_loc_wkt = "POINT(%s)" % default_loc polygon_buffer = SPoint(float(default_loc.split()[0]), float(default_loc.split()[1])).buffer(buffer_from_miles(1)) default_loc_polygon_wkt = polygon_buffer.wkt self.default_search_polygon = WKTSpatialElement(default_loc_polygon_wkt) self.default_search_location = WKTSpatialElement(default_loc_wkt)
def post(self): # Required parameters pet_name = request.json.get("pet_name") age = request.json.get("age") approach = request.json.get("approach") last_seen_loc = request.json.get("last_seen_loc") status = request.json.get("status") last_seen_city = request.json.get("last_seen_city") last_seen_state = request.json.get("last_seen_state") search_range = request.json.get("search_range") if not pet_name or not age or not approach or not last_seen_loc or not status \ or not last_seen_city or not last_seen_state or not search_range: abort(400, message="Bad parameters") # Get the current user user = g.user user_id = user.id # Create the Well Known Text format of the location coordinate and the search polygon last_seen_wkt = "POINT(%s)" % last_seen_loc polygon_buffer = Point(float(last_seen_loc.split()[0]), float(last_seen_loc.split()[1])).buffer(buffer_from_miles(1)) last_seen_polygon_wkt = polygon_buffer.wkt new_pet = Pet(pet_name, WKTSpatialElement(last_seen_wkt), str(datetime.now()), WKTSpatialElement(last_seen_polygon_wkt), user_id, age, approach, status, search_range, last_seen_city, last_seen_state) # Optional parameters if request.json.get("vet"): new_pet.vet = request.json.get("vet") if request.json.get("personality"): new_pet.personality = request.json.get("personality") if request.json.get("fav_food"): new_pet.fav_food = request.json.get("fav_food") if request.json.get("known_words"): new_pet.known_words = request.json.get("known_words") if request.json.get("house_broken"): new_pet.house_broken = request.json.get("house_broken") if request.json.get("description"): new_pet.description = request.json.get("description") db_session.add(new_pet) db_session.commit() return jsonify(new_pet.serialize)
def get(self): if request.args.get('current_loc') and request.args.get('search_range'): current_loc = request.args.get('current_loc') try: search_range = float(request.args.get('search_range')) except Exception, e: abort(400, message="Bad search range parameter") # If the user wants pets within their search range, geo-query the database # for all pets whose last_seen_polygons are within the user's current search range current_loc_wkt = "POINT(%s)" % current_loc current_loc_float_tuple = (float(current_loc.split()[0]), float(current_loc.split()[1])) search_polygon = Point(current_loc_float_tuple[0], current_loc_float_tuple[1]).buffer(buffer_from_miles(search_range)) search_polygon_wkt = search_polygon.wkt # Now that we have our search range polygon, we need to query pets and return pets # whose last_seen_polygons overlap with our search_range polygon pets = Pet.query.filter(Pet.search_polygon.intersects(search_polygon_wkt)).all() return jsonify(pets_in_range=[i.serialize for i in pets])