def create(self, request): """Creating a task for a user""" print ">>> User:"******">>> Token:", request.auth, request.current_user serializer = TodoSerializer(data=request.data) serializer.initial_data["author"] = request.user if serializer.is_valid(): post = Todo() post.description = serializer.data["description"] post.author = serializer.data["author"] if "due_at" in serializer.data and serializer.data["due_at"]: post.due_at = convert_time(serializer.data["due_at"]) else: today = datetime.datetime.now() _date = (today - datetime.timedelta(days=settings.DEFAULT_DAYS)) post.due_at = _date post.save() serializer = TodoSerializer(post) return Response(serializer.data, status=status.HTTP_201_CREATED) else: msg = { "error": 400, "message": serializer.errors } return Response(msg, status=status.HTTP_400_BAD_REQUEST)
def create(self, request): """Creating a task for a user""" print ">>> User:"******">>> Token:", request.auth, request.current_user serializer = TodoSerializer(data=request.data) serializer.initial_data["author"] = request.user if serializer.is_valid(): post = Todo() post.description = serializer.data["description"] post.author = serializer.data["author"] if "due_at" in serializer.data and serializer.data["due_at"]: post.due_at = convert_time(serializer.data["due_at"]) else: today = datetime.datetime.now() _date = (today - datetime.timedelta(days=settings.DEFAULT_DAYS)) post.due_at = _date post.save() serializer = TodoSerializer(post) return Response(serializer.data, status=status.HTTP_201_CREATED) else: msg = {"error": 400, "message": serializer.errors} return Response(msg, status=status.HTTP_400_BAD_REQUEST)
def test_delete(self): todo_data = {"title": "task 1", "description": "description"} todo = Todo(**todo_data) todo.save() response = self.client.delete(f"/todos/{todo.pk}/") self.assertEqual(response.status_code, 204) self.assertFalse(Todo.objects.filter(**todo_data).exists())
def todo_create(request): if(request.method == 'POST'): title = request.POST['title'] description = request.POST['description'] priority = request.POST['priority'] todo = Todo(title = title, description = description) todo.save() return redirect('/todos') return render(request, 'todo/todo_create.html', {"active_menu": "todo"})
def todo_add (request): if (request.method == 'POST'): title = request.POST['title'] priority = request.POST['priority'] todo = Todo (title=title, priority=priority) todo.save() return redirect('/todos') else: return render(request, "todo/todo_add.html", {"active_menu": "todo"})
def test_get(self): todo_data = {"title": "task 1", "description": "description"} todo = Todo(**todo_data) todo.save() response = self.client.get("/todos/") self.assertEqual(response.status_code, 200) todo_result = response.data['results'][0] self.assertEqual(todo_result.get("title"), todo.title) self.assertEqual(todo_result.get("description"), todo.description)
def create_todo(request): form = TodoForm(request.POST) if form.is_valid(): todo = Todo(title=form.cleaned_data['title'], description=form.cleaned_data['description'], is_done=False) todo.save() return redirect('todos index') return index(request, form)
def create(req): if req.method == 'GET': form = ToDoForm(req.POST) context = {'form': form} return render(req, 'todo_app/create.html', context) else: form = ToDoForm(req.POST) if form.is_valid(): todo = Todo(title=form.cleaned_data['title'], description=form.cleaned_data['description']) todo.save() return redirect('index') return redirect('create_todo')
def create(request): if request.method == 'POST': form = TodoForm(request.POST) if form.is_valid(): todo = Todo(title=form.cleaned_data['title'], description=form.cleaned_data['description'], is_done=False) todo.save() return redirect('all_todos') else: form = TodoForm() context = {'form': form} return render(request, 'todo_app/create.html', context)
def create(request): form = TodoForm(request.POST) if form.is_valid(): title = form.cleaned_data['title'] description = form.cleaned_data['description'] todo = Todo( title=title, description=description) todo.save() return redirect('index') context = { 'form': form, } return render(request, 'create_template.html', context)
def post(self, request): """ add new todo-note """ serializer = TodoSerializer(data=request.data) if not serializer.is_valid(): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) data = serializer.data todo_obj = Todo(description=data['description'], is_done=False) todo_obj.save() request.data['id'] = todo_obj.id return Response(request.data, status=status.HTTP_201_CREATED)
def put(self, request, todo_id): """ update already saved todo note """ serializer = TodoSerializer(data=request.data) if not serializer.is_valid(): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) data = serializer.data desc = data['description'] is_done = data['is_done'] todo_obj = Todo(id=todo_id, description=desc, is_done=is_done, updated=datetime.now()) todo_obj.save() return Response(status=status.HTTP_200_OK)
def index(request, obj=None): todos = Todo.objects.all() if request.method == 'POST': form = TodoInput(request.POST) if form.is_valid(): todo = Todo(title=request.POST['title'], description=request.POST['description']) todo.save() else: if obj: initial_value = { 'title': obj.title, 'description': obj.description } else: initial_value = None form = TodoInput(initial=initial_value) context = {'todos': todos, 'form': form} return render(request, 'todo/index.html', context)
def update(request, todo_id): if request.method == 'POST': try: todo = Todo.objects.get(pk=todo_id) except ObjectDoesNotExist: return redirect('index') form = TodoInput(request.POST) if form.is_valid(): form.cleaned_data.update({'id': todo_id}) todo = Todo(**form.cleaned_data) todo.save() return redirect('index') else: try: todo = Todo.objects.get(pk=todo_id) except ObjectDoesNotExist: return redirect('index') return index(request, todo)
def handle(self, *args, **kwargs): internet = Internet() text = Text() total = kwargs['total'] for i in range(total): project = Project(title=text.title(), repository=internet.home_page(), text=text.words(quantity=5)) project.save() random_user_list = [] for _ in range(random.randint(1, 5)): random_user_list.append( str(random.choice(User.objects.values_list('uuid'))[0])) project.users.set(random_user_list) for _ in range(random.randint(1, 5)): todo = Todo( todo=text.title(), text=text.text(quantity=2), project=project, user=User.objects.get(uuid=random.choice( User.objects.values_list('uuid'))[0]), ) todo.save()
def add_todo(request): text=request.POST.get('text') date_added=timezone.now() todo=Todo(date_added=date_added,text=text) todo.save() return HttpResponseRedirect('/')