def remove_photo_from_trip(): '''removes a photo from the trip board for that location''' trip_id = request.form.get("trip") img_id = request.form.get("photo") trip = Trip.get_trip(trip_id) # checks if authorized user is accessing this page if session.get('login') == trip.user_id: already_exists = TripPhotoRelationship.get_trip_photo(trip_id, img_id) # photo is in current trip board, delete relationship from the database if already_exists: db.session.delete(already_exists) db.session.commit() else: return ('This photo is not in your trip board!') return 'Successfully removed' # unauthorized user, redirect to homepage else: flash('You do not have permission to access this feature') return 'Error: Photo not removed.'
def trip_details(): '''shows details and photos for each trip board''' # obtain trip_id from the request form trip_id = request.form.get("trip") # get trip from the database using trip_id trip = Trip.get_trip(trip_id) photos = trip.photos user = session.get('login') return render_template('tripdetails.html', user=user, trip=trip, photos=photos)
def add_photo_to_trip(): '''adds a photo to the trip board for that location''' trip_id = request.form.get('trip') img_id = request.form.get('img_id') url = request.form.get('url') lat = request.form.get('lat') lon = request.form.get('lon') city_name = request.form.get('city_name') trip = Trip.get_trip(trip_id) # checks if authorized user is accessing this page if session.get('login') == trip.user_id: # if photo is not in the database, add it to the database if not Photo.get_photo(img_id): photo = Photo(img_id=int(img_id), url=url, lon=float(lon), lat=float(lat), city_name=city_name) db.session.add(photo) db.session.commit() # check if photo already exists in current users trip already_exists = TripPhotoRelationship.get_trip_photo(trip_id, img_id) if already_exists: return 'This photo is already in your trip board!' # photo is not in current trip board, add relationship to the database else: trip_photo = TripPhotoRelationship(trip_id=trip_id, photo_id=img_id) db.session.add(trip_photo) db.session.commit() return 'Photo Added' # unauthorized user, redirect to homepage else: flash('You do not have permission to access this feature') return 'Unauthorized User: Photo Not Added'
def remove_trip(): '''removes trip board from user boards''' trip_id = request.form.get("trip") user_id = request.form.get("user") # get trip from the database trip = Trip.get_trip(trip_id) # if the trip exists in the database if trip: # check if trip owner is same as logged in user if session.get('login') == trip.user_id: # get all liked relationship items for this trip from the database liked = LikedTrip.query.get(trip_id) liked_trips = TripUserLikes.query.filter( TripUserLikes.trip_id == trip_id).all() # if the trip has any liked relationships if liked_trips: # remove all liked trip relationships from the database for liked_trip in liked_trips: db.session.delete(liked_trip) db.session.commit() db.session.delete(liked) db.session.commit() else: pass # remove the liked trip and trip from the database db.session.delete(trip) db.session.commit() return redirect(url_for('userhome', user_id=user_id)) else: flash('You do not have permission to access this feature') return redirect('/') else: flash('Sorry, that is not a valid trip') return redirect('/')
def get_map(): ''' Shows a map with pins of photos for the trip ''' trip_id = request.form.get('trip-map') trip = Trip.get_trip(trip_id) # if the trip exists in the database if trip: photos = trip.photos # get city center coordinates to center map lon = trip.city.lon lat = trip.city.lat # turn city center coordinates into dictionary object city_geo = {"lon": lon, "lat": lat} trip_photos = [] # loop through each photo in the trip object for photo in photos: # extract photo details and create dictionary for each photo with details photo_details = { 'img_id': photo.img_id, 'url': photo.url, 'lat': photo.lat, 'lon': photo.lon } # append photo details dictionary to trip_photos list trip_photos.append(photo_details) # pass variables to html template as json objects return render_template('getmap.html', photos=json.dumps(trip_photos), cityGeo=json.dumps(city_geo), key=map_box_key) else: flash('Sorry could not find this trip!') return redirect('/')
def show_favorites(): '''Shows favorites page saved by user''' # check if user is logged in if session.get('login'): user_id = session.get('login') # gets user's favorite trip_ids from the database liked_trips_ids = User.get_user_by_id(user_id).liked_trips trips = [] # look up trip ids and append trip objects to trips list for liked_trip in liked_trips_ids: trip = Trip.get_trip(liked_trip.trip_id) trips.append(trip) return render_template('favorites.html', trips=trips) # user is not logged in, redirect to homepage else: return redirect('/')