def edit_task(self): """Edit the task.""" utilities.show_edit_title() self.show_task_detail() # Display the entry to be edited utilities.show_edit_menu_options() # Ask the user what to change while True: edit_item = input("\nEnter 'a', 'b', 'c' 'd' or 'r': ") if edit_item.upper() == 'A': # Edit date self.edit_taskdate() break if edit_item.upper() == 'B': # Edit task name self.edit_taskname() break if edit_item.upper() == 'C': # Edit time spent self.edit_tasktime() break if edit_item.upper() == 'D': # Edit notes self.edit_tasknotes() break if edit_item.upper() == 'R': from work_log import search_menu search_menu() break else: # Capture incorrect user input utilities.show_edit_title() self.show_task_detail() utilities.show_edit_menu_options() print("\nSorry, we did not recoginse '{}'" ", please try again.".format(edit_item)) continue
def time_search(self, duration): """Search for an exact duration match.""" for row in self.dataset[0:self.numtasks]: if row['task_time'] == str(duration): self.results.append(row) if self.results: self.show_results() else: from work_log import search_menu utilities.show_results_title() input('\nSorry, there are no tasks listed for with a duration ' 'of {} minutes.\n\nPress ENTER to return to the ' 'search menu.'.format(duration)) search_menu()
def date_search(self, exact_date): """Search for an exact date match.""" for row in self.dataset[0:self.numtasks]: if row['task_date'] == str(exact_date): self.results.append(row) if self.results: self.show_results() else: from work_log import search_menu utilities.show_results_title() input('\nSorry, there are no tasks listed for {}\n\n' 'Press ENTER to return to the search menu.'.format( friendly_date(exact_date))) search_menu()
def pattern_search(self, pattern): """Search for an exact text match.""" for row in self.dataset[0:self.numtasks]: if re.search(pattern, row['task_name']) or \ re.search(pattern, row['task_notes']): self.results.append(row) if self.results: self.show_results() else: from work_log import search_menu utilities.show_results_title() input("\nSorry, there are no tasks listed that match the pattern" " '{}'.\n\nPress ENTER to return to the " "search menu.".format(pattern)) search_menu()
def text_search(self, text): """Search for an exact text match.""" for row in self.dataset[0:self.numtasks]: if text.lower() in row['task_name'].lower() or \ text.lower() in row['task_notes'].lower(): self.results.append(row) if self.results: self.show_results() else: from work_log import search_menu utilities.show_results_title() input("\nSorry, there are no tasks listed that include " "'{}'.\n\nPress ENTER to return to the " "search menu.".format(text)) search_menu()
def range_search(self, start_date, end_date): """Search across a date range.""" for row in self.dataset[0:self.numtasks]: task_date = datetime.datetime.strptime(row['task_date'], '%Y-%m-%d %H:%M:%S') if start_date <= task_date <= end_date: self.results.append(row) if self.results: self.show_results() else: from work_log import search_menu utilities.show_results_title() input('\nSorry, there are no tasks listed ' 'within that date range.\n\n' 'Press ENTER to return to the search menu.') search_menu()
def show_results_menu(self): """Show the results options.""" from work_log import search_menu while True: selction = input('\n[N]ext, [E]dit, [D]elete, ' '[R]eturn to seach menu > ') if selction.upper() == 'N': # Show next search result try: self.count += 1 self.show_results() break except IndexError: utilities.show_results_title() input('\nNo more results to show.\n\n' 'Press ENTER to return to the search menu.') search_menu() break if selction.upper() == 'E': # Edit the entry self.edit_task() break if selction.upper() == 'D': # Delete row from CSV file confirm = input('\nDelete entry? (Y/N)') if confirm.upper() == 'Y': self.do_edit(delete=True) break else: search_menu() break if selction.upper() == 'R': # Return to search menu search_menu() break else: utilities.show_results_title() print('\nResult {} of {}:'.format(self.count + 1, len(self.results))) self.show_task_detail() print("\nSorry, we did not recoginse '{}'" ", please try again.".format(selction)) continue
def do_edit(self, **kwargs): """Update the CSV with the edited record.""" self.delete = False self.date = None self.name = None self.time = None self.notes = None for key, value in kwargs.items(): setattr(self, key, value) utilities.show_edit_title() if self.delete: # Delete the record (https://tinyurl.com/y4je42ka) status = "deleted" fieldnames = [ 'task_id', 'task_date', 'task_name', 'task_time', 'task_notes' ] with open('tasks.csv') as csvfile, \ open('temp.csv', 'w', newline='') as outputfile: reader = csv.DictReader(csvfile, fieldnames=fieldnames) writer = csv.DictWriter(outputfile, fieldnames=fieldnames) for row in reader: if not row['task_id'] == self.results[ self.count]['task_id']: writer.writerow({ 'task_id': row['task_id'], 'task_date': row['task_date'], 'task_name': row['task_name'], 'task_time': row['task_time'], 'task_notes': row['task_notes'] }) shutil.move('temp.csv', 'tasks.csv') elif self.date: # Update the task date status = "updated" fieldnames = [ 'task_id', 'task_date', 'task_name', 'task_time', 'task_notes' ] with open('tasks.csv') as csvfile, \ open('temp.csv', 'w', newline='') as outputfile: reader = csv.DictReader(csvfile, fieldnames=fieldnames) writer = csv.DictWriter(outputfile, fieldnames=fieldnames) for row in reader: if row['task_id'] == self.results[self.count]['task_id']: writer.writerow({ 'task_id': row['task_id'], 'task_date': self.date, 'task_name': row['task_name'], 'task_time': row['task_time'], 'task_notes': row['task_notes'] }) else: writer.writerow({ 'task_id': row['task_id'], 'task_date': row['task_date'], 'task_name': row['task_name'], 'task_time': row['task_time'], 'task_notes': row['task_notes'] }) shutil.move('temp.csv', 'tasks.csv') elif self.name: # Update the task name status = "updated" fieldnames = [ 'task_id', 'task_date', 'task_name', 'task_time', 'task_notes' ] with open('tasks.csv') as csvfile, \ open('temp.csv', 'w', newline='') as outputfile: reader = csv.DictReader(csvfile, fieldnames=fieldnames) writer = csv.DictWriter(outputfile, fieldnames=fieldnames) for row in reader: if row['task_id'] == self.results[self.count]['task_id']: writer.writerow({ 'task_id': row['task_id'], 'task_date': row['task_date'], 'task_name': self.name, 'task_time': row['task_time'], 'task_notes': row['task_notes'] }) else: writer.writerow({ 'task_id': row['task_id'], 'task_date': row['task_date'], 'task_name': row['task_name'], 'task_time': row['task_time'], 'task_notes': row['task_notes'] }) shutil.move('temp.csv', 'tasks.csv') elif self.time: # Update the task time status = "updated" fieldnames = [ 'task_id', 'task_date', 'task_name', 'task_time', 'task_notes' ] with open('tasks.csv') as csvfile, \ open('temp.csv', 'w', newline='') as outputfile: reader = csv.DictReader(csvfile, fieldnames=fieldnames) writer = csv.DictWriter(outputfile, fieldnames=fieldnames) for row in reader: if row['task_id'] == self.results[self.count]['task_id']: writer.writerow({ 'task_id': row['task_id'], 'task_date': row['task_date'], 'task_name': row['task_name'], 'task_time': self.time, 'task_notes': row['task_notes'] }) else: writer.writerow({ 'task_id': row['task_id'], 'task_date': row['task_date'], 'task_name': row['task_name'], 'task_time': row['task_time'], 'task_notes': row['task_notes'] }) shutil.move('temp.csv', 'tasks.csv') elif self.notes or self.notes == '': # Update the task notes status = "updated" fieldnames = [ 'task_id', 'task_date', 'task_name', 'task_time', 'task_notes' ] with open('tasks.csv') as csvfile, \ open('temp.csv', 'w', newline='') as outputfile: reader = csv.DictReader(csvfile, fieldnames=fieldnames) writer = csv.DictWriter(outputfile, fieldnames=fieldnames) for row in reader: if row['task_id'] == self.results[self.count]['task_id']: writer.writerow({ 'task_id': row['task_id'], 'task_date': row['task_date'], 'task_name': row['task_name'], 'task_time': row['task_time'], 'task_notes': self.notes }) else: writer.writerow({ 'task_id': row['task_id'], 'task_date': row['task_date'], 'task_name': row['task_name'], 'task_time': row['task_time'], 'task_notes': row['task_notes'] }) shutil.move('temp.csv', 'tasks.csv') utilities.show_search_title() # Show the sucess message input('\nEntry {} successfully.\n\n' 'Press ENTER to return to the search menu.'.format(status)) from work_log import search_menu search_menu()
def test_search_menu(self, mock_input, mock_date_rang, mock_time_search, mock_exact_search, mock_term_search, mock_date_search, mock_main_menu): work_log.search_menu() self.assertTrue(mock_date_rang.called) work_log.search_menu() self.assertTrue(mock_time_search.called) work_log.search_menu() self.assertTrue(mock_exact_search.called) work_log.search_menu() self.assertTrue(mock_term_search.called) work_log.search_menu() self.assertTrue(mock_date_search.called) work_log.search_menu() self.assertTrue(mock_main_menu.called)