def put(self, request, pk, format=None): todo = self.get_todo(pk) serializer = TodoSerializer(todo, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=HTTP_200_OK) return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
def todo_detail(request, pk): """ Get, udpate, or delete a specific task """ 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) else: return Response(serilizer.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): """ Get, udpate, or delete a specific task """ 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) else: return Response( serilizer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': todo.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def todo_list(request): if request.method == 'GET': todos = Todo.objects.all() title = request.GET.get('title', None) if title is not None: todos = todos.filter(title__icontains=title) todos_serializer = TodoSerializer(todos, many=True) return JsonResponse(todos_serializer.data, safe=False) # 'safe=False' for objects serialization elif request.method == 'POST': todo_data = JSONParser().parse(request) todo_serializer = TodoSerializer(data=todo_data) if todo_serializer.is_valid(): todo_serializer.save() return JsonResponse(todo_serializer.data, status=status.HTTP_201_CREATED) return JsonResponse(todo_serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': count = Todo.objects.all().delete() return JsonResponse( { 'message': '{} All Todo Items were deleted successfully!'.format(count[0]) }, status=status.HTTP_204_NO_CONTENT)
def todo_detail(request, todo_id): try: todo = Todo.objects.get(id=todo_id) except Todo.DoesNotExist: return JsonResponse({'message': 'The todo does not exist'}, status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': todo_serializer = TodoSerializer(todo) return JsonResponse(todo_serializer.data) if request.method == 'PUT': todo_data = JSONParser().parse(request) todo_serializer = TodoSerializer(todo, data=todo_data) if todo_serializer.is_valid(): todo_serializer.save() return JsonResponse(todo_serializer.data) return JsonResponse(todo_serializer.errors, status=status.HTTP_400_BAD_REQUEST) if request.method == 'DELETE': todo.delete() return JsonResponse({'message': 'Tutorial was deleted successfully!'}, status=status.HTTP_204_NO_CONTENT) return JsonResponse({'message': 'Unhandled Request'}, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): serializer = TodoSerializer(data=request.data) if serializer.is_valid(): serializer.save(owner=self.request.user) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create(self, request, **kwargs): serializer_data = request.data serializer_context = { 'include_details': True, 'description': request.data['description'] } serializer = TodoSerializer(data=serializer_data, context=serializer_context) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED)
def todo_list(request): """ List all todos, or create a new todo. """ 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 todo_list(request): """ List all tasks, or create a new task. """ 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) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def todo_list(request): """ List all tasks, or create a new task. """ 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) else: return Response( serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def todo_list(request): if request.method == 'GET': todos = Todo.objects.all() response_data = [] for todo in todos: ddl = (todo.deadline.astimezone(pytz.timezone('Asia/Taipei')) + timedelta(hours=8)).astimezone(pytz.timezone('Asia/Taipei')) ddl_string = ddl.strftime("%Y/%m/%d %H:%M") response_data.append( dict(id=str(todo.id), title=todo.title, ddl=ddl_string, status=getStatus(todo))) response_data.sort(key=getTimedeltaSec, reverse=True) return JsonResponse(response_data, safe=False) if request.method == 'POST': todo_data = JSONParser().parse(request) todo_serializer = TodoSerializer(data=todo_data) if todo_serializer.is_valid(): todo_serializer.save() return JsonResponse(todo_serializer.data, status=status.HTTP_201_CREATED) return JsonResponse(todo_serializer.errors, status=status.HTTP_400_BAD_REQUEST) if request.method == 'DELETE': count = Todo.objects.all().delete() return JsonResponse( {'message': '{} Todos were deleted successfully!'.format(count)}, status=status.HTTP_204_NO_CONTENT) return JsonResponse({'message': 'Unhandled Request'}, status=status.HTTP_400_BAD_REQUEST)
def working_to_complete(request, id): todo =Todo.objects.get(id=id) if todo.status == 'WR': todo.status = 'DN' todo.save() return Response('', status=status.HTTP_200_OK) else: Todo_serializer = TodoSerializer(todo, data=request.data) if Todo_serializer.is_valid(): Todo_serializer.save() return Response({"data": 'ID Updated successfully.'}, status=status.HTTP_200_OK) else: error_details = [] for key in Todo_serializer.errors.keys(): error_details.append({"field": key, "message": Todo_serializer.errors[key][0]}) data = { "Error": { "status": 400, "message": "Your submitted data was not valid - please correct the below errors", "error_details": error_details } } return Response(data, status=status.HTTP_400_BAD_REQUEST)
def todo_detail(request, pk): """ Retrieve, update or delete a todo. """ 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 requset.method == 'DELETE': todo.delete() return HttpResponse(status=204)