def load_sample_trails(): """Create sample trail instances""" trail1 = Trail(trail_id=1234, trail_name="Sample trail") trail2 = Trail(trail_id=12345, trail_name="Sample trail 2") db.session.add_all([trail1, trail2]) db.session.commit()
def create_trail(name, area_name, city_name, state_name, country_name, _geoloc, popularity, length, elevation_gain, difficulty_rating, route_type, visitor_usage, avg_rating, num_reviews, features, activities, units): """Create and return a new trail.""" trail = Trail(name=name, area_name=area_name, city_name=city_name, state_name=state_name, country_name=country_name, _geoloc=_geoloc, popularity=popularity, length=length, elevation_gain=elevation_gain, difficulty_rating=difficulty_rating, route_type=route_type, visitor_usage=visitor_usage, features=features, activities=activities, units=units) db.session.add(trail) db.session.commit() return trail
def create_trail(latitude, longitude, name, length=None, location=None, image=None, hiking_id=None): """Create and return a new trail.""" trail = Trail.query.filter((Trail.name == name) & (Trail.latitude == latitude) & (Trail.longitude == longitude)).first() if trail is not None: return trail else: trail = Trail(latitude=latitude, longitude=longitude, name=name, length=length, location=location, image=image, hiking_id=hiking_id) db.session.add(trail) db.session.commit() return trail
def record_ratings(): trail_id = request.form.get("trailId") user_id = session["user_id"]["user_id"] rating = request.form.get("rating") trail_name = request.form.get("trailName") trail = Trail.query.get(trail_id) if not trail: new_trail = Trail(trail_id=trail_id, trail_name=trail_name) db.session.add(new_trail) db.session.commit() existing_rating = Rating.query.filter_by(trail_id=trail_id, user_id=user_id).first() if existing_rating: existing_rating.rating = rating response = "Thank you, your rating has been updated to %s stars." % ( rating) else: new_rating = Rating(trail_id=trail_id, user_id=user_id, rating=rating) db.session.add(new_rating) response = "Thank you, we have recorded your rating of %s stars." % ( rating) db.session.commit() return jsonify(response)
def trail(): trail = Trail(latitude=45, longitude=-122, name='New Trail', length=30, location='Vancouver, WA', image='trail.jpg', hiking_id='1234') return trail
def add_hike(): """Checks if the selected trail to hike: - is in the db (if not, adds) - has most recent conditions (if not, updates) and creates a new hike on the trail with is_complete as false""" api_trail_id = request.form.get("apiId") user = User.query.filter_by(user_id=session["current_user"]).first() payload = {"key": key, "ids": api_trail_id} r_trail = requests.get( "https://www.hikingproject.com/data/get-trails-by-id", params=payload) if r_trail.json()['success'] == 0: return 'Failed to add hike, try another trail or try again later.' trail_obj = r_trail.json()['trails'][0] trail_in_db = Trail.query.filter_by(api_trail_id=api_trail_id).first() if trail_in_db: current_status_at = trail_in_db.status_at if not current_status_at == trail_obj["conditionDate"]: trail_in_db.status = trail_obj['conditionStatus'] trail_in_db.status_details = trail_obj['conditionDetails'] trail_in_db.status_at = trail_obj['conditionDate'] db.session.commit() else: trail = Trail(api_trail_id=trail_obj['id'], trail_name=trail_obj['name'], description=trail_obj['summary'], difficulty=trail_obj['difficulty'], distance_in_miles=trail_obj['length'], total_ascent=trail_obj['ascent'], total_descent=trail_obj['descent'], location=trail_obj['location'], latitude=trail_obj['latitude'], longitude=trail_obj['longitude'], api_rating=trail_obj['stars'], status=trail_obj['conditionStatus'], status_details=trail_obj['conditionDetails'], status_at=trail_obj['conditionDate']) db.session.add(trail) db.session.commit() trail_in_db = Trail.query.filter_by(api_trail_id=api_trail_id).first() user_id = session.get("current_user", None) if user_id: hike_to_do = Hike.query.filter( (Hike.user_id == user_id) & (Hike.trail_id == trail_in_db.trail_id) & (Hike.is_complete == False) & (Hike.canceled_by_user == False)).all() if hike_to_do: return 'This trail is already in your hikes to complete.' hike = Hike(user_id=user.user_id, trail_id=trail_in_db.trail_id, is_complete=False, canceled_by_user=False) db.session.add(hike) db.session.commit() return 'Added hike on this trail!'
def create_trail(name, desc, lat, long, gps, length, ascent, descent, difficulty, location, url, img): """Create and return a new trail.""" trail = Trail(name = name, desc = desc, long = long, lat = lat, gps = gps, length = length, ascent = ascent, descent = descent, difficulty = difficulty, location = location, url = url, img = img) db.session.add(trail) db.session.commit() return trail
def load_test_data(): t_user = User(username='******', email='*****@*****.**', password='******') db.session.add(t_user) t_trail = Trail(latitude=45, longitude=-122, name='New Trail', length=30, location='Vancouver, WA', image='trail.jpg', hiking_id='1234') db.session.add(t_trail) t_restaurant = Restaurant(latitude=45, longitude=-122, name='New Restaurant', price=4, location='Test, Vancouver, WA', phone='1234567890', image='rest.jpg', yelp_id='1234') db.session.add(t_restaurant) t_walk = Walk(user=t_user, walk_date=datetime.datetime(2020, 5, 17)) db.session.add(t_walk) t_rest_rating = RestRating(restaurant=t_restaurant, user=t_user, rest_comment='Wow, what a restaurant.', rest_star=5, masks_worn=True, socially_distanced=True, outdoor_seating=True) db.session.add(t_rest_rating) t_trail_rating = TrailRating(trail=t_trail, user=t_user, trail_comment='Wow, what a trail.', trail_star=5, difficulty_level=3, crowded=False) db.session.add(t_trail_rating) db.session.commit()
def find_and_add_trail(): """Get Trail Data by making a call to API and add one Trail to Trail Table - from the returned data""" r = requests.get( "https://www.hikingproject.com/data/get-trails?lat=37.3382&lon=-121.8863&maxDistance=10&key=hiking_consumer_key" ) trail = r.json() num_results = trail['trails'] url = num_results[2]['url'] name = num_results[2]['name'] trail_id = 7024835 new_trail = Trail(trail_id=trail_id, name=name, url=url) db.session.add(new_trail) db.session.commit()
def get_trail_info_add_to_db(): """Get Trail info from front end when user selects and stores in DB""" trail_id = request.form.get("trail_id") date = request.form.get("date") user_id = session['id'] name = request.form.get("name") url = request.form.get("url") length = request.form.get("length") trail_type = request.form.get("type") rating = request.form.get("stars") #Add this record in Trail, UserTrails and Hike Table #Need to check if same trail with same date exists in Hike Table already then flash the message #to the user - You have already added this Hike! #Need to check if this record already exists in Trails then don't add it in Trails query = db.session.query(Trail) users_trails = query.filter(Trail.trail_id == trail_id).all() if not users_trails: trail = Trail(trail_id=trail_id, name=name, url=url, length=length, trail_type=trail_type) db.session.add(trail) db.session.commit() usertrails = UserTrail(trail_id=trail_id, user_id=user_id, rating=rating) db.session.add(usertrails) db.session.commit() usertrail_id = usertrails.usertrail_id parsed_date = dateutil.parser.parse(date).date() print parsed_date hike = Hike(usertrail_id=usertrail_id, date=parsed_date) db.session.add(hike) db.session.commit() flash("you just added " + name + " to your hike list!") return jsonify({"trail_id": trail_id})
def save_fav_trails(): img = request.form.get("trailImage") trailDir = request.form.get("trailDirections") trailUrl = request.form.get("trailUrl") trailName = request.form.get("trailName") trail = Trail(trail_name=trailName, trail_image=img, trail_google_direction=trailDir, trail_yelp_link=trailUrl) db.session.add(trail) db.session.commit() fav = Favorites(user_id=session["user_id"], trail_id=trail.trail_id) db.session.add(fav) db.session.commit() return "1"
def seed_trails_into_db(api_response): """Take Hiking Project API response and seed trail data into database if trail does not already exist""" for trail in api_response["trails"]: trail_id = trail["id"] trail_name = trail["name"] length = trail["length"] difficulty = convert_trail_difficulty(trail["difficulty"]) img_thumb_url = trail["imgSmall"] img_lg_url = trail["imgSmallMed"] long = trail["longitude"] lat = trail["latitude"] location = trail["location"].split(",") city = location[0] state = location[1][1:] description = trail["summary"] ascent = trail["ascent"] descent = trail["descent"] high_altitude = trail["high"] low_altitude = trail["low"] if not Trail.query.filter_by(trail_id=trail_id).all(): new_trail = Trail(trail_id=trail_id, trail_name=trail_name, length=length, difficulty=difficulty, img_thumb_url=img_thumb_url, img_lg_url=img_lg_url, long=long, lat=lat, city=city, state=state, description=description, ascent=ascent, descent=descent, high_altitude=high_altitude, low_altitude=low_altitude) db.session.add(new_trail) db.session.commit()
def load_trails_and_status(): """Load trail data obtained from an API request from trails.csv""" trail_json = json.load(open("seed_data/trails_94703.json")) Trail.query.delete() for trail_obj in trail_json['trails']: trail = Trail(api_trail_id=trail_obj['id'], trail_name=trail_obj['name'], description=trail_obj['summary'], difficulty=trail_obj['difficulty'], distance_in_miles=trail_obj['length'], total_ascent=trail_obj['ascent'], total_descent=trail_obj['descent'], location=trail_obj['location'], latitude=trail_obj['latitude'], longitude=trail_obj['longitude'], api_rating=trail_obj['stars'], status=trail_obj['conditionStatus'], status_details=trail_obj['conditionDetails'], status_at=trail_obj['conditionDate']) db.session.add(trail) db.session.commit()
def record_favorites(): trail_id = request.form.get("trailId") user_id = session["user_id"]["user_id"] trail_name = request.form.get("trailName") tag_id = request.form.get("id") trail = Trail.query.get(trail_id) if not trail: new_trail = Trail(trail_id=trail_id, trail_name=trail_name) db.session.add(new_trail) db.session.commit() existing_favorite = Favorite.query.filter_by(trail_id=trail_id, user_id=user_id).first() if existing_favorite: db.session.delete(existing_favorite) response = { 'trailName': '%s has been removed from your Favorites list.' % (trail_name), 'id': tag_id } else: new_favorite = Favorite(trail_id=trail_id, user_id=user_id) db.session.add(new_favorite) response = { 'trailName': '%s has been added to your Favorites list.' % (trail_name), 'id': tag_id } db.session.commit() return jsonify(response)
def load_trails(): """ load trails and images of trails """ for row in open("data/sftrails.csv"): row = row.rstrip() name, city, address, distance, trail_type, latitude, longitude, image_url = row.split( ",")[1:] trail = Trail(name=name, city_id=db.session.query(City).filter( City.name == city).one().city_id, address=address, distance=float(distance), trail_type=trail_type, latitude=float(latitude), longitude=float(longitude), image_url=image_url) try: trail = db.session.query(Trail).filter(Trail.name == name).one() except NoResultFound: db.session.add(trail) db.session.commit()