def console_add_record(a_user): """Allows the user to add a Record to a day. Calls the console_create_record function to do so. :param a_user: User """ good_format = False # Will not allow the user to continue without a valid date format. while not good_format: date_string = input("Enter date for record (MM/DD/YYYY, 0 for today: ") # If the user entered a date of their own, check it for validity. if date_string != "0": try: date_obj = datetime.datetime.strptime(date_string, '%m/%d/%Y').date() good_format = True except ValueError: print("Accepted inputs are 0 or the format MM/DD/YYYY. Try again.") # Otherwise, set the date to today's date. else: good_format = True date_obj = datetime.datetime.today().date() current_day = Day(date_of_day=date_obj) # Create a new Day object. # Attempt to insert the day into the user's doubly-linked list of Days. if not a_user.insert_day_in_list(current_day): # If the date was already there, find the appropriate day instead of inserting a new one. current_day = a_user.last_day while current_day.date_of_day != date_obj: current_day = current_day.previous_day # Display menu for adding records. print("Add record for:") print("1. Morning") print("2. Afternoon") print("3. Evening") print("4. Back") choice = int(input("\nEnter number: ")) while choice < 1 or choice > 4: print("Invalid entry.") choice = int(input("Enter number:")) if choice != 4: if choice == 1: current_day.morning_record = console_create_record() elif choice == 2: current_day.afternoon_record = console_create_record() else: current_day.evening_record = console_create_record()
def add_record(self, time, new_entry_window, close_on_add=True): """Strips date and Record from new_entry_window. Inserts Record into appropriate spot in the_user's doubly-linked list of Days. (adds spot if not found) :param time: int (represents Morning (1), Afternoon (2), Evening (3) :param new_entry_window: tkinter.Toplevel (specifically a _new_or_view_base window) :param close_on_add: bool (defaults to True) :return: """ # Strip date from window date = datetime.datetime.strptime( new_entry_window.date_entry_box.get(), "%m/%d/%Y").date() # Strip Record from window record_to_add = self._create_record(new_entry_window) # Creates a Day object with the appropriate date current_day = Day(date_of_day=date) # Inserts day in list (unless it is already there) if not the_user.insert_day_in_list(current_day): # Navigates to appropriate day in the list current_day = the_user.last_day while current_day.date_of_day != date: current_day = current_day.previous_day # Inserts record in appropriate spot of Day if time == 1: current_day.morning_record = record_to_add elif time == 2: current_day.afternoon_record = record_to_add elif time == 3: current_day.evening_record = record_to_add else: raise ValueError("Time of day not set.") # Closes window upon successfully adding record if close_on_add is true if close_on_add: new_entry_window.grab_release() new_entry_window.destroy()