Esempio n. 1
0
    def post(self, request, *args, **kwargs):
        user = request.user
        values = request.data
        values['user'] = user.pk

        serializer = TodoSerializer(data=values)
        serializer.is_valid(raise_exception=True)
        todo = serializer.save()

        return Response({'todo': TodoSerializer(todo, context=self.get_serializer_context()).data})
Esempio n. 2
0
    def put(self, request, pk):
        """
        Handle PUT request for the `/todo/` endpoint.

        Retrieves a `todo` instance based on the primary key contained
        in the URL. Then takes the `data` property from the `request` object
        to update the relevant `todo` instance.

        Returns the updated object if the update was successful, otherwise
        400 (bad request) is returned
        """
        todo = Todo.objects.get(id=pk)
        serializer = TodoSerializer(todo, data=request.data)

        # Check to see if the data in the `request` is valid.
        # If the data cannot be deserialised into a Todo object then
        # a bad request response will be returned containing the error.
        # Else, save and return the data.
        if not serializer.is_valid():
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

        else:
            serializer.save()
            return Response(serializer.data)
Esempio n. 3
0
    def post(self, request):
        """
        Handle the POST request for the `/todo/` endpoint.

        This view will take the `data` property from the `request` object,
        deserialize it into a `Todo` object and store in the DB.

        Returns a 201 (successfully created) if the item is successfully
        created, otherwise returns a 400 (bad request)
        """
        serializer = TodoSerializer(data=request.data)
        # Check to see if the data in the `request` is valid.
        # If the cannot be deserialized into a
        # a bad request respose will be returned containing the error.
        # Else, save the data and return the data and a successfully
        # created status
        if not serializer.is_valid():
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
        else:
            data = serializer.data
            #Get the `user` based on the username contained in the data
            user = User.objects.get(username=data["username"])
            #create the new todo item
            Todo.objects.create(user=user,
                                title=data["title"],
                                description=data["description"],
                                status=data["status"])
            return Response(serializer.data, status=status.HTTP_201_CREATED)
Esempio n. 4
0
    def put(self, request, pk):
        """
        Handle PUT request for the '/todo/' endpoint

        Retrieve a todo object based on the priimary key contained
        in the URL. Then takes the 'data' property from the 'request' object
        to update the relevant 'todo' instance

        Returns the updated object if the update was successful,
        otherwise 400 (bad request) is returned
        :param request:
        :param pk:
        :return: todo object or 400 response
        """
        todo = Todo.objects.get(id=pk)
        serializer = TodoSerializer(todo, data=request.data)

        #   Check to see if the data in the 'request' is valid
        #   If it cannot be deserialized into a Todo object then
        #   a bad request response will be returned containing the
        #   error. Else, save and return the data
        if not serializer.is_valid():
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
        else:
            # Get the `user` based on the request data (not in the serializer)
            # user = User.objects.get(username=request.data["username"])
            # Get the todo item data from the serializer
            # data = serializer.data
            # Create the new `todo` item
            # Todo.objects.create(user=user, title=data["title"],
            #                    description=data["description"], status=data["status"])
            #   return Response(serializer.data,status=status.HTTP_201_CREATED)
            serializer.save()
            return Response(serializer.data)
Esempio n. 5
0
 def post(self, request, format=None):
     request.data['useruid'] = self.request.user.id
     serializer = TodoSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 6
