|
5 | 5 | from pickpack import pickpack |
6 | 6 | from rich.console import Console |
7 | 7 | from rich.table import Table |
8 | | - |
| 8 | +from rich import print |
9 | 9 | """ |
10 | 10 | This file contains all of the functions needed for the Travel Itinerary Planner. |
11 | 11 | 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, |
52 | 52 | else: |
53 | 53 | return False |
54 | 54 |
|
55 | | - # Add the new itinerary after validation checks |
56 | | - itinerary_list.append(Itinerary(name, location, summary, start_date, end_date, flights, attractions)) |
57 | | - |
58 | 55 |
|
59 | | -def edit_itinerary(itinerary_list, chosen_itinerary): |
| 56 | +def edit_itinerary(chosen_itinerary): |
60 | 57 | # Use pickpack module (https://github.com/anafvana/pickpack#map-function-for-nested-lists) |
61 | 58 | pass |
62 | 59 |
|
63 | 60 |
|
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) |
67 | 87 | 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 | + |
71 | 92 |
|
| 93 | +def delete_itinerary(chosen_itinerary): |
| 94 | + itinerary_list = load_itineraries() |
72 | 95 |
|
73 | | -def delete_itinerary(itinerary_list, chosen_itinerary): |
74 | 96 | print("What would you like to delete?") |
75 | 97 | # Use pickpack module (https://github.com/anafvana/pickpack#map-function-for-nested-lists) |
76 | 98 | pass |
77 | 99 |
|
78 | 100 |
|
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 |
83 | 139 |
|
84 | 140 |
|
85 | 141 | # Validation functions |
@@ -123,3 +179,16 @@ def validate_dates(start_date, end_date, flights): |
123 | 179 | return False |
124 | 180 |
|
125 | 181 | 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