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)
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)
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)
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)
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)
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})
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)
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)
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)
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)
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 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)
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)
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)
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)
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
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_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
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)
def get(self, request, format=None): """ get request lists out all tasks """ tasks = Todo.objects.filter( user=request.user) #fetching tasks of logged in user serializer = TodoSerializer(tasks, many=True) return Response(serializer.data)
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)
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 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)
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())
def get(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) return Response(serializer.data)
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)
def get(self,request, pk=None): """ Handle the GET request for the `/todo/` endpoint. Gets `username` from the `query_params` in order to retrieve the `todo` items belonging to that user, IF primary key has been provided by the URL: Return the relevant object ELSE Retrieve a complete list of 'todo' items from the Todo model, 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 'to-do' items based on this 'user' todo_items = Todo.objects.filter(user=user) # todo_items = Todo.objects.all() # Serialize the data retrieved from the DB and # serialize them using the 'TodoSerializer' # many=True informs the serializer that there's # multiple records to be serialized serializer = TodoSerializer(todo_items, many= True) # the serialized data 'serialized_data' is # stored in the .data property of the serializer # object serialized_data = serializer.data # return an instance of Django REST Response obj # containing the serialized data return Response(serialized_data) else: # Get the object containing the pk provided by the URL todo = Todo.objects.get(id=pk) # Serialize the 'to-do' 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)
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)
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)