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)
Beispiel #2
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)
Beispiel #3
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)
Beispiel #4
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)
Beispiel #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)
    def get(self, request, pk=None):
        """
        Handle the GET request for the `/todo/` endpoint.

        Checks to see if a primary key has been provided by the URL.
        If not, a full list of `todo` will be retrieved. If a primary key
        has been provided then only that instance will be retrieved

        Returns the serialized `todo` object(s).
        """
        if pk is None:
            todo_items = Todo.objects.all()
            # Serialize the data retrieved from the DB and serialize
            # them using the `TodoSerializer`
            serializer = TodoSerializer(todo_items, many=True)
            # Store the serialized data `serialized_data`
            serialized_data = serializer.data
            return Response(serialized_data)
        else:
            # Get the object containing the pk provided by the URL
            todo = Todo.objects.get(id=pk)
            # Serialize the `todo` item
            serializer = TodoSerializer(todo)
            # Store it in the serialized_data variable and return it
            serialized_data = serializer.data
            return Response(serialized_data)
Beispiel #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)
Beispiel #8
0
    def get(self, request, pk=None):
        """
        Handle the GET request for the `/todo/` endpoint.

        Retrieve a complete list of `todo` items from the Todo
        model, serialize them to JSON and return the serialized
        todo items.

        Returns the serialized `todo` objects.
        """
        if pk is None:
            todo_items = Todo.objects.all()
            # Serialize the data retrieved from the DB and serialize
            # them using the `TodoSerializer`
            serializer = TodoSerializer(todo_items, many=True)
            # Store the serialized data `serialized_data`
            serialized_data = serializer.data
            return Response(serialized_data)
        else:
            # Get the object containing the pk provided by the URL
            todo = Todo.objects.get(id=pk)
            # Serialize the `todo` item
            serializer = TodoSerializer(todo)
            # Store it in the serialized_data variable and return it
            serialized_data = serializer.data
            return Response(serialized_data)
Beispiel #9
0
    def get(self, request, pk=None):
        """
        Handle the GET request for the `/todo/` endpoint.

        Checks to see if a primary key has been provided by the URL.
        If not, a full list of `todo` will be retrieved. If a primary key
        has been provided then only that instance will be retrieved

        Returns the serialized `todo` object(s).
        """
        if "username" in request.query_params:
            if pk is None:
                #Get the `user` based on the username provided by the `query_params`
                user = User.objects.get(
                    username=request.query_params["username"])
                #filter the `todo` items based on this `user`
                todo_items = Todo.objects.all()
                #Serialize the data retrieved from the DB and serialize them using the TodoSerializer
                serializer = TodoSerializer(todo_items, many=True)
                serialized_data = serializer.data
                return Response(serialized_data)
            else:
                # Get the object containing the pk provided by the URL
                todo = Todo.objects.get(id=pk)
                # Serialize the `todo` item
                serializer = TodoSerializer(todo)
                # Store it in the serialized_data variable and return it
                serialized_data = serializer.data
                return Response(serialized_data)
        else:
            return Response(status=status.HTTP_404_NOT_FOUND)
Beispiel #10
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)
Beispiel #11
0
 def get(self, request, pk=None):
     '''
     Retrieve a complete list of 'todo' items from the Todo model
     , serialize them to JSON and return the serialized todo items
     :param request:
     :return: serialized data
     '''
     #if "username" in request.query_params:
     if pk is None:
         #   Retrieve the items
         todo_items = Todo.objects.all()
         #   Serialize the data retrieved from the DB and serialize them
         #   using the 'TodoSerialize'
         serializer = TodoSerializer(todo_items, many=True)
         #   Store the serialized_data 'serialized_data'
         #   in the .data property of the object "serialized_data"
         #   e.g. JSON is at serialized_data.data
         serialized_data = serializer.data
         #   return the data in an instance of the Response object
         return Response(serialized_data)
     else:
         #   Retrieve the item
         todo = Todo.objects.get(id=pk)
         #   Serialize the data retrieved from the DB and serialize them
         #   using the 'TodoSerialize'
         serializer = TodoSerializer(todo)
         #   Store the serialized_data 'serialized_data'
         #   in the .data property of the object "serialized_data"
         #   e.g. JSON is at serialized_data.data
         serialized_data = serializer.data
         #   return the data in an instance of the Response object
         return Response(serialized_data)
Beispiel #12
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)
Beispiel #13
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)
Beispiel #14
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)
Beispiel #15
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)
Beispiel #16
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)
Beispiel #17
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)
Beispiel #18
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)
Beispiel #19
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)
Beispiel #20
0
    def test_user_can_get_another_user_todo(self):
        todo1 = TodoFactory(description='todo1')
        todo2 = TodoFactory(description='todo2')

        another_user = UserFactory(username='******')
        todo3 = TodoFactory(description='todo3', created_by=another_user)

        response = self.client.get(LIST_TODO_URL)
        serializer = TodoSerializer([todo1, todo2], many=True)

        self.assertEqual(response.data, serializer.data)
        self.assertNotIn(TodoSerializer(todo3).data, serializer.data)
Beispiel #21
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)
Beispiel #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)
Beispiel #23
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)
Beispiel #24
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)
Beispiel #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)
Beispiel #26
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)
Beispiel #27
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)
Beispiel #28
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)
Beispiel #29
0
def get_one_todo(task_id):
    try:
        task_id = Todo.objects.get(id=task_id)
    except Todo.DoesNotExist:
        return None
    serialized = TodoSerializer(task_id)
    return serialized.data
Beispiel #30
0
def get_task_with_user(reporter_id):
    try:
        tasks = Todo.objects.get(id=reporter_id, many=True)
    except Todo.DoesNotExist:
        return None
    serialized = TodoSerializer(tasks)
    return serialized.data
Beispiel #31
0
 def get(self,request,id):
   try:
     obj=Todo.objects.get(id=id)
     serialize = TodoSerializer(obj)
     return Response(serialize.data)
   except Todo.DoesNotExist:
     data={"detail": "Not found."}
     return Response(data,status=404)
Beispiel #32
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)
Beispiel #33
0
 def get(self, request, pk=None):
     if pk is None:
         todo_items = Todo.objects.all()
         # Serialize the data retrieved from the DB and serialize
         # them using the `TodoSerializer`
         serializer = TodoSerializer(todo_items, many=True)
         # Store the serialized data `serialized_data`
         serialized_data = serializer.data
         return Response(serialized_data)
     else:
         # Get the object containing the pk provided by the URL
         todo = Todo.objects.get(id=pk)
         # Serialize the `todo` item
         serializer = TodoSerializer(todo)
         # Store it in the serialized_data variable and return it
         serialized_data = serializer.data
         return Response(serialized_data)
 def test_todo_list(self):
     payload = {
         "title": "test serializer",
         "description": "test serializer"
     }
     todos = Todo.objects.all().order_by('title')
     serializer = TodoSerializer(payload, many=True)
     self.assertEqual(todos.count(), serializer.data.count())
Beispiel #35
0
    def test_todo_list_for_current_user(self):
        todo1 = TodoFactory(description='todo1')
        todo2 = TodoFactory(description='todo2')

        response = self.client.get(LIST_TODO_URL)
        serializer = TodoSerializer([todo1, todo2], many=True)

        self.assertEqual(response.data, serializer.data)
    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)