0
    def post(self, request):
        """
        HANDLE the POST request for the 'todo' endpoint

        This view will take the 'data' property from the 'request' object
        and deserialize it into a Todo object and store it in the DB

        Returns a 201 (Successfully created)
        if the item is successfully
        created, otherwise returns a 400 (bad request)
        :param request:
        :return:
        """
        serializer = TodoSerializer(data=request.data)

        #   Check to see if the data in the request is valid
        #   If it cannot be desrialized into a Todo object then
        #   a bad request response will be returned containing the error
        #   Else, save the data and return the data successfully
        #   created status
        if not serializer.is_valid():
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
        else:
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
Esempio n. 7
0
    def post(self, request):
        """
        Handle the POST request for the "/todo/" endpoint

        This view will take the "data" property from the "request" object,
        deserialize it into a "Todo" object and store it in the DB.

        Returns a 201 (successfully created) if the item is successfully
        created, otherwise returns a 400 (bad request)
        """
        serializer = TodoSerializer(data=request.data)

        # Check to see if the data in thte "request" is valid.
        # If the data cannot be deserialized into a Todo object then
        # a bad request response will be returned containing the error.
        # Else, save the data and return the data and a successfully
        # created status
        if not serializer.is_valid():
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
        else:
            # Get the "user" based on the request data (not in the serializer)
            user = User.objects.get(username=request.data["username"])
            # Get the todo item data from the serializer
            data = serializer.data
            # Create the new "todo" item
            Todo.objects.create(user=user,
                                title=data["title"],
                                description=data["description"],
                                status=data["status"])
            return Response(serializer.data, status=status.HTTP_201_CREATED)
    def post(self, request):
        """
        Handle the POST request for the `/todo/` endpoint.

        This view will take the `data` property from the `request` object,
        deserialize it into a `Todo` object and store in the DB

        Returns a 201 (successfully created) if the item is successfully
        created, otherwise returns a 400 (bad request)
        """
        serializer = TodoSerializer(data=request.data)

        # Check to see if the data in the `request` is valid.
        # If the cannot be deserialized into a Todo object then
        # a bad request respose will be returned containing the error.
        # Else, save the data and return the data and a successfully
        # created status
        if not serializer.is_valid():
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
        else:
            # Get the `user` based on the request data (not in the serializer)
            user = User.objects.get(username=request.data["username"])

            # Get the todo item data from the serializer
            data = serializer.data
            # Create the new `todo` item
            Todo.objects.create(user=user,
                                title=data["title"],
                                description=data["description"],
                                status=data["status"])
            return Response(serializer.data,
                            status=status.HTTP_201_CREATED)
