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, )
def home(): # global login_logout global trip_id if request.method == "GET": if session.get("logged_in") and session["logged_in"] == True: trip_id = get_auto_gen_trip_id(str(datetime.now()), app_user.get_email()) trips_output = [] # set empty str admin_result = repo.get_trip_list(app_user.get_email(), True) # list of admin trip_id(s) in 'user' table user_result = repo.get_trip_list(app_user.get_email(), False) # list of user trip_id(s) in 'user' table all_users = [] sorted_by_datetime_all = [] if not admin_result or admin_result[0] == "None": pass else: ## 'admin_list' from Database Table 'trips' to table view (html) admin_list = [] for each in admin_result: admin_list.append(repo.get_trips_from_trips_table(each)) # 1. trips_output.append(get_trips_in_table(admin_list, True)) all_users.extend(admin_list) pass if not user_result or user_result[0] == "None": pass else: ## 'user_list' from Database Table 'trips' to table view (html) user_list = [] for each in user_result: user_list.append(repo.get_trips_from_trips_table(each)) # 2. trips_output.append(get_trips_in_table(user_list, False)) all_users.extend(user_list) pass # 3. trips_output = Markup(get_trips_in_table(sorted_by_datetime_all)) # sort by datetime if all_users: sorted_by_datetime_all = sorted(all_users, key=lambda tup: tup[3]) trips_output = Markup(get_trips_in_table(sorted_by_datetime_all)) else: trips_output = "Create or Add your First Trip!" ### END.......................................... # trips_output = Markup(" ".join(trips_output)) return render_template( "index.html", title="Home Page", user_name=app_user.get_fname(), auto_gen_trip_id=trip_id, acc="'s Account" if session["log"] else "", log_in_out="Log Out" if session["log"] else "Log In", show_trip_list=trips_output, year=datetime.now().year, ) pass else: print("failed to auth handling") pass elif request.method == "POST": ## Add Existing Trip_id from aother user if request.form["new_trip"] == "add_existing": # invalid input handling existing_trip_id = "" if request.form["existing_trip_id"]: existing_trip_id = request.form["existing_trip_id"] current_datetime = datetime.now().strftime("%Y-%m-%dT%H:%M") t = Trip( existing_trip_id, "trip_name", app_user.get_email(), current_datetime, current_datetime ) # trip_id, trip_name, user_id(email) # check if the trip_id is existing ? (True or False) # repo.check_exist_trip(existing_trip_id) # True or False if repo.check_exist_trip(existing_trip_id): repo.add_trip_name(t, False) else: flash_line = "The Trip Id '" + existing_trip_id + "' is not found. Please try again." flash(flash_line) pass else: flash("Please Enter Existing Trip Id") pass ## ***************************** START trips_output = [] # set empty str admin_result = repo.get_trip_list(app_user.get_email(), True) # list of admin trip_id(s) in 'user' table user_result = repo.get_trip_list(app_user.get_email(), False) # list of user trip_id(s) in 'user' table all_users = [] sorted_by_datetime_all = [] if not admin_result or admin_result[0] == "None": pass else: ## 'admin_list' from Database Table 'trips' to table view (html) admin_list = [] for each in admin_result: admin_list.append(repo.get_trips_from_trips_table(each)) # 1. trips_output.append(get_trips_in_table(admin_list, True)) all_users.extend(admin_list) pass if not user_result or user_result[0] == "None": pass else: ## 'user_list' from Database Table 'trips' to table view (html) user_list = [] for each in user_result: user_list.append(repo.get_trips_from_trips_table(each)) # 2. trips_output.append(get_trips_in_table(user_list, False)) all_users.extend(user_list) pass # 3. trips_output = Markup(" ".join(trips_output)) # sort by datetime if all_users: sorted_by_datetime_all = sorted(all_users, key=lambda tup: tup[3]) trips_output = Markup(get_trips_in_table(sorted_by_datetime_all)) else: trips_output = "Create or Add your First Trip!" return render_template( "index.html", title="Home Page", user_name=app_user.get_fname(), # travel_id = travel_id, auto_gen_trip_id=trip_id, show_trip_list=trips_output, acc="'s Account" if session["log"] else "", log_in_out="Log Out" if session["log"] else "Log In", year=datetime.now().year, ) ## ***************************** END elif request.form["new_trip"] == "create_new": # invalid input handling (TO DO) trip_name = "" if request.form["new_trip_name"]: trip_name = request.form["new_trip_name"] current_datetime = datetime.now().strftime("%Y-%m-%dT%H:%M") t = Trip(trip_id, trip_name, app_user.get_email(), current_datetime, current_datetime) repo.add_trip_name(t, True) else: flash("Please Enter Trip Name.") ## ***************************** START trip_id = get_auto_gen_trip_id(str(datetime.now()), app_user.get_email()) trips_output = [] # set empty str admin_result = repo.get_trip_list(app_user.get_email(), True) # list of admin trip_id(s) in 'user' table user_result = repo.get_trip_list(app_user.get_email(), False) # list of user trip_id(s) in 'user' table all_users = [] sorted_by_datetime_all = [] if not admin_result or admin_result[0] == "None": pass else: ## 'admin_list' from Database Table 'trips' to table view (html) admin_list = [] for each in admin_result: admin_list.append(repo.get_trips_from_trips_table(each)) # 1. trips_output.append(get_trips_in_table(admin_list, True)) all_users.extend(admin_list) pass if not user_result or user_result[0] == "None": pass else: ## 'user_list' from Database Table 'trips' to table view (html) user_list = [] for each in user_result: user_list.append(repo.get_trips_from_trips_table(each)) # 2. trips_output.append(get_trips_in_table(user_list, False)) all_users.extend(user_list) pass # 3. trips_output = Markup(" ".join(trips_output)) # sort by datetime if all_users: sorted_by_datetime_all = sorted(all_users, key=lambda tup: tup[3]) trips_output = Markup(get_trips_in_table(sorted_by_datetime_all)) else: trips_output = "Create or Add your First Trip!" return render_template( "index.html", title="Home Page", user_name=app_user.get_fname(), # travel_id = travel_id, auto_gen_trip_id=trip_id, show_trip_list=trips_output, acc="'s Account" if session["log"] else "", log_in_out="Log Out" if session["log"] else "Log In", year=datetime.now().year, ) ## ***************************** END if is_bad_str(trip_name): msg = "WRONG INPUT: please try again to create a trip list" return render_template("wrongInputTrip.html", user_name=app_user.get_fname(), msg=msg)