def handle_remove_driver(driver_repo: Repo, parser: Parser): """ Handle removing a driver Args: driver_repo (Repo): Driver repository parser (Parser): Input parser """ # Get driver id: done_id = False _, driver_list = driver_repo.get() while not done_id: id_ = input( "Enter driver id (numeric) or leave blank to see the driver list > " ) if id_ == "": table_data = [["ID", "Name"]] for driver in driver_list: table_data.append([str(driver.id), driver.name]) driver_table = SingleTable(table_data, title="Drivers") driver_table.justify_columns = { 0: "left", 1: "center", } while True: console.clear_console() print(driver_table.table) input_ = input("Type b or back to go back > ") if input_ == "b" or input_ == "back": break else: continue else: try: id_ = int(id_) if parser.check_if_already_exists(by_id=True, id=id_): # Id exists, continue: done_id = True driver_repo.delete(entity_id=id_) save_data(mode="single", only="drivers", driver_instance_list=driver_list) olt.show(title="Success", message="The driver was removed succesfully") except ValueError: console.clear_console() olt.show(title="Info", message="Invalid ID! The ID Must be numeric", go_back=False) else: console.clear_console() olt.show(title="Info", message="Invalid ID!", go_back=False)
def show_cars(self) -> None: car_repo = Repo(self.car_instance_list) response, car_list = car_repo.get(mode="all") if response == "found" and len(car_list) != 0: table_data = [["ID", "Registration", "Brand", "HP", "KMs"]] for car in car_list: table_data.append([ str(car.id), car.reg, car.brand, str(car.hp), str(car.kms) ]) car_table = SingleTable(table_data, title="Cars") car_table.justify_columns = { 0: "left", 1: "center", 2: "center", 3: "left", 4: "left" } while True: console.clear_console() print(car_table.table) input_ = input("Type b or back to go back > ") if input_ == "b" or input_ == "back": break else: continue elif len(car_list) == 0: olt.show( title="Info", message="No cars! Either add using the menu or change the file" ) else: olt.show(title="Something went wrong", message="Please try again")
def show_drivers(self) -> None: driver_repo = Repo(self.driver_instance_list) response, driver_list = driver_repo.get(mode="all") if response == "found" and len(driver_list) != 0: table_data = [["ID", "Name", "Age", "Car"]] for driver in driver_list: car_ = driver.car car_data = f"{car_.brand} with reg: {car_.reg}" if car_ is not None else "Driver does not have a car asociatted" table_data.append( [str(driver.id), driver.name, str(driver.age), car_data]) driver_table = SingleTable(table_data, title="Drivers") driver_table.justify_columns = { 0: "left", 1: "left", 2: "left", 3: "center" } while True: console.clear_console() print(driver_table.table) input_ = input("Type b or back to go back > ") if input_ == "b" or input_ == "back": break else: continue elif len(driver_list) == 0: olt.show( title="Info", message= "No drivers! Either add using the menu or change the file") else: olt.show(title="Something went wrong", message="Please try again")
def handle_update_driver(driver_repo: Repo, parser: Parser, car_repo: Repo): """ Handle updateing a driver Args: driver_repo (Repo): Driver repository parser (Parser): Input data parser car_repo (Repo): Car repository """ # Get driver id: got_id = False _, driver_list = driver_repo.get() while not got_id: id_ = input( "Enter driver id (numeric) or leave blank to see the driver list > " ) if id_ == "": table_data = [["ID", "Name"]] for driver in driver_list: table_data.append([str(driver.id), driver.name]) driver_table = SingleTable(table_data, title="Drivers") driver_table.justify_columns = { 0: "left", 1: "center", } while True: console.clear_console() print(driver_table.table) input_ = input("Type b or back to go back > ") if input_ == "b" or input_ == "back": break else: continue else: try: id_ = int(id_) if parser.check_if_already_exists(by_id=True, id=id_): # Id exists, continue: got_id = True done_id = False done_age = False #new_car = None while not done_id: new_id = input( "If you want to change the ID enter it now, leave blank to skip > " ) if new_id != "": try: new_id = int(new_id) done_id = True except ValueError: console.clear_console() olt.show(title="Info", message= "Invalid ID! The ID Must be numeric", go_back=False) else: new_id = None done_id = True new_name = input( "If you want to change the Name enter it now, leave blank to skip > " ) if new_name == "": new_name = None while not done_age: new_age = input( "If you want to change the Age enter it now, leave blank to skip > " ) if new_age != "": try: new_age = int(new_age) done_age = True except ValueError: console.clear_console() olt.show(title="Info", message= "Invalid ID! The ID Must be numeric", go_back=False) else: new_age = None done_age = True new_car_yn = input( "Do you want to change the driver's car enter \"y\" to change and \"n\" to skip > " ).strip().lower() if new_car_yn == 'y': new_car_id = input( "Enter the new car's ID, leave blank to see all cars, or type \"none\" to delete the car from the driver > " ).strip().lower() if new_car_id == "": _, car_list = car_repo.get() if len(car_list) == 0 or car_list is None: olt.show( title="Something went wrong", message= "Either there are no cars at this moment or something else went wrong" ) else: table_data = [["ID", "Registration"]] for car in car_list: table_data.append([str(car.id), car.reg]) car_table = SingleTable(table_data, title="Cars") car_table.justify_columns = { 0: "left", 1: "center", } while True: console.clear_console() print(car_table.table) input_ = input( "Type b or back to go back > ") if input_ == "b" or input_ == "back": break else: continue new_car_id = input("Enter the new car's ID > ") try: new_car_id = int(new_car_id) resp, car = car_repo.get( mode="single", entity_id=new_car_id) if resp is "found": new_car = car else: olt.show( title="Fail", message= "The car coudn't be found, discarding car changes", go_back=False) except ValueError: olt.show( title="Fail", message= "The car's id must be numeric, discarding car changes", go_back=False) elif new_car_id == "none": new_car = -1 else: try: new_car_id = int(new_car_id) resp, car = car_repo.get(mode="single", entity_id=new_car_id) if resp == "found": new_car = car else: olt.show( title="Fail", message= "The car coudn't be found, discarding car changes", go_back=False) except ValueError: olt.show( title="Fail", message= "222The car's id must be numeric, discarding car changes", go_back=False) else: new_car = None driver_repo.update(type_of_entity="driver", entity_id=id_, id=new_id, name=new_name, age=new_age, car=new_car) save_data(mode="single", only="drivers", driver_instance_list=driver_list) olt.show(title="Success", message="The driver was updated succesfully") except ValueError: console.clear_console() olt.show(title="Info", message="Invalid ID! The ID Must be numeric", go_back=False) else: console.clear_console() olt.show(title="Info", message="Invalid ID!", go_back=False)
def handle_update_car(car_repo: Repo, parser: Parser): """ Handle updateing a car Args: car_repo (Repo): Car repository parser (Parser): Input parser """ # Get car id: got_id = False _, car_list = car_repo.get() while not got_id: id_ = input( "Enter car id (numeric) or leave blank to see the car list > ") if id_ == "": table_data = [["ID", "Registration"]] for car in car_list: table_data.append([str(car.id), car.reg]) car_table = SingleTable(table_data, title="Cars") car_table.justify_columns = { 0: "left", 1: "center", } while True: console.clear_console() print(car_table.table) input_ = input("Type b or back to go back > ") if input_ == "b" or input_ == "back": break else: continue else: try: id_ = int(id_) if parser.check_if_already_exists(by_id=True, id=id_): # Id exists, continue: got_id = True done_id = False done_hp = False done_kms = False while not done_id: new_id = input( "If you want to change the ID enter it now, leave blank to skip > " ) if new_id != "": try: new_id = int(new_id) done_id = True except ValueError: console.clear_console() olt.show(title="Info", message= "Invalid ID! The ID Must be numeric", go_back=False) else: new_id = None done_id = True new_reg = input( "If you want to change the Registration No. enter it now, leave blank to skip > " ) if new_reg == "": new_reg = None new_brand = input( "If you want to change the Brand enter it now, leave blank to skip > " ) if new_brand == "": new_brand = None while not done_hp: new_hp = input( "If you want to change the Horsepower enter it now, leave blank to skip > " ) if new_hp != "": try: new_hp = int(new_hp) done_hp = True except ValueError: console.clear_console() olt.show( title="Info", message= "Invalid HP! The Horespower be a numeric value", go_back=False) else: new_hp = None done_hp = True while not done_kms: new_kms = input( "If you want to change the KMs enter them now, leave blank to skip > " ) if new_kms != "": try: new_kms = int(new_kms) done_kms = True except ValueError: console.clear_console() olt.show(title="Info", message= "Invalid KM! The KMs Must be numeric", go_back=False) else: new_kms = None done_kms = True # Actually update-ing: car_repo.update(type_of_entity="car", entity_id=id_, id=new_id, reg=new_reg, brand=new_brand, hp=new_hp, kms=new_kms) save_data(mode="single", only="cars", car_instance_list=car_list) olt.show(title="Success", message="The car was updated succesfully") except ValueError: console.clear_console() olt.show(title="Info", message="Invalid ID! The ID Must be numeric", go_back=False) else: console.clear_console() olt.show(title="Info", message="Invalid ID!", go_back=False)
def handle_add_car(car_repo: Repo, parser: Parser): """ Handle adding a car Args: car_repo (Repo): Car repository parser (Parser): Input parser """ new_id = None new_reg = "" new_brand = "" new_hp = None new_kms = None # Get car id: done_id = False while not done_id: id_ = input("Enter car id (numeric), leave blank for autocomplete > ") if id_ == "": _, last_entity = car_repo.get(mode="last") id_ = last_entity.id found = False while not found: id_ += 1 if not parser.check_if_already_exists(by_id=True, id=int(id_)): found = True new_id = int(id_) done_id = True print(f"\tAutocomplete succesfull, assigned id {new_id}") else: if not parser.check_if_already_exists(by_id=True, id=int(id_)): new_id = int(id_) done_id = True else: console.clear_console() olt.show( title="Info", message= "This id already exists, you might want to auto-complete, just press enter", go_back=False) # Get car registration: new_reg = input("Enter car registration no. > ").upper() # Get car brand: new_brand = input("Enter car brand > ") # Get car hp: done_hp = False while not done_hp: hp = input("Enter the car's horsepower (numeric) > ") try: new_hp = int(hp) done_hp = True except ValueError: console.clear_console() olt.show(title="Info", message="The horsepower must be a numeric value, ex: 105", go_back=False) # Get car kms: done_kms = False while not done_kms: kms = input("Enter the car's current KMs (numeric) > ") try: new_kms = int(kms) done_kms = True except ValueError: console.clear_console() olt.show(title="Info", message= "The current kilometers must be a numeric value, ex: 105", go_back=False) car_repo.add(Car(new_id, new_reg, new_brand, new_hp, new_kms)) _, car_instance_list = car_repo.get() save_data(mode="single", only="cars", car_instance_list=car_instance_list) olt.show(title="Success", message="The new car was added succesfully")
def handle_add_driver(driver_repo: Repo, parser: Parser, car_repo: Repo) -> None: """ Handle adding a driver Args: driver_repo (Repo): Driver repository parser (Parser): Input data parser car_repo (Repo): Car repository """ new_id = None new_name = "" new_age = None # Get driver id: done_id = False while not done_id: id_ = input( "Enter driver id (numeric), leave blank for autocomplete > ") if id_ == "": _, last_entity = driver_repo.get(mode="last") id_ = last_entity.id found = False while not found: id_ += 1 if not parser.check_if_already_exists(by_id=True, id=int(id_)): found = True new_id = int(id_) done_id = True print(f"\tAutocomplete succesfull, assigned id {new_id}") else: if not parser.check_if_already_exists(by_id=True, id=int(id_)): new_id = int(id_) done_id = True else: console.clear_console() olt.show( title="Info", message= "This id already exists, you might want to auto-complete, just press enter", go_back=False) # Get driver name: new_name = input("Enter the driver's name > ") # Get driver age: done_age = False while not done_age: age = input("Enter the driver's age (numeric) > ") try: new_age = int(age) done_age = True except ValueError: console.clear_console() olt.show(title="Info", message="The age must be a numeric value, ex: 45", go_back=False) # Deal with car assignment: if input("Do you want to assign a car now? y/n > ").lower() == "y": # Add car: print( "In order to assign a car to a driver you need it's ID, do you know it, or do you want to see all cars" ) input_ = input("Press enter to see all cars or enter an ID > ") if input_ == "": _, car_list = car_repo.get() if len(car_list) == 0 or car_list is None: olt.show( title="Something went wrong", message= "Either there are no cars at this moment or something else went wrong" ) else: table_data = [["ID", "Registration"]] for car in car_list: table_data.append([str(car.id), car.reg]) car_table = SingleTable(table_data, title="Cars") car_table.justify_columns = { 0: "left", 1: "center", } while True: console.clear_console() print(car_table.table) input_ = input("Type b or back to go back > ") if input_ == "b" or input_ == "back": break else: continue console.clear_console() input_ = input( "Enter car id or \"back\" if you want to leave it blank for now > " ) if input_ == "back": # Just add driver with no car assigned driver_repo.add(Driver(new_id, new_name, new_age, None)) else: try: new_car_id = int(input_) resp, car_ = car_repo.get(mode="single", entity_id=new_car_id) if resp == "found": # Add driver with respective car driver_repo.add( Driver(new_id, new_name, new_age, car_)) else: olt.show( title="Warning", message= "ID not found! Leaving blank for now, update via menu" ) driver_repo.add( Driver(new_id, new_name, new_age, None)) except ValueError: olt.show( title="Warning", message= "The id must be numeric. Leaving blank for now, update via menu" ) driver_repo.add(Driver(new_id, new_name, new_age, None)) else: # User gave manual id: try: new_car_id = int(input_) resp, car_ = car_repo.get(mode="single", entity_id=new_car_id) if resp == "found": # Add driver with respective car driver_repo.add(Driver(new_id, new_name, new_age, car_)) else: olt.show( title="Warning", message= "ID not found! Leaving blank for now, update via menu") driver_repo.add(Driver(new_id, new_name, new_age, None)) except ValueError: olt.show( title="Warning", message= "The id must be numeric. Leaving blank for now, update via menu" ) driver_repo.add(Driver(new_id, new_name, new_age, None)) else: # Just add driver with no car assigned driver_repo.add(Driver(new_id, new_name, new_age, None)) _, driver_instance_list = driver_repo.get() save_data(mode="single", only="drivers", driver_instance_list=driver_instance_list) olt.show(title="Success", message="The new driver was added succesfully")