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 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 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 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 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 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 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)
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 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 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)
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)
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)
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 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)
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 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 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)
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 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)
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)
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)
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)