Skip to content

Commit 5dcea41

Browse files
Feat: view_itinerary feature added and works successfully.
1 parent 00864d2 commit 5dcea41

3 files changed

Lines changed: 89 additions & 25 deletions

File tree

Travel Itinerary Planner/main.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,11 @@ def run_app():
2828
print("Enter a number between 1-5.")
2929
else:
3030
("Invalid input: Please enter a number between 1-5.")
31-
31+
3232
# Add a new itinerary
3333
if user_choice == "1":
34-
flights_list_done = False
35-
attractions_list_done = False
36-
3734
print("How exciting! Please provide us information about the trip: \n")
38-
name = input("Title of the itinerary: ")
35+
name = input("Name of the itinerary (please choose a name you will remember for later): ")
3936
location = input("Location (NA if not applicable): ")
4037
summary = input("Brief description of trip: ")
4138
start_date = input("Start date in DD-MM-YYYY: ")
@@ -123,9 +120,7 @@ def run_app():
123120

124121
# View itinerary
125122
elif user_choice == "3":
126-
chosen_itinerary = input("Please choose a trip")
127-
view_itineraries(itineraries, chosen_itinerary)
128-
pass
123+
view_itineraries(itineraries)
129124

130125
# Delete itinerary
131126
elif user_choice == "4":

