def main_menu_loop(): main_menu = ['Add Log', 'Search Logs', 'Exit Program'] while True: clear() nav = menu(main_menu) if nav == 'Add Log': date = get_date() employee_name = get_text('employee name', optional=False) task_name = get_text('task name', optional=False) time_spent = get_time_spent() note = get_text('note', optional=True) Log.create(date=date, employee_name=employee_name, task_name=task_name, time_spent=time_spent, note=note) input("\nLog Created! Press any key to continue.") if nav == 'Search Logs': search_loop() if nav == 'Exit Program': break
def test_view_logs_loop_and_edit_all(self, input): from work_logs_sqlite import view_logs_loop logs = Log.select() view_logs_loop(logs) logs = Log.select() self.assertEqual(logs[0].date, '03/03/3000') self.assertEqual(logs[0].employee_name, 'Gamma') self.assertEqual(logs[0].task_name, 'Task Three') self.assertEqual(logs[0].time_spent, 3) self.assertEqual(logs[0].note, 'Thryee')
def get_distinct_names(search_term=None): """Returns a list of distinct employee names in the db.""" if search_term: query = Log.select(Log.employee_name).distinct().where( Log.employee_name.contains(search_term)).order_by( Log.employee_name) else: query = Log.select(Log.employee_name).distinct().order_by( Log.employee_name) return [log.employee_name for log in query]
def test_main_menu_add_log(self, input): from work_logs_sqlite import main_menu_loop main_menu_loop() logs = Log.select() self.assertEqual(len(logs), 3) self.assertEqual(logs[2].date, '03/03/3000') self.assertEqual(logs[2].employee_name, 'Gamma') self.assertEqual(logs[2].task_name, 'Task Three') self.assertEqual(logs[2].time_spent, 3) self.assertEqual(logs[2].note, 'Thryee')
def find_by_date_range(start, end): """Searches for work logs by dates within a specified range""" start_date = datetime.datetime.strptime(start, '%m/%d/%Y') end_date = datetime.datetime.strptime(end, '%m/%d/%Y') # Create a timedelta with a value of 0 (for the range's minimum) and # calculate a timedelta that represents the maximum value allowed # by the search (end_date - start_date) range_start = datetime.timedelta(0) range_max = end_date - start_date # Return a list of logs that have dates falling within the search # range. logs = Log.select() search_results = [] for log in logs: date = end_date - datetime.datetime.strptime(log.date, "%m/%d/%Y") if date >= range_start and date <= range_max: search_results.append(log) return search_results
def setUp(self): # Bind model class to test db. Log.bind(test_db, bind_refs=False, bind_backrefs=False) # Connect to in-memory test database and create Log table test_db.connect() test_db.create_tables([Log]) Log.create(date='01/01/2000', employee_name='Alpha', task_name='Task One', time_spent=1, note='Won') Log.create(date='01/02/2000', employee_name='Beta', task_name='Task Two', time_spent=2, note='Too')
def find_by_time_spent(search_time): """Find by time spent.""" return Log.select().where(Log.time_spent == int(search_time))
def find_by_name(search_term): """Find by name.""" names = get_distinct_names(search_term) print("Matches for search: ") chosen_name = menu(names) return Log.select().where(Log.employee_name == chosen_name)
def find_by_employee(): """Search by a list of employees.""" chosen_name = menu(get_distinct_names()) return Log.select().where(Log.employee_name == chosen_name)
def find_by_date(dates): """Find by date""" chosen_date = menu(dates) return Log.select().where(Log.date == chosen_date)
def test_view_logs_loop_and_edit_date(self, input): from work_logs_sqlite import view_logs_loop logs = Log.select() view_logs_loop(logs) self.assertEqual(Log.select()[0].date, '03/03/3000')
def test_view_logs_loop_and_edit_note(self, input): from work_logs_sqlite import view_logs_loop logs = Log.select() view_logs_loop(logs) self.assertEqual(Log.select()[0].note, 'Thryee')
def test_view_logs_loop_and_edit_time_spent(self, input): from work_logs_sqlite import view_logs_loop logs = Log.select() view_logs_loop(logs) self.assertEqual(Log.select()[0].time_spent, 3)
def test_view_logs_loop_and_edit_task_name(self, input): from work_logs_sqlite import view_logs_loop logs = Log.select() view_logs_loop(logs) self.assertEqual(Log.select()[0].task_name, 'Task Three')
def test_view_logs_loop_and_edit_employee_name(self, input): from work_logs_sqlite import view_logs_loop logs = Log.select() view_logs_loop(logs) self.assertEqual(Log.select()[0].employee_name, 'Gamma')
def find_by_string(search_term): """Find by search term.""" logs = Log.select() return (logs.where(Log.task_name.contains(search_term)) or logs.where(Log.note.contains(search_term)))
def get_distinct_dates(): """Returns a list of distinct dates in the db.""" query = Log.select(Log.date).distinct() dates = [log.date for log in query] return sorted(dates, key=sorting)
def test_view_logs_loop_delete_disconfirm(self, input): from work_logs_sqlite import view_logs_loop logs = Log.select() view_logs_loop(logs) assert len(Log.select()) == 2