def search_city(): if 'username' not in session or 'password' not in session: return redirect(url_for('signin')) try: city = request.form.get('search_box') preferences = dict() preferences['time_spent'] = 2 places_list = ItineraryService.generate_tourist_spot_list(city, preferences=preferences) tourist_spot_list = [] all_spots = [] for spot in places_list: ts = TouristSpot(place=spot) query = ExploriumDbApi.TOURIST_SPOT_TABLE.select().where(and_(ExploriumDbApi.TOURIST_SPOT_TABLE.c.google_id == ts.destinationID, ExploriumDbApi.TOURIST_SPOT_TABLE.c.name == ts.destinationName)) result = ExploriumDbApi.conn.execute(query).fetchone() if result is None: ts_id = ExploriumDbApi.save_tourist_spot(ts) ts.db_id = ts_id else: ts.db_id = result['tourist_spot_id'] tourist_spot_list.append(ts) for t_spot in tourist_spot_list: all_spots.append(t_spot.to_dict()) all_spots = json.dumps(all_spots) return render_template('index.html', this_username=session['username'], show_what="Search Results: " + city, result_info_list=all_spots) except Exception as e: return render_template('index.html', this_username=session['username'], show_what="Welcome!", result_info_list='')
def test_generate_tourist_spot_list(self): spot_list = ItineraryService.generate_tourist_spot_list("New York") for spot in spot_list: tourist_spot = spot self.assertNotIn(b'dasd', spot_list) assert spot['rating'] <= 5 assert spot['rating'] >= 0 assert spot['geometry']['location']['lat'] > 40 assert spot['geometry']['location']['lat'] < 41
def test_generate_tourist_spot_list_pref1_valid_city(self): prefs = dict() prefs['types'] = [] prefs['min_price'] = 0 prefs['max_price'] = 4 prefs['time_spent'] = 2 l = ItineraryService.generate_tourist_spot_list(city='New York', preferences=prefs) self.assertNotEqual(l, None) self.assertGreater(len(l), 0)
def estimate_daily_route_times(self, day): for index, spot in enumerate(self.touristSpots[day]): if index + 1 < len(self.touristSpots[day]): if spot.address and self.touristSpots[day][index + 1].address: #print ItineraryService.get_travel_duration(spot.address, self.touristSpots[day][index+1].address) time = ItineraryService.get_travel_duration( spot.address, self.touristSpots[day][index + 1].address) if time and 'text' in time: spot.next_dest_time = time['text']
def __init__(self, city, preferences=None, db_id=None, itinerary=None): self.city = city self.db_id = None self.touristSpots = None self.days = None self.comments = None if db_id: self.db_id = db_id self.touristSpots = ExploriumDbApi.load_itinerary(db_id) self.days = len(self.touristSpots) self.comments = ExploriumDbApi.get_itinerary_comments(db_id) elif itinerary: self.touristSpots = [] for day in itinerary['tourist_spots']: day_num = [] for t_spot in day: day_num.append(TouristSpot(t_spot=t_spot)) self.touristSpots.append(day_num) self.days = len(self.touristSpots) self.comments = itinerary.get('comments', None) else: self.touristSpots = [] if preferences: self.days = preferences.get('time_spent', 1) else: self.days = 1 raw_tourist_spots = ItineraryService.generate_tourist_spot_list( city, preferences) tourist_spot_list = [] for spot in raw_tourist_spots: ts = TouristSpot(place=spot) query = ExploriumDbApi.TOURIST_SPOT_TABLE.select().where( and_( ExploriumDbApi.TOURIST_SPOT_TABLE.c.google_id == ts.destinationID, ExploriumDbApi.TOURIST_SPOT_TABLE.c.name == ts.destinationName)) result = ExploriumDbApi.conn.execute(query).fetchone() if result is None: ts_id = ExploriumDbApi.save_tourist_spot(ts) ts.db_id = ts_id else: ts.db_id = result['tourist_spot_id'] tourist_spot_list.append(ts) #print tourist_spot_list self.recalibrate_itinerary(tourist_spot_list)
def test_init_TouristSpot(self): place = ItineraryService.generate_tourist_spot_list(city='New York', preferences=None)[0] ts = TouristSpot(place) self.assertEqual(ts.db_id , None) self.assertEqual(ts.destinationID , place["place_id"]) self.assertEqual(ts.destinationName , place["name"]) self.assertEqual(ts.open_time , None) self.assertEqual(ts.close_time , None) self.assertEqual(ts.geocode , place["geometry"]["location"]) self.assertEqual(ts.image_link , place.get('photos', None)[0]['photo_reference']) self.assertEqual(ts.introduction , None) self.assertEqual(ts.rating , place.get("rating", None)) self.assertEqual(ts.next_dest_time , None)
def test_get_tourist_spot_details(self): ts = TouristSpot() dets = ItineraryService.get_tourist_spot_details(ts) self.assertNotEqual(dets, None)
def test_generate_tourist_spot_list_no_pref_valid_city(self): l = ItineraryService.generate_tourist_spot_list(city='New York', preferences=None) self.assertNotEqual(l, None) self.assertGreater(len(l), 0)
def test_get_travel_duration_valid(self): coord1 = (40.80887209540821, -73.96184921264648) # Columbia University Long Lat coord2 = (40.73083612189599, -73.99734020233154) # NYU Long Lat dur = ItineraryService.get_travel_duration(coord1, coord2) self.assertNotEqual(dur, None)