def get_user_task(self, name_user): """Scanned all tasks in file and return list tasks when this user meeting in the attributes 'admins' and 'members' (default file=tasks.txt) :param 'name_user': name selected user :return: list tasks with this user """ user_tasks = [] scanned_task = [] with open(self.path_to_task_file, 'r') as file: for line in file: task = Task() task.load(line) if name_user in task.admins: user_tasks.append(task) elif name_user in task.members: user_tasks.append(task) else: scanned_task.append(line) self.save_scanned_tasks( scanned_task) # return unsuccessful tasks in file return user_tasks
def get_task(self, key_task): """Returns a task with the specified key from a file (default file=tasks.txt). If task not found, return None :param 'key_task': key of the selected tasks :return: object Task or None, if task not found in file """ task = None scanned_tasks = [] with open(self.path_to_task_file, 'r') as file: for line in file: current_task = Task() current_task.load(line) if current_task.key == key_task: task = current_task else: scanned_tasks.append(line) self.check_time(task) self.save_scanned_tasks( scanned_tasks) # return unsuccessful tasks in file return task
def test_create_double_link(self): key_first = self.manager.add_task( Task(None, 'task1', 'andrew', 3, 'waiting', '2018-08-09 10:20', '', '')) key_second = self.manager.add_task( Task(None, 'task2', 'andrew', 3, 'waiting', '2018-08-09 10:20', '', '')) self.manager.add_link(key_first, key_second) self.assertRaisesRegex( Exception, 'Link between tasks N{1} and N{0} already exist'.format( key_first, key_second), self.manager.add_link, key_second, key_first)
def test_delete_2_task(self): key_first = self.manager.add_task( Task(None, 'task1', 'andrew', self.manager.get_free_key_task(), 3, 'waiting', '2018-08-09 10:20', '', '')) key_second = self.manager.add_task( Task(key_first, 'task2', None, self.manager.get_free_key_task(), 3, 'waiting', '2018-08-09 10:20', '', '')) self.manager.delete_task(key_second) task = self.manager.get_task(key_first) self.assertNotIn(key_second, task.subtasks) self.assertIsNone(self.manager.get_task(key_second))
def test_created_link(self): key_first = self.manager.add_task( Task(None, 'task1', 'andrew', 3, 'waiting', '2018-08-09 10:20', '', '')) key_second = self.manager.add_task( Task(None, 'task2', 'andrew', 3, 'waiting', '2018-08-09 10:20', '', '')) self.manager.add_link(key_first, key_second) first = self.manager.get_task(key_first) second = self.manager.get_task(key_second) self.assertIn(key_second, first.links) self.assertIn(key_first, second.links)
def test_exist_task(self): key_second = self.manager.add_task( Task(None, 'task2', 'andrew', self.manager.get_free_key_task(), 3, 'waiting', '2018-08-09 10:20', '', '')) self.assertRaisesRegex(Exception, 'Task N1000 not found in file', self.manager.add_link, key_second, '1000')
def add_task(name_user, key_parent, name_task, status, priority_task, time_start, time_end, period, type_task): # ok try: if time_start: time_start = normal_format_date(time_start) else: time_start = datetime.now().strftime("%H:%M %d/%m/%Y") if time_end: time_end = normal_format_date(time_end) if datetime.strptime(time_start, "%H:%M %d/%m/%Y") >= datetime.strptime( time_end, "%H:%M %d/%m/%Y"): raise Exception("Start time later then end time") else: time_end = "" check_correct_format_period(period) manager = Manager(Config.get_tasks_file(), Config.get_messages_file()) task = Task(key_parent, name_task, name_user, priority_task, status, time_start, time_end, period, type_task) manager.add_task(task) print("Task N{0} was added".format(task.key)) except ValueError as ve: print(ve) except Exception as e: print(e)
def test_removed_link(self): key_first = self.manager.add_task( Task(None, 'task1', 'andrew', self.manager.get_free_key_task(), 3, 'waiting', '2018-08-09 10:20', '', '')) key_second = self.manager.add_task( Task(None, 'task2', 'andrew', self.manager.get_free_key_task(), 3, 'waiting', '2018-08-09 10:20', '', '')) self.manager.add_link(key_first, key_second) self.manager.delete_link(key_first, key_second) first = self.manager.get_task(key_first) second = self.manager.get_task(key_second) self.assertNotIn(key_second, first.links) self.assertNotIn(key_first, second.links)
def test_add_subtask(self): key_parent = self.manager.add_task( Task(None, 'task', 'andrew', 3, 'waiting', '2018-08-09 10:20', '', '')) key_task = self.manager.add_task( Task(key_parent, 'sub', None, 3, 'waiting', '2018-08-09 10:20', '', '')) task = self.manager.get_task(key_task) par = self.manager.get_task(key_parent) self.assertIsNotNone(task) self.assertIsNotNone(par) self.assertEqual(task.parent, par.key) self.assertIn(task.key, par.subtasks) self.assertEqual(task.host, par.host)
def test_change_status_task(self): key_first = self.manager.add_task( Task(None, 'task1', 'andrew', self.manager.get_free_key_task(), 3, 'waiting', '2018-08-09 10:20', '', '')) self.manager.change_status_task(key_first, 'ended') task = self.manager.get_task(key_first) self.assertEqual('ended', task.status)
def test_delete_1_task(self): key_first = self.manager.add_task( Task(None, 'task1', 'andrew', self.manager.get_free_key_task(), 3, 'waiting', '2018-08-09 10:20', '', '')) self.manager.delete_task(key_first) self.assertIsNone(self.manager.get_task(key_first))
def get_keys_tasks(self): """Scanned all tasks in file and return list of task keys (default file=tasks.txt) :return: list keys tasks """ keys_task = [] scanned_task = [] with open(self.path_to_task_file, 'r') as file: for line in file: task = Task() task.load(line) keys_task.append(task.key) scanned_task.append(line) self.save_scanned_tasks( scanned_task) # return unsuccessful tasks in file return keys_task
def test_add_task(self): key_task = self.manager.add_task( Task(None, 'task', 'andrew', 3, 'waiting', '2018-08-09 10:20', '', '')) task = self.manager.get_task(key_task) self.assertEqual(task.key, key_task) self.assertEqual(task.host, 'andrew')
def test_add_member(self): key_first = self.manager.add_task( Task(None, 'task1', 'andrew', self.manager.get_free_key_task(), 3, 'waiting', '2018-08-09 10:20', '', '')) self.manager.add_member_in_task(key_first, 'kot') task = self.manager.get_task(key_first) self.assertIn('kot', task.members)
def test_delete_admin(self): key_first = self.manager.add_task( Task(None, 'task1', 'andrew', self.manager.get_free_key_task(), 3, 'waiting', '2018-08-09 10:20', '', '')) self.manager.add_admin_in_task(key_first, 'kot') self.manager.delete_admin_in_task(key_first, 'kot') task = self.manager.get_task(key_first) self.assertNotIn('kot', task.admins)
def get_all_tasks(self): """Returns all objects tasks of file and clear this (default file=tasks.txt) :return: list objects Tasks of file """ tasks = [] with open(self.path_to_task_file, 'r') as file: for line in file: task = Task() task.load(line) tasks.append(task) with open(self.path_to_task_file, 'w'): pass for task in tasks: self.check_time(task) return tasks
def test_delete_admin_double(self): key_first = self.manager.add_task( Task(None, 'task1', 'andrew', self.manager.get_free_key_task(), 3, 'waiting', '2018-08-09 10:20', '', '')) self.manager.add_admin_in_task(key_first, 'kot') self.manager.delete_admin_in_task(key_first, 'kot') self.assertRaisesRegex( Exception, "Admin {0} not found in task N{1}".format('kot', key_first), self.manager.delete_admin_in_task, key_first, 'kot')
def get_all_users(self): """Scanned all tasks in file and return set name users meeting in the attributes 'admins' and 'members' (default file=tasks.txt) :return: set name users """ set_users = set() scanned_task = [] with open(self.path_to_task_file, 'r') as file: for line in file: task = Task() task.load(line) for user in task.admins: set_users.add(user) for user in task.members: set_users.add(user) scanned_task.append(line) self.save_scanned_tasks( scanned_task) # return unsuccessful tasks in file return set_users
def get_free_key_task(self): """Generate unique key for tasks. Loads all tasks and returns the first random key. Max key counts = 500 (temp.) :return: key in string format or None if count key=500 """ keys = [] try: with open(self.path_to_task_file, 'r') as file: for line in file: current_task = Task() current_task.load(line) keys.append(current_task.key) except: pass while True: if len(keys) == 500: return None key = random.randint(0, 500) if key not in keys: return str(key)
def test_save_task(self): key_first = self.manager.get_free_key_task() key_second = self.manager.get_free_key_task() task = Task(None, 'task', 'andrew', key_first, 3, 'waiting', '2018-08-09 10:20', '', '') task.key = key_first second_task = Task(None, 'task2', 'andrew', key_second, 3, 'waiting', '2018-08-09 10:20', '', '') second_task.key = key_second self.manager.save_task(task) self.manager.save_task(second_task) new_task = self.manager.get_task(key_first) self.assertEqual(new_task.host, 'andrew') self.assertEqual(new_task.key, key_first) self.manager.save_task(new_task)
def test_not_found_parent(self): self.assertRaisesRegex( Exception, 'Task N1000 not found in file', self.manager.add_task, Task('1000', 'sub', None, 3, 'waiting', '2018-08-09 10:20', '', ''))
def check_time(cls, task): """Checks the time of the task relative to the current time and translates it if necessary :param 'task': object type of Task """ if not task: return cur_time = datetime.now().strftime("%H:%M %d/%m/%Y") if task.period: # if this task has end time and period and end time has passed, we will move time this step period # while end time less then current time while datetime.strptime(cur_time, "%H:%M %d/%m/%Y") > datetime.strptime( task.time_last_copy, "%H:%M %d/%m/%Y"): task.time_last_copy = cls.date_translation( task.time_last_copy, task.period) new_task = Task() new_task.name = task.name new_task.parent = task.parent new_task.host = task.host new_task.key = cls.get_free_key_task() new_task.type_task = task.type_task new_task.admins = task.admins.copy() new_task.members = task.members.copy() new_task.priority = task.priority new_task.status = task.status new_task.start_time = task.start_time new_task.end_time = task.end_time new_task.period = '' cls.save_task(new_task)