def get_trip_events_in_table(trip_id): global login_logout output = "" travel_result = repo.get_travel_events(trip_id) # list of tuples # sort by datetime sorted_by_datetime = sorted(travel_result, key=lambda tup: tup[2]) # display trip events in trip obj for event in sorted_by_datetime: add = """ <tr> <td> """ output += add for cell in event: add = ( cell + """</td> <td>""" ) output += add add = """</td> </tr> """ output += add return output
def travel(): if request.method == 'GET': return render_template('ShowTravelInfo.html', travel_id = travel_id) elif request.method == 'POST': travel_result = repo.get_travel_events(travel_id) # list of tuples output = "" for event in travel_result: add = """ <tr> <td> """ output += add for cell in event: add = cell + """</td> <td>""" output += add add = """</td> </tr> """ output += add output = Markup(output) return render_template('ShowTravelInfo.html', travel_id = travel_id, sorted_result= output) # return str(result_flight) return
def trip(trip_id): if ( request.method == "GET" ): # ------------------------------------------------------------------------------------------------ Get output = get_trip_events_in_table(trip_id) output = Markup(output) # get admin_user_id admin_user_of_this_trip = repo.get_admin_user_id(trip_id) admin_button = get_admin_button(trip_id, admin_user_of_this_trip) # admin_button = Markup(admin_button), return render_template( "trip.html", title="Home Page", user_name=app_user.get_fname(), trip_id=trip_id, trip_name=repo.get_trip_name(trip_id), admin_button=Markup(admin_button), sorted_result=output, acc="'s Account" if session["log"] else "", log_in_out="Log Out" if session["log"] else "Log In", year=datetime.now().year, ) elif ( request.method == "POST" ): # ------------------------------------------------------------------------------------------------ POST # retrieve lists flights = request.values.getlist("flight") flights_dt = request.values.getlist("flight_datetime") hotels = request.values.getlist("hotel") hotels_dt = request.values.getlist("hotel_datetime") places = request.values.getlist("place") places_dt = request.values.getlist("place_datetime") f_obj_list = [] h_obj_list = [] p_obj_list = [] # save respective objs in lists if len(flights) != 0 and len(flights) == len(flights_dt): for i in range(len(flights)): # input validation if not is_valid_datetime(flights_dt[i]) or is_bad_str(flights[i]): msg = "" if not is_valid_datetime(flights_dt[i]): msg = ( msg + "<p style='color:#f00'><b>INVALID INPUT:</b> please try again to create a trip 'Flight Date/Time' field</p>" ) if is_bad_str(flights[i]): msg = ( msg + "<p style='color:#f00'><b>INVALID INPUT:</b> please try again to create a trip 'Flight Name' field</p>" ) return render_template( "wrongInputField.html", trip_id=trip_id, user_name=app_user.fname, acc="'s Account" if session["log"] else "", log_in_out="Log Out" if session["log"] else "Log In", msg=Markup(msg), ) f_obj = Flight(trip_id, flights_dt[i], flights[i]) # create a Flight obj url = "https://flightaware.com/live/flight/" + str(f_obj.info) req = req = requests.get(url) soup = BeautifulSoup(req.text, "html.parser") flight_state = [] # from website https://flightware.com/live/flight/ for each in soup.findAll("td", {"class": "smallrow1"}): flight_state.append(each.text) if len(flight_state) == 0: f_obj.status = "NOT AVAILABLE" else: # flight information is good to go f_obj.status = str(re.sub(r"\([^)]*\)", "", flight_state[0])) f_obj_list.append(f_obj) if len(hotels) != 0 and len(hotels) == len(hotels_dt): for i in range(len(hotels)): # input validation if not is_valid_datetime(hotels_dt[i]) or is_bad_str(hotels[i]): msg = "" if not is_valid_datetime(hotels_dt[i]): msg = ( msg + "<p style='color:#f00'><b>INVALID INPUT:</b> please try again to create a trip 'Hotel Date/Time' field</p>" ) if is_bad_str(hotels[i]): msg = ( msg + "<p style='color:#f00'><b>INVALID INPUT:</b> please try again to create a trip 'Hotel Name' field</p>" ) return render_template( "wrongInputField.html", trip_id=trip_id, user_name=app_user.fname, acc="'s Account" if session["log"] else "", log_in_out="Log Out" if session["log"] else "Log In", msg=Markup(msg), ) h_obj = Hotel(trip_id, hotels_dt[i], hotels[i]) h_obj_list.append(h_obj) if len(places) != 0 and len(places) == len(places_dt): for i in range(len(places)): # input validation if not is_valid_datetime(places_dt[i]) or is_bad_str(places[i]): msg = "" if not is_valid_datetime(places_dt[i]): msg = ( msg + "<p style='color:#f00'><b>INVALID INPUT:</b> please try again to create a trip 'Place Date/Time' field</p>" ) if is_bad_str(places[i]): msg = ( msg + "<p style='color:#f00'><b>INVALID INPUT:</b> please try again to create a trip 'Place Name' field</p>" ) return render_template( "wrongInputField.html", trip_id=trip_id, user_name=app_user.fname, acc="'s Account" if session["log"] else "", log_in_out="Log Out" if session["log"] else "Log In", msg=Markup(msg), ) p_obj = Place(trip_id, places_dt[i], places[i]) p_obj_list.append(p_obj) # add to repo -> database for each_flight_obj in f_obj_list: repo.add_flight(each_flight_obj) # update datetime start/end if repo.get_trip_datetime_start(each_flight_obj) > each_flight_obj.datetime: repo.update_trip_datetime_start(each_flight_obj.datetime, each_flight_obj.travel_id) if repo.get_trip_datetime_end(each_flight_obj) < each_flight_obj.datetime: repo.update_trip_datetime_end(each_flight_obj.datetime, each_flight_obj.travel_id) for each_hotel_obj in h_obj_list: repo.add_hotel(each_hotel_obj) # update datetime start/end if repo.get_trip_datetime_start(each_hotel_obj) > each_hotel_obj.datetime: repo.update_trip_datetime_start(each_hotel_obj.datetime, each_hotel_obj.travel_id) if repo.get_trip_datetime_end(each_hotel_obj) < each_hotel_obj.datetime: repo.update_trip_datetime_end(each_hotel_obj.datetime, each_hotel_obj.travel_id) for each_place_obj in p_obj_list: repo.add_place(each_place_obj) # update datetime start/end if repo.get_trip_datetime_start(each_place_obj) > each_place_obj.datetime: repo.update_trip_datetime_start(each_place_obj.datetime, each_place_obj.travel_id) if repo.get_trip_datetime_end(each_place_obj) < each_place_obj.datetime: repo.update_trip_datetime_end(each_place_obj.datetime, each_place_obj.travel_id) # time to display output = "" travel_result = repo.get_travel_events(trip_id) # list of tuples # sort by datetime sorted_by_datetime = sorted(travel_result, key=lambda tup: tup[2]) for event in sorted_by_datetime: add = """ <tr> <td> """ output += add for cell in event: add = ( cell + """</td> <td>""" ) output += add add = """</td> </tr> """ output += add output = Markup(output) # get admin_user_id admin_user_of_this_trip = repo.get_admin_user_id(trip_id) admin_button = get_admin_button(trip_id, admin_user_of_this_trip) # admin_button = Markup(admin_button), return render_template( "trip.html", user_name=app_user.get_fname(), # travel_id = travel_id, trip_id=trip_id, trip_name=repo.get_trip_name(trip_id), admin_button=Markup(admin_button), sorted_result=output, acc="'s Account" if session["log"] else "", log_in_out="Log Out" if session["log"] else "Log In", year=datetime.now().year, )