def post(self, request, format=None): 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): todo = get_todo(pk) 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 put(self, request, pk, format=None): todo = get_object_or_404(Todo, pk=pk) 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 create_todo(request): if request.method == "GET": return JsonResponse({'error': "GET is not allowed"}) elif request.method == "POST": serializer = TodoSerializer(data=request.POST) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, safe=False) else: return JsonResponse(serializer.errors, safe=False)
def post(self, request): data = request.data data['is_done'] = False deserialized_data = TodoSerializer(data=data) if deserialized_data.is_valid(): todo = deserialized_data.save() serialized_data = TodoSerializer(todo) return Response(serialized_data.data, status=status.HTTP_201_CREATED) return Response({'error': 'Data tidak valid'}, status=status.HTTP_400_BAD_REQUEST)
def list_create_todo(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.data, status.HTTP_400_BAD_REQUEST)
def test_view_todo_task_detail(self): '''Test viewing a todo task detail''' todo = sample_todo(user=self.user) url = detail_url(todo.id) res = self.client.get(url) serializer = TodoSerializer(todo) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
def get(self, request): try: todos = Todo.objects.order_by("-created_at") except Todo.DoesNotExist as e: return Response(status=status.HTTP_404_NOT_FOUND) serializer = TodoSerializer(todos, many=True, context={"request": request}) return Response(serializer.data)
def test_retrieve_todo_tasks(self): '''Test retrieving a list of todo tasks''' sample_todo(user=self.user) sample_todo(user=self.user) res = self.client.get(TODO_URL) todo = Todo.objects.all() serializer = TodoSerializer(todo, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
def get(self, request, format=None): try: todos = Todo.objects.all().order_by('-id') serialized_todos = TodoSerializer(todos, many=True) return Response({ "todos": serialized_todos.data, }, status=status.HTTP_200_OK) except Exception as e: return Response({"error": "Something went wrong"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def todo_detail(request, todo_id): try: todo = Todo.objects.get(pk=todo_id) except Exception as e: return JsonResponse({"error": str(e)}, status=404) if (request.method == "GET"): serializer = TodoSerializer(todo) return JsonResponse(serializer.data) elif (request.method == "PUT"): curTodo = JSONParser().parse(request) serializer = TodoSerializer(todo, curTodo) if (serializer.is_valid()): serializer.save() return JsonResponse(serializer.data) elif (request.method == "DELETE"): todo.delete() serializer = TodoSerializer(todo) return JsonResponse(serializer.data)
def post(self, request, format=None): title = request.data.get('title') text = request.data.get('text') try: todo = Todo.objects.create(title=title, text=text) serialized_todo = TodoSerializer(todo, many=False) return Response({ "todo": serialized_todo.data, }, status=status.HTTP_200_OK) except Exception as e: return Response({"error": "Something went wrong"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def todo(request, pk): def get_queryset(): try: todo = Todo.objects.get(pk=pk) except Todo.DoesNotExist: return JsonResponse( {"error": f"There is no todo having primary key {pk}"}) return todo if request.method == "GET": todo = get_queryset() serializer = TodoSerializer(instance=todo) return JsonResponse(serializer.data, safe=False)
def get(self, request): """ Get all todos """ # todos = Todo.objects.filter(user=request.user.id) todos = Todo.objects.filter(user=request.user) serializer = TodoSerializer(todos, many=True) unique_bucket = [] for item in serializer.data: unique_bucket.append(list(item.values())[1]) unique_bucket = set(list(unique_bucket)) response = [{"value": b, "label": b} for b in unique_bucket] if len(response) == 0: response = [{"value": "default", "label": "default"}] return Response(response)
def todo_list(request): if (request.method == "GET"): todos = Todo.objects.all() serializer = TodoSerializer(todos, many=True) return JsonResponse(serializer.data, safe=False) elif (request.method == "POST"): todoToAdd = JSONParser().parse(request) serializer = TodoSerializer(data=todoToAdd) if (serializer.is_valid()): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400)
def todo_list(request): 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, format=None): id = request.GET.get('id') try: todo = Todo.objects.get(id=id) todo.completed = not todo.completed todo.save() serialized_todo = TodoSerializer(todo, many=False) return Response({ "todo": serialized_todo.data, }, status=status.HTTP_200_OK) except Exception as e: # print(e) return Response({"Error": "ToDo doesn't exists with given Id."}, status=status.HTTP_400_BAD_REQUEST)
def get_todos(request): if request.method == 'POST': serializer = TodoSerializer(data=json.loads(request.body)) if serializer.is_valid(): serializer.save() return JsonResponse({"status": "CREATED"}) else: request.GET.get('name') result_set = Todo.objects.all() serializer = TodoSerializer(result_set, many=True) return JsonResponse(serializer.data, safe=False)
def todo_detail(request, id): try: todo = Todo.objects.get(id=id) 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': serializer = TodoSerializer(todo) todo.delete() return JsonResponse({'id': id})
def get(self, request, format=None): todos = Todo.objects.all() serializer = TodoSerializer(todos, many=True, context={'request': request}) return Response(serializer.data)
def post(self, request): 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): todo = get_todo(pk) serializer = TodoSerializer(todo, context={"request": request}) return Response(serializer.data)
def get(self, request): """ Get a todo item """ todos = Todo.objects.filter(id=request.GET.get('id')) serializer = TodoSerializer(todos, many=True) return Response(serializer.data)
def get(self, request): """ Get all todos """ todos = Todo.objects.filter(user=request.user) serializer = TodoSerializer(todos, many=True) return Response(serializer.data)
def TodoView(request, todo_id=None): if request.method == 'GET' and request.resolver_match.url_name == 'todos': todos = Todo.objects.filter(user=request.user).order_by('-updated_at') todo_serializer = TodoListSerializer(todos, many=True) return Response(todo_serializer.data, status=200) if request.method == 'GET' and request.resolver_match.url_name == 'shared_todos': todos = Todo.objects.filter(assigned_users=request.user) todo_serializer = TodoListSerializer(todos, many=True) return Response(todo_serializer.data, status=200) if request.method == 'GET' and request.resolver_match.url_name == 'todo': if not todo_id: return Response({'error': 'Todo id is missing'}, status=400) try: todo = Todo.objects.get(id=todo_id) except Todo.DoesNotExist: return Response({'error': 'Not Found Todo'}, status=404) if todo.user != request.user and not todo.assigned_users.filter( username=request.user.username).exists(): return Response({'error': 'Permission Denied'}, status=403) todo_serializer = TodoSerializer(todo, many=False) return Response(todo_serializer.data, status=200) if request.method == 'PUT' and request.resolver_match.url_name == 'todos': _id = request.data.get('id', None) title = request.data.get('title', None) status = request.data.get('status', False) content = request.data.get('content', '') if not title: return Response({'error': 'Title is missing'}, status=400) if _id: # For Update try: todo = Todo.objects.get(id=_id) except Todo.DoesNotExist: return Response({'error': 'Todo not Found'}, status=404) if todo.user != request.user and not todo.assigned_users.filter( username=request.user.username).exists(): return Response({'error': 'Permission Denied'}, status=403) else: todo = Todo() if not _id: todo.user = request.user todo.title = title todo.content = content todo.status = status todo.updated_at = timezone.now() if not _id: todo.created_at = timezone.now() todo.save() return Response({ 'success': 'todo is created', 'id': todo.id }, status=201) if request.method == 'POST' and request.resolver_match.url_name == 'users': if not todo_id: return Response({'error': 'Todo id is missing'}, status=400) try: todo = Todo.objects.get(id=todo_id) except Todo.DoesNotExist: return Response({'error': 'Not Found Todo'}, status=404) if todo.user != request.user: return Response({'error': 'Permission Denied'}, status=403) usr_email = request.data.get('email', None) if not usr_email: return Response({'error': 'User is missing'}, status=400) try: user = User.objects.get(email=usr_email) except User.DoesNotExist: return Response({'error': 'User Not Found'}, status=404) if not todo.assigned_users.filter(username=user.username).exists(): todo.assigned_users.add(user) return Response({'success': 'added user'}, status=200) if request.method == 'DELETE' and request.resolver_match.url_name == 'todo': if not todo_id: return Response({'error': 'Todo id is missing'}, status=400) try: todo = Todo.objects.get(id=todo_id) except Todo.DoesNotExist: return Response({'error': 'Not Found Todo'}, status=404) if todo.user != request.user: return Response({'error': 'Permission Denied'}, status=403) todo.delete() return Response({'success': 'todo is deleted'}, status=200) if request.method == 'DELETE' and request.resolver_match.url_name == 'users': if not todo_id: return Response({'error': 'Todo id is missing'}, status=400) try: todo = Todo.objects.get(id=todo_id) except Todo.DoesNotExist: return Response({'error': 'Not Found Todo'}, status=404) if todo.user != request.user: return Response({'error': 'Permission Denied'}, status=403) email = request.query_params.get('email', None) if not email: return Response({'error': 'Email is missing'}, status=400) try: user = User.objects.get(email=email) except User.DoesNotExist: return Response({'error': 'User not Found'}, status=404) if todo.assigned_users.filter(email=email).exists(): todo.assigned_users.remove(user) return Response({'success': 'User removed the todo list'}, status=200) return Response({'error': 'Bad Request'}, status=400)
def get(self, request): todos = Todo.objects.all() serialized_todos = TodoSerializer(todos, many=True) return Response(serialized_todos.data, status=status.HTTP_200_OK)