def add_place(name, phone, lat, lon, address, fs_id=None, yelp_id=None, wifi=None, plugs=None, exp=None): # ensure it has a fs_id or a yelp_id if not (fs_id or yelp_id): return ErrorResponse.POST_INVALID_ARGS_FS_ID_YELP_ID # Create a review dict review = make_review_dict(wifi, plugs, exp) # Check if place already exists in the database if fs_id: spec = {Place.A_FS_ID: fs_id} place_dict = Place.mdbc().find_one(spec) # If place exists add a review to it if place_dict: place_id = place_dict.get(Place.A_FS_ID) return Place.review_place(place_id=place_id, review=review) # If place doesn't already exist in the database add it place_data = { Place.A_NAME: name, Place.A_FS_ID: fs_id, Place.A_YELP_ID: yelp_id, Place.A_ADDRESS: address, Place.A_LOCATION: [float(lat), float(lon)] } # Only add phone if it's not null if phone: place_data[Place.A_PHONE] = phone return Place.add_place(place_data=place_data, review=review)
def get_places_from_db(lat=None, lon=None, x_1=None, x_2=None, y_1=None, y_2=None, filter_by_wifi=MUST_HAVE_WIFI, filter_by_plugs=MUST_HAVE_PLUGS): if not (lat and lon) and not (x_1 and x_2 and y_1 and y_2): return ErrorResponse.GET_INVALID_ARGS if (lat and lon): places = Place.get_places(lat=lat, lon=lon) if (x_1 and x_2 and y_1 and y_2): box = [[x_1, y_1], [x_2, y_2]] places = Place.get_places(box=box) places_output = process_places_from_db(places, filter_by_wifi=filter_by_wifi, filter_by_plugs=filter_by_plugs) return places_output
def get_place_by_field(self, field_name, field_value): if field_name not in [FIELD_NAME, FIELD_PLACE_ID]: raise ValueError('incorrect field name') cursor = self.db.cursor() sql = f'SELECT place_id, name, category, location FROM places WHERE {field_name} = %s' val = (field_value,) cursor.execute(sql, val) el = cursor.fetchone() if el is None: raise MysqlPlaceDaoError return Place(id=el[0], name=el[1], category=el[2], location=el[3])
def list_places(self): cursor = self.db.cursor() sql = 'SELECT place_id, name, category, location from places ORDER BY name ASC' cursor.execute(sql) reslist = cursor.fetchall() return [Place(id=el[0], name=el[1], category=el[2], location=el[3]) for el in reslist or []]
def review_place(place_id, wifi=None, plugs=None, exp=None): review = make_review_dict(wifi, plugs, exp) return Place.review_place(place_id=place_id, review=review)
def test_post(): if not Place.mdbc().count() == 0: Place.mdbc().remove() # Add new place url = "http://localhost/places" payload = { "fs_id": "aslfkj13409182491k1we1-12e801", "name": "Ninth Street Espresso", "phone": "2129201880", "lat": "42.881111", "lon": "78.191111", "address": "180 Crosby Street", "wifi": "2", "plugs": "1", } r = requests.post(url=url, data=payload) spec = r.content spec_js = json.loads(spec) place_id = spec_js.get("place_id") # Add another review to previous place payload = {"place_id": place_id, "wifi": "0", "plugs": "0"} r = requests.post(url=url, data=payload) # Add another review to previous place payload = {"place_id": place_id, "wifi": "0", "plugs": "0"} r = requests.post(url=url, data=payload) # Add new place payload = { "fs_id": "12e801", "name": "The Bean", "phone": "2129201881", "lat": "42.881111", "lon": "78.191111", "address": "180 Crosby Street", "wifi": "2", "plugs": "1", } r = requests.post(url=url, data=payload) r.content # Add new place payload = { "fs_id": "12801", "name": "The Native Bean", "phone": "2129202880", "lat": "42.881111", "lon": "78.191111", "address": "180 Crosby Street", "wifi": "1", "plugs": "1", } r = requests.post(url=url, data=payload) r.content # Add new place payload = { "fs_id": "12801", "name": "Gimme Coffee", "phone": "2129201980", "lat": "42.881111", "lon": "78.191111", "address": "180 Crosby Street", "wifi": "0", "plugs": "1", } r = requests.post(url=url, data=payload) r.content # Add new place payload = { "fs_id": "12801", "name": "Gimme Coffee Now", "phone": "3129201980", "lat": "43.881111", "lon": "79.191111", "address": "180 Crosby Street", "wifi": "0", "plugs": "1", } r = requests.post(url=url, data=payload) r.content print "Database_count = " + str(Place.mdbc().count())