def post(self, request, *args, **kwargs): data = request.data data["user"] = self.request.user serializer = TodoSerializer(data=data) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED)
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 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)