Travel Itinerary Planner/src/itinerary.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(self, name, location, summary, start_date, end_date, flights, attra
2727
self.summary = summary
2828
self.start_date = start_date
2929
self.end_date = end_date
30-
self.flights = flights
30+
self.flights = flights # https://docs.python.org/3/tutorial/classes.html#class-and-instance-variables
3131
self.attractions = attractions
3232
pass
3333

Travel Itinerary Planner/src/manage_itineraries.py

Lines changed: 85 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from pickpack import pickpack
66
from rich.console import Console
77
from rich.table import Table
8-
8+
from rich import print
99
"""
1010
This file contains all of the functions needed for the Travel Itinerary Planner.
1111
It allows the user to add, edit, view, delete, and export an itinerary while checking for errors in input.
@@ -52,34 +52,90 @@ def add_itinerary(itinerary_list, name, location, summary, start_date, end_date,
5252
else:
5353
return False
5454

55-
# Add the new itinerary after validation checks
56-
itinerary_list.append(Itinerary(name, location, summary, start_date, end_date, flights, attractions))
57-
5855

59-
def edit_itinerary(itinerary_list, chosen_itinerary):
56+
def edit_itinerary(chosen_itinerary):
6057
# Use pickpack module (https://github.com/anafvana/pickpack#map-function-for-nested-lists)
6158
pass
6259

6360

64-
def view_itineraries(itinerary_list, chosen_itinerary):
65-
if not itinerary_list:
66-
print("No itineraries planned!")
61+
def view_itineraries(itinerary_list):
62+
# Use pick module: Ask if they would like to view all itineraries, or a specific one
63+
view_prompt = 'Would you like to view all the existing itineraries?: '
64+
view_options = ['View All', 'View One']
65+
option, index = pick(view_options, view_prompt)
66+
if option == 'View All':
67+
print(f"\nFilter selected: {option}")
68+
# Use rich to print table
69+
print_table(itinerary_list)
70+
elif option == 'View One':
71+
# Use pick module to select an itinerary by name and location
72+
print(f"\nFilter selected: {option}")
73+
itinerary_choice = 'Which itinerary would you like to view?: '
74+
itinerary_options = []
75+
76+
for trip in itinerary_list:
77+
itinerary_options.append(trip["name"])
78+
filter_option, filter_index = pick(itinerary_options, itinerary_choice)
79+
80+
# Filter itinerary list
81+
filtered_itineraries = []
82+
for itinerary in itinerary_list:
83+
if filter_option == itinerary["name"]:
84+
filtered_itineraries.append(itinerary)
85+
# Use rich to print table
86+
print_table(filtered_itineraries)
6787
else:
68-
# Use pick module: Ask if they would like to view all itineraries, or a specific one
69-
pass
70-
return
88+
print("Error: Valid filter not selected, returning to Task Manager menu.\n")
89+
return False
90+
return True
91+
7192

93+
def delete_itinerary(chosen_itinerary):
94+
itinerary_list = load_itineraries()
7295

73-
def delete_itinerary(itinerary_list, chosen_itinerary):
7496
print("What would you like to delete?")
7597
# Use pickpack module (https://github.com/anafvana/pickpack#map-function-for-nested-lists)
7698
pass
7799

78100

79-
def export_itinerary(itinerary, chosen_itinerary):
80-
# Export to .pdf or .csv file
81-
# Use pick (not pickpack) library to list itineraries by name and location
82-
pass
101+
# Print functions
102+
def print_table(trips):
103+
trip_console = Console()
104+
trip_table = Table(title="Itineraries", show_lines=True)
105+
106+
trip_table.add_column("Trip Name", justify="center", no_wrap=True)
107+
trip_table.add_column("Location", justify="center")
108+
trip_table.add_column("Summary", justify="center")
109+
trip_table.add_column("Start Date", justify="center", no_wrap=True)
110+
trip_table.add_column("End Date", justify="center", no_wrap=True)
111+
trip_table.add_column("Flights", justify="left", no_wrap=True)
112+
trip_table.add_column("Attractions", justify="left", style="bold")
113+
114+
for trip in trips:
115+
for key in trip["flights"]:
116+
if len(key) == 1:
117+
departure_flight_name = key
118+
flight_departure = trip["flights"][f"{departure_flight_name}"]["departure date"]
119+
flight_list = f'[bold red]{departure_flight_name}[/bold red]: {flight_departure}\n'
120+
elif len(key) >= 2:
121+
flight_list = ''
122+
departure_flight_name = key
123+
flight_departure = trip["flights"][f"{departure_flight_name}"]["departure date"]
124+
flight_item = f'[bold red]{departure_flight_name}[/bold red]: {flight_departure}\n'
125+
flight_list = flight_list + f'{flight_item}'
126+
for key in trip["attractions"]:
127+
if len(key) == 1:
128+
attraction = key
129+
attraction_list = f'[bold red]{attraction}[/bold red] \nAddress: {trip["attractions"][f"{attraction}"]["address"]} \nSummary: {trip["attractions"][f"{attraction}"]["summary"]} \nTag(s): {trip["attractions"][f"{attraction}"]["tag(s)"]}\n'
130+
elif len(key) >= 2:
131+
attraction_list = ''
132+
attraction = key
133+
# Printing python text with colour using ANSI codes: https://vascosim.medium.com/how-to-print-colored-text-in-python-52f6244e2e30
134+
attraction_item = f'[bold red]{attraction}[/bold red] \nAddress: {trip["attractions"][f"{attraction}"]["address"]} \nSummary: {trip["attractions"][f"{attraction}"]["summary"]} \nTag(s): {trip["attractions"][f"{attraction}"]["tag(s)"]}\n'
135+
attraction_list = attraction_list + f'{attraction_item}'
136+
trip_table.add_row(trip["name"], trip["location"], trip["summary"], trip["start_date"], trip["end_date"], flight_list, attraction_list)
137+
trip_console.print(trip_table)
138+
return
83139

84140

85141
# Validation functions
@@ -123,3 +179,16 @@ def validate_dates(start_date, end_date, flights):
123179
return False
124180

125181
return True
182+
183+
184+
def find_itinerary(itinerary_list, trip_name):
185+
'''
186+
187+
Args:
188+
itinerary_list: List of all itineraries saved to itinerary.bin file.
189+
trip_name: Name of the trip user wants to view.
190+
191+
Returns:
192+
itinerary: The itinerary the user wants to view.
193+
'''
194+
return [itinerary for itinerary in itinerary_list if itinerary["name"] == trip_name]

0 commit comments

Comments
 (0)