def detail(request, task_id): current_user = request.user controller = TaskController(current_user.id) comment_controller = CommentController(current_user.id) task = controller.get_task_by_id(task_id=task_id) comment_list = comment_controller.get_task_comments(task_id=task_id) user_list = [] for user in task['users']: user_list.append(User.objects.get(id=user)) owner = task['creator'] try: owner = User.objects.get(id=owner) except: raise Exception() print(owner) return render( request, 'tasks/detail.html', { 'task': task, 'comment_list': comment_list, 'user_list': user_list, 'owner': owner })
def edit(request, task_id): # if this is a POST request we need to process the form data current_user = request.user controller = TaskController(current_user.id) task = controller.get_task_by_id(task_id) if request.method == 'POST': # create a form instance and populate it with data from the request: form = TaskForm(request.POST) # check whether it's valid: if form.is_valid(): data = form.cleaned_data controller.edit_task( task_id, { 'title': data['title'], 'text': data['text'], 'status': data['status'], 'tags': data['tags'] }) return HttpResponseRedirect('/tasks') # if a GET (or any other method) we'll create a blank form else: form = TaskForm(data=task) return render(request, 'tasks/edit-task.html', { 'form': form, 'task': task })
def comment(request, task_id): current_user = request.user controller = TaskController(current_user.id) comment_controller = CommentController(current_user.id) controller.get_task_by_id(task_id=task_id) comment = comment_controller.create_task_comment( task_id=task_id, text=request.POST.get('text')) return HttpResponseRedirect('/tasks/{}/'.format(task_id))
def toggle_task_completion(request, task_id): current_user = request.user controller = TaskController(current_user.id) task = controller.get_task_by_id(task_id) if task['status'] == TaskStatus.PLANNED.value: new_status = TaskStatus.COMPLETED.value else: new_status = TaskStatus.PLANNED.value controller.edit_task(task_id=task_id, edited_task={'status': str(new_status)}) return JsonResponse({'success': True})
def share_permission(request, task_id): current_user = request.user try: user = User.objects.get(username=request.POST.get('user')) except User.DoesNotExist: return JsonResponse({ 'success': False, 'message': 'User not found', 'url': '/tasks/{}/'.format(task_id), }) controller = TaskController(current_user.id) controller.share_permission(new_user_id=user.id, task_id=task_id) return JsonResponse({ 'success': True, 'url': '/tasks/{}/'.format(task_id), })
def index(request): current_user = request.user controller = TaskController(current_user.id) tasks = controller.get_tasks() tags = set() for task in tasks: if task['tags']: tags.update(map(lambda tag: tag.strip(), task['tags'].split(','))) for task in tasks: task['subtasks'] = controller.get_task_subtasks(task['id']) if request.is_ajax(): active_tag = request.GET.get('active_tag') req_period = request.GET.get('period') if active_tag: if active_tag != 'all': tasks = controller.get_tasks_by_tag(active_tag) return render(request, 'tasks/tasks-list.html', { 'tasks': tasks, 'tags': tags }) if req_period: res_tasks = [] result_tasks = [] date_tasks_list = controller.get_tasks_on_period( periods[req_period][0].strftime('%d/%m/%y'), periods[req_period][1].strftime('%d/%m/%y')) #get all tasks for date_tasks in date_tasks_list: date = list(date_tasks.keys())[0] for task in date_tasks[date]: res_tasks.append(task) #remove duplicate for res_task in res_tasks: is_dublicate = False for res in result_tasks: if res_task['id'] == res['id']: is_dublicate = True if not is_dublicate: result_tasks.append(res_task) else: is_dublicate = False for task in result_tasks: task['subtasks'] = controller.get_task_subtasks(task['id']) return render(request, 'tasks/tasks-list.html', { 'tasks': result_tasks, 'tags': tags }) return render(request, 'tasks/index.html', {'tasks': tasks, 'tags': tags})
def create_subtask(request, task_id): current_user = request.user controller = TaskController(current_user.id) if request.method == 'POST': form = TaskForm(request.POST) if form.is_valid(): data = form.cleaned_data if data['period'] and data['start_date']: controller.create_periodic_task( data['title'], data['text'], data['status'], tags=data['tags'], start_date=data['start_date'].strftime('%d/%m/%y %H:%M') if data['start_date'] else None, deadline=data['date'].strftime('%d/%m/%y %H:%M') if data['date'] else None, period=data['period'], parent_id=task_id) else: controller.create_task( data['title'], data['text'], data['status'], tags=data['tags'], date=data['date'].strftime('%d/%m/%y %H:%M') if data['date'] else None, parent_id=task_id) return HttpResponseRedirect('/tasks') else: form = TaskForm() return render(request, 'tasks/create-subtask.html', { 'form': form, 'task_id': task_id })
def create(request): current_user = request.user controller = TaskController(current_user.id) if request.method == 'POST': form = TaskForm(request.POST) if form.is_valid(): data = form.cleaned_data if data['period'] and data['start_date']: controller.create_periodic_task( data['title'], data['text'], data['status'], tags=data['tags'], start_date=data['start_date'].strftime('%d/%m/%y %H:%M') if data['start_date'] else None, deadline=data['date'].strftime('%d/%m/%y %H:%M') if data['date'] else None, period=data['period']) else: controller.create_task( data['title'], data['text'], data['status'], tags=data['tags'], date=data['date'].strftime('%d/%m/%y %H:%M') if data['date'] else None, ) return HttpResponseRedirect('/tasks') # if a GET (or any other method) we'll create a blank form else: form = TaskForm() return render(request, 'tasks/task-create.html', {'form': form})
class CommentControllerTest(unittest.TestCase): def setUp(self): self.controller = CommentController(user_id=1) self.task_controller = TaskController(user_id=1) def test_get_comment(self): task = {'title': '__test__ title', 'text': '__test__ text', 'status': 1} created_task = self.task_controller.create_task(task['title'], task['text'], task['status']) self.controller.create_task_comment(task_id=created_task.id, text="TEXT") comments = self.controller.get_task_comments(task_id=created_task.id) self.assertIn("TEXT", comments[0]['text']) self.task_controller.delete_task(created_task.id) def test_create_comment(self): task = {'title': '__test__ title', 'text': '__test__ text', 'status': 1} created_task = self.task_controller.create_task(task['title'], task['text'], task['status']) comment = self.controller.create_task_comment(task_id=created_task.id, text="lala") comments = self.controller.get_task_comments(task_id=created_task.id) self.assertIsInstance(comments, list) self.task_controller.delete_task(created_task.id) def test_check_user_commentw(self): with self.assertRaises(errs.CommentNotFoundError): self.controller.check_user_in_comment(user_id=1, comment_id=1777)
def setUp(self): self.controller = TaskController(user_id=1)
class TaskControllerTestCase(unittest.TestCase): def setUp(self): self.controller = TaskController(user_id=1) def test_create_task(self): task = { 'title': '__test__ title', 'text': '__test__ text', 'status': 1 } created_task = self.controller.create_task(task['title'], task['text'], task['status']) self.assertEqual(task['title'], created_task.title) self.assertEqual(task['text'], created_task.text) self.assertEqual(task['status'], created_task.status) self.controller.delete_task(created_task.id) def test_create_periodic_task(self): task = { 'title': '__test__ title', 'text': '__test__ text', 'status': 1, 'start_date': '31/12/98 00:00', 'deadline': '31/12/02 00:00', 'period': '* * * * *', 'tags': "test_love" } created_task = self.controller.create_periodic_task( title=task['title'], text=task['text'], status=task['status'], start_date=task['start_date'], deadline=task['deadline'], period=task['period'], tags=task['tags']) self.assertEqual(task['title'], created_task.title) self.assertEqual(task['text'], created_task.text) self.assertEqual(task['status'], created_task.status) self.assertEqual(task['period'], created_task.period) def test_check_permisson(self): with self.assertRaises(errs.TaskNotExistError): self.controller.check_permission(user_id=45, task_id=1111) def test_get_task_by_id(self): task = { 'title': '__test__ title', 'text': '__test__ text', 'status': 1 } created_task = self.controller.create_task(task['title'], task['text'], task['status']) task = self.controller.get_task_by_id(task_id=created_task.id) self.assertEqual(task['id'], created_task.id) self.controller.delete_task(created_task.id) def test_edit_title(self): ###########3 task = { 'title': '__test__ title', 'text': '__test__ text', 'status': 1 } created_task = self.controller.create_task(task['title'], task['text'], task['status']) task = self.controller.edit_task(created_task.id, {'title': 'NEW'}) self.assertNotEqual(created_task.title, "NEW") self.controller.delete_task(created_task.id) def test_edit_text(self): task = { 'title': '__test__ title', 'text': '__test__ text', 'status': 1 } created_task = self.controller.create_task(task['title'], task['text'], task['status']) task = self.controller.edit_task(created_task.id, {'text': "lol"}) self.assertNotEqual(created_task.status, "RARA") self.controller.delete_task(created_task.id) def test_get_subtasks(self): task = { 'title': '__test__ title', 'text': '__test__ text', 'status': 1 } created_task = self.controller.create_task(task['title'], task['text'], task['status']) subtask = self.controller.create_task(task['title'], task['text'], task['status'], parent_id=created_task.id) task_subtasks = self.controller.get_task_subtasks( task_id=created_task.id) self.assertIsNotNone(task_subtasks) def test_share_permission(self): task = { 'title': '__11test__ title', 'text': '__test__ text', 'status': 1 } created_task = self.controller.create_task(task['title'], task['text'], task['status']) self.controller.share_permission(task_id=created_task.id, new_user_id=1) self.assertEqual([1], created_task.users) def test_get_by_tag(self): task = { 'title': '__11test__ title', 'text': '__test__ text', 'status': 1, 'tag': 'life' } created_task = self.controller.create_task(task['title'], task['text'], task['status'], tags=task['tag']) tasks = self.controller.get_tasks_by_tag("life") self.assertIsNotNone(task) def test_task_parameters_type(self): task = { 'title': '__11test__ title', 'text': '__test__ text', 'status': 1, 'tag': 'life' } created_task = self.controller.create_task(task['title'], task['text'], task['status'], tags=task['tag']) self.assertIsInstance(created_task.title, str) self.assertIsInstance(created_task.text, str) self.assertIsInstance(created_task.status, int) self.assertIsInstance(created_task.tags, str) self.controller.delete_task(created_task.id) def test_task_type(self): tasks = self.controller.get_tasks() self.assertIsInstance(tasks, list) def test_cron_string_periodic_task(self): with self.assertRaises(errs.CronValueError): self.controller.create_periodic_task(title="title", text="text,", status=1, start_date="01/02/12 00:00", deadline="02/02/12 00:00", period="* * help", parent_id=666) def test_parent_id_periodic_task(self): with self.assertRaises(errs.TaskWithParentIdNotExistError): self.controller.create_periodic_task(title="title", text="text,", status=1, start_date="01/02/12 00:00", deadline="02/02/12 00:00", period="* * * * *", parent_id=666) def test_delete_task(self): with self.assertRaises(errs.TaskNotExistError): self.controller.delete_task(task_id=89) def test_share_permission_exception(self): with self.assertRaises(errs.TaskNotExistError): self.controller.share_permission(new_user_id=1, task_id=666)
class TaskView: def __init__(self): self.user_controller = UserController() self.controller = TaskController( self.user_controller.user_id(get_token())) def create_new_task(self, args): try: self.controller.create_task(args.title, args.text, args.status, args.tags, args.parent_id, args.date) except (errs.TaskNotExistError, errs.TaskWithParentIdNotExistError) as e: errs_help.console_print(e) except ValueError: print("Not valid date") def create_new_periodic_task(self, args): try: self.controller.create_periodic_task(args.title, args.text, args.status, args.start_date, args.date, args.period, args.tags, args.parent_id) except (errs.TaskWithParentIdNotExistError, errs.TaskNotExistError, errs.CronValueError) as e: errs_help.console_print(e) # except ValueError: # print("Not valid date") def delete_task(self, args): try: self.controller.delete_task(args.task_id) except (errs.AccessError, errs.TitleError, errs.TaskNotExistError, errs.UserNotHaveAccessToTaskError) as e: errs_help.console_print(e) def get_tasks(self, args): tasks = self.controller.get_tasks() for task in tasks: task_helper.print_task(task) self.__print_subtasks(task['subtasks'], 2) def get_task_by_id(self, args): try: task = self.controller.get_task_by_id(args.task_id) task_helper.print_task(task) except (errs.TaskNotExistError, errs.AccessError) as e: errs_help.console_print(e) def edit_task(self, args): try: self.controller.edit_task(args.task_id, {args.parameter: args.new_parameter}) except (errs.AccessError, errs.CronValueError, errs.TaskNotExistError, errs.StatusValueError, errs.IncorrectDateValueError) as e: errs_help.console_print(e) def get_tasks_by_tag(self, args): try: tasks = self.controller.get_tasks_by_tag(args.tag) for task in tasks: # print(str(task.id) + "--id--" + " " + task.title +" " + task.text + " " + str(task.status)) task_helper.print_task(task) except (errs.TaskNotExistError, errs.TitleError) as e: errs_help.console_print(e) def __print_subtasks(self, subtasks, offset=0): for subtask in subtasks: task_helper.print_task(subtask, offset) self.__print_subtasks(subtask['subtasks'], offset + 2) def get_task_subtasks(self, args): try: subtasks = self.controller.get_task_subtasks(args.task_id) self.__print_subtasks(subtasks) except (errs.TaskNotExistError, errs.AccessError, errs.NoSubtaskError) as e: errs_help.console_print(e) def share_task_permission(self, args): try: self.controller.share_permission(args.new_user_id, args.task_id) except (errs.TaskNotExistError, errs.TitleError, errs.AccessError) as e: errs_help.console_print(e) def get_tasks_on_period(self, args): dates_tasks = self.controller.get_tasks_on_period(args.start, args.end) for date_tasks in dates_tasks: date = list(date_tasks.keys())[0] print(date) for task in date_tasks[date]: task_helper.print_task(task)
def __init__(self): self.user_controller = UserController() self.controller = TaskController( self.user_controller.user_id(get_token()))
def delete_permission(request, task_id): current_user = request.user controller = TaskController(current_user.id) controller.delete_permission(task_id, int(request.POST.get('user_id'))) return JsonResponse({'success': True, 'url': request.POST.get('redirect')})
def delete(request, task_id): current_user = request.user controller = TaskController(current_user.id) controller.delete_task(task_id=task_id) return HttpResponseRedirect(redirect_to='/tasks')
def setUp(self): self.controller = CommentController(user_id=1) self.task_controller = TaskController(user_id=1)