Esempio n. 9
0
 def post(self, request, pk):
     serializer = TodoSerializer(data=request.data)
     if not serializer.is_valid():
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
     else:
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
Esempio n. 10
0
 def post(self, request, format=None):
     serializer = TodoSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()  # creating new todo task with the POST data
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     else:
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
Esempio n. 11
0
 def put(self, request, pk):
     todo = self.get_object(pk)
     serializer = TodoSerializer(todo, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return TodoSerializer(serializer.data,
                               status=status.HTTP_201_CREATED)
     return TodoSerializer(serializer.errors,
                           status=status.HTTP_400_BAD_REQUEST)
Esempio n. 12
0
 def put(self, request, pk, format=None):
     todo = self.get_object(pk)
     request.data['useruid'] = self.request.user.id
     serializer = TodoSerializer(todo, data=request.data, partial=True)
     if serializer.is_valid():
         if self.request.user.id == todo.useruid:
             serializer.save()
             return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 13
0
 def post(self,request):
   if(request.data['title']==''or request.data['description']==''):
     data={'title':["This field may not be blank."],'description':["This field may not be blank."]}
     return JsonResponse(data)
   serialize=TodoSerializer(data=request.data)
   if(serialize.is_valid()):
       serialize.save()
       return Response(serialize.data, status=status.HTTP_201_CREATED)
   return Response(serialize.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 14
0
    def put(self, request, pk):
        todo = Todo.objects.get(id=pk)
        serializer = TodoSerializer(todo, data=request.data)

        if not serializer.is_valid():
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
        else:
            serializer.save()
            return Response(serializer.data)
Esempio n. 15
0
 def delete(self, request, todo_id):
     """ Update a todo """
     serializer = TodoSerializer(data=request.DATA)
     if not serializer.is_valid():
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
     else:
         data = serializer.data
         t = Todo.objects.get(id=todo_id)
         t.delete()
         return Response(status=status.HTTP_200_OK)
Esempio n. 16
0
 def put(self,request,todo_id):
     serializer = TodoSerializer(data=request.DATA)
     if not serializer.is_valid():
        return Response(serializer.errors, status = status.HTTP_400_BAD_REQUEST)
     else:
        data = serializer.data
        desc = data['description']
        done = data['done']
        t = Todo(id=todo_id,owner=request.user,description=desc,done=done,updated=datetime.now())
        t.save()
        return Response(status=status.HTTP_200_OK)
Esempio n. 17
0
 def post(self,request):
     serializer = TodoSerializer(data = request.DATA)
     if not serializer.is_valid():
        return Response(serializer.errors, status = status.HTTP_400_BAD_REQUEST)
     else:
        data = serializer.data
        owner = request.user
        t = Todo(owner=request.user, description=data['description'],done=False)
        t.save()
        request.DATA['id'] = t.pk;
        return Response(request.DATA,status=status.HTTP_201_CREATED)
Esempio n. 18
0
    def put(self, request, pk):
        try:
            todo = Todo.objects.get(pk=pk)
        except Todo.DoesNotExist as e:
            return Response(status=status.HTTP_404_NOT_FOUND)

        serializer = TodoSerializer(todo, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 19
0
 def post(self, request):
     serializer = TodoSerializer(data=request.DATA)
     if not serializer.is_valid():
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
     else:
         data = serializer.data
         owner = request.user
         t = Todo(owner=owner, description=data['description'], done=False)
         t.save()
         request.DATA['id'] = t.pk  # return id
         return Response(request.DATA, status=status.HTTP_201_CREATED)
Esempio n. 20
0
def todo_list(request):
    if request.method == 'GET':
        todos = Todo.objects.all()
        serializer = TodoSerializer(todos, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = TodoSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 21
0
  def patch(self,request,id):
    try:

      obj=Todo.objects.get(id=id)

      serialize=TodoSerializer(obj,data=request.data,partial=True)
      if(serialize.is_valid()):
          serialize.save()
          return Response(serialize.data, status=status.HTTP_200_OK)
    except Todo.DoesNotExist:
      data={"detail": "Not found."}
      return Response(data,status=404)
Esempio n. 22
0
    def put(self, request, pk):
        try:
            todo = Todo.objects.get(pk=pk)
        except Todo.DoesNotExist as e:
            return Response(status=status.HTTP_404_NOT_FOUND)

        serializer = TodoSerializer(todo, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 23
0
def todo_list(request):
    if request.method == 'GET':
        todo = Todo.objects.all()
        serializer = TodoSerializer(todo, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = TodoSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)
Esempio n. 24
0
def list_create_todo(request):
    if request.method == 'GET':
        todo = Todo.objects.order_by('-created_at')
        serializer=TodoSerializer(todo, many=True, context={'request': request})
        return Response(serializer.data)

    elif request.method == 'POST':

        serializer = TodoSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 25
0
 def put(self, request, todo_id):
     serializer = TodoSerializer(data=request.DATA)
     if not serializer.is_valid():
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
     else:
         data = serializer.data
         desc = data['description']
         done = data['done']
         t = Todo(id=todo_id, owner=request.user, description=desc,\
                  done=done, updated=datetime.now())
         t.save()
         return Response(status=status.HTTP_200_OK)
Esempio n. 26
0
	def post(self, request):
		""" Adding a new todo """
		serializer = TodoSerializer(data=request.data)
		if not serializer.is_valid():
			return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

		else:
			data = request.data
			# owner = request.user
			owner = User.objects.first()
			t = Todo.objects.create(owner=owner, description=data['description'], done=False)
			t.save()
			# data['id'] = t.pk
			return Response(request.data, status=status.HTTP_201_CREATED)
Esempio n. 27
0
 def put(self, request, pk, format=None):
     """
     updating task with respect to the new data
     """
     task = get_object_or_404(Todo, pk=pk)
     if request.user == task.user:
         serializer = TodoSerializer(task, data=request.data)
         if serializer.is_valid():
             serializer.save()
             return Response(serializer.data)
         else:
             return Response(serializer.errors,
                             status=status.HTTP_400_BAD_REQUEST)
     else:
         return Response(status=status.HTTP_403_FORBIDDEN)
Esempio n. 28
0
def list_create_todo(request):
    if request.method == 'GET':
        todo = Todo.objects.order_by('-created_at')
        serializer = TodoSerializer(todo,
                                    many=True,
                                    context={'request': request})
        return Response(serializer.data)

    elif request.method == 'POST':

        serializer = TodoSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 29
0
def todo_list(request):
    """
    List all code types, or create a new snippet.
    """
    if request.method == 'GET':
        todos = Todo.objects.all()
        serializer = TodoSerializer(todos, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = TodoSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)
Esempio n. 30
0
def create_todo(request):
    if request.method == 'POST':
        ts = time.time()
        timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d')
        data = {
            'id': 1,
            'user_id': 1,
            'bucket': 1,
            'message': request.data.get('message'),
            'done': 0,
            'created': timestamp
        }
        serializer = TodoSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 31
0
def todo_detail(request, pk):
    try:
        todo = Todo.objects.get(pk=pk)
    except Todo.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = TodoSerializer(todo)
        return Response(serializer.data)

    elif request.method == 'PUT':
        serializer = TodoSerializer(todo, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        todo.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
Esempio n. 32
0
    def post(self,request):
        """
        Handle the POST request for the '/todo/' endpoint

        This view will take the 'data' property from the 'request'
        object, deserializer it into a 'Todo' object and store it
        it in the DB.

        Returns a 201 (successfully created) if the item is successfully
        created, otherwise returns a 400 (bad request)
        """
        # Instantiate a new TodoSerializer with the data
        # contained in the request object
        # This deserializes the request data
        serializer = TodoSerializer(data=request.data)

        # Check to see if data in the 'request' is valid.
        # If (the data cannot be deserialized into a To-do object)
        #   then a bad request response will be returned containing
        #   the error.
        # Else,
        #   save the data and return the data and a successfully
        #   created status
        if not serializer.is_valid():
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        else:
            # Get the 'user' based on the request data(not in the serializer)
            user = User.objects.get(username = request.data['username'])

            # Get the to-do item data from the serializer
            data = serializer.data

            # Create the new to-do item
            Todo.objects.create(user=user,
                                title=data["title"],
                                description=data["description"],
                                status=data["status"])

            # save data to the DB
            #serializer.save()
            return Response(serializer.data,status=status.HTTP_201_CREATED)
Esempio n. 33
0
def todo_detail(request, pk):
    try:
        todo = Todo.objects.get(pk=pk)
    except Todo.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = TodoSerializer(todo)
        return JsonResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = TodoSerializer(todo, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        todo.delete()
        return HttpResponse(status=204)
Esempio n. 34
0
    def put(self, request, pk):
        """
        Handle PUT request for the '/todo/' endpoint
        Retrieves a 'todo' instance based on the primary key contained
        in the URL.
        Then takes the 'data' property from the 'request' object to update the relevant 'todo' instance
        :return: The Updated object if the update was successful, otherwise 400 (bad request) is returned
        """

        todo = Todo.objects.get(id=pk)
        # Tell the serializer which To-do item to update and pass in the data
        serializer = TodoSerializer(todo,data=request.data)

        # Check to see if the data in the 'request' is valid.
        # If the data cannot be deserialized into a To-do object then
        # a bad request response will be returned containing the error.
        # Else, save and return the data
        if not serializer.is_valid():
            return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST)
        else:
            serializer.save()
            return Response(serializer.data)
Esempio n. 35
0
    def post(self, request):
        """
        Handle the POST request for the `/todo/` endpoint.
 
        This view will take the `data` property from the `request` object,
        deserialize it into a `Todo` object and store in the DB.
 
        Returns a 201 (successfully created) if the item is successfully
        created, otherwise returns a 400 (bad request)
        """
        serializer = TodoSerializer(data=request.data)

        # Check to see if the data in the `request` is valid.
        # If the cannot be deserialized into a Todo object then
        # a bad request respose will be returned containing the error.
        # Else, save the data and return the data and a successfully
        # created status
        if not serializer.is_valid():
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
        else:
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
    def put(self, request, pk):
        """
        Handle PUT request for the `/todo/` endpoint.

        Retrieves a `todo` instance based on the primary key contained
        in the URL. Then takes the `data` property from the `request` object
        to update the relevant `todo` instance.

        Returns the updated object if the update was successful, otherwise
        400 (bad request) is returned
        """
        todo = Todo.objects.get(id=pk)
        serializer = TodoSerializer(todo, data=request.data)

        # Check to see if the data in the `request` is valid.
        # If the cannot be deserialized into a Todo object then
        # a bad request respose will be returned containing the error.
        # Else, save and return the data.
        if not serializer.is_valid():
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
        else:
            serializer.save()
            return Response(serializer.data)
    def post(self, request):
        """
        Handle the POST request for the `/todo/` endpoint.

        This view will take the `data` property from the `request` object,
        deserialize it into a `Todo` object and store in the DB

        Returns a 201 (successfully created) if the item is successfully
        created, otherwise returns a 400 (bad request)
        """
        serializer = TodoSerializer(data=request.data)

        # Check to see if the data in the `request` is valid.
        # If the cannot be deserialized into a Todo object then
        # a bad request respose will be returned containing the error.
        # Else, save the data and return the data and a successfully
        # created status
        if not serializer.is_valid():
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
        else:
            serializer.save()
            return Response(serializer.data,
                            status=status.HTTP_201_CREATED)