def put(self, request, pk, format=None): task = self.get_object(pk) serializer = TaskSerializer(task, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def task_detail(request, pk): """ Get, udpate, or delete a specific task """ try: task = Task.objects.get(pk=pk) except Task.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = TaskSerializer(task) return Response(serializer.data) elif request.method == 'PUT': serializer = TaskSerializer(task, 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': task.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def _populate(self): serializer = TaskSerializer(data=self._data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=200) return Response(serializer.errors, status=500)
def put(self, request, pk): task = self.get_object(pk) serializer = TaskSerializer(instance=task, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def put(self, request, pk, format=None): task = self.get_object(pk) serializer =TaskSerializer(task, data=request.DATA) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def taskUpdate(request, pk): task = Task.objects.get(id=pk) serializer = TaskSerializer(instance=task, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data)
def update(self, request, pk=None): """ (PUT): Actualiza el elemento solicitado mediante su ID. Args: pk (int, optional): ID de la tarea a actualizar. Returns: Response: Respuesta en el formato requerido por el header "Accept" """ try: queryset = Task.objects.all() task = get_object_or_404(queryset, pk=pk) except ValueError: return Response({'detail': "El id debe ser un entero."}, status.HTTP_400_BAD_REQUEST) # Validaciones serialized_data = TaskSerializer(task, data=request.data, partial=True) if serialized_data.is_valid(): if task.estatus == 'completada': # No actualiza si el estatus es "completada" return Response( { 'estatus': "La tarea no se puede actualizar, ya ha sido completada." }, status.HTTP_403_FORBIDDEN) serialized_data.save_updated() return Response(serialized_data.data) # Tarea actualizada. return Response(serialized_data.errors, status.HTTP_400_BAD_REQUEST)
def post(self, request, pk): taskList = self.get_task_list(pk) serializer = TaskSerializer(data=request.data) if serializer.is_valid(): serializer.save(task_list=taskList) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def task_list(request): """ List all tasks, or create a new task. """ tasks = Task.objects.all() if request.method == 'GET': serializer = TaskSerializer(tasks, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = TaskSerializer(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) elif request.method == 'DELETE': for task in tasks: task.delete() return Response(status=status.HTTP_204_NO_CONTENT) elif request.method == 'FILTER': types = [] for task in tasks: types.append(str(task.get_pk())) return Response(" ".join(types))
def post(self, request, pk): request.data['task_list'] = pk serializer = TaskSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=201) return Response(serializer.errors, status=500)
def taskCreate(request): serializer = TaskSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data)
def post(self, request): serializer = TaskSerializer(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 patch(self, request, pk, pk_task, format='json'): try: task = Task.objects.get(owner=request.user.pk, id=pk_task) except (TypeError, ValueError, OverflowError, Task.DoesNotExist): task = None if task is not None: request.data['owner'] = task.owner.id if 'title' not in request.data: request.data['title'] = task.title if 'deadline' not in request.data: request.data['deadline'] = task.deadline if 'assigned_to' not in request.data: request.data['assigned_to'] = task.assigned_to else: try: request.data['assigned_to'] = User.objects.get( username=request.data['assigned_to']).id except (TypeError, ValueError, OverflowError, User.DoesNotExist): return Response('invalid username', status.HTTP_400_BAD_REQUEST) serializer = TaskSerializer(task, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status.HTTP_202_ACCEPTED) else: return Response(serializer.errors, status.HTTP_400_BAD_REQUEST) else: return Response('', status.HTTP_403_FORBIDDEN)
def post(self, request, format=None): serializer = TaskSerializer(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 put(self, request, id): task = self.get_object(id) serializer = TaskSerializer(task, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.data, status=status.HTTP_404_BAD_REQUEST)
def post(self, request, pk): t_list = request.data.pop('task_list') task_list = TaskList(t_list['id'], t_list['name']) serializer = TaskSerializer(task_list=task_list, data=request.data) if serializer.is_valid(): serializer.save(task_list=task_list) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors)
def post(self, request): serialized_data = TaskSerializer(data=request.data, many=True) if serialized_data.is_valid(): # Preparación de la información faltante. set_dummy(serialized_data.validated_data) serialized_data.save() return Response(serialized_data.data, status.HTTP_201_CREATED) return Response(serialized_data.errors, status.HTTP_400_BAD_REQUEST)
def post(self, request, id): data = json.loads(request.body) data['task_list'] = id serializer = TaskSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400)
def _populate(self): todo_obj = Todo.objects.get(id=self._tid) serializer = TaskSerializer(todo_obj, data=self._data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, 400)
def put(self, request, pk, format=None): try: task = get_object(Task, pk) serializer = TaskSerializer(task, data=request.data, partial=True) if serializer.is_valid(raise_exception=True): serializer.save() return Response(serializer.data) except ResourceNotFoundException as exc: return Response(data=exc.data, status=exc.status_code)
def post(self, request): serializer = TaskSerializer(data=request.data) if not serializer.is_valid(): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) l = StdOutListener(serializer.data['count'], serializer.data['search']) stream = Stream(auth, l) stream.filter(track=serializer.data['search']) return Response(serializer.data, status=status.HTTP_201_CREATED)
def taskUpdateView(request, id): try: task = Task.objects.get(id=id) except: return Response(False) serializer = TaskSerializer(instance=task, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data)
def put(self, request, pk, pk2): tasks_info = self.get_object(pk) task = tasks_info.tasks_set.get(pk2) serializer = TaskSerializer(instance=task, 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_500_INTERNAL_SERVER_ERROR)
def put(self, request, pk1, pk2): task = self.get_task(request, pk1, pk2) try: request.data.pop('task_list') except: pass serializer = TaskSerializer(instance=task, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors)
def create(self, request): """ (POST): Crea una nueva tarea en el modelo de base de datos. """ if not request.data.get('tiempo_estimado'): return Response({"tiempo_estimado": ["Este campo es requerido."]}, status.HTTP_400_BAD_REQUEST) serialized_data = TaskSerializer(data=request.data) if serialized_data.is_valid(): serialized_data.save_new() return Response(serialized_data.data, status.HTTP_201_CREATED) return Response(serialized_data.errors, status.HTTP_400_BAD_REQUEST)
def put(self, request, pk, pk2): t_list = self.get_object(pk) data = request.data try: task = t_list.task_set.get(id=pk2) t_list = data.pop('task_list') taskList = TaskList(t_list['id'], t_list['name']) serializer = TaskSerializer(instance=task, data=data) if serializer.is_valid(): serializer.save(task_list=taskList) return Response(serializer.data) return Response(serializer.errors) except Task.DoesNotExist as e: return Response({'error': str(e)})
def post(self, request, pk, format='json'): request.data['owner'] = pk serializer_context = { 'request': request, } serializer = TaskSerializer(data=request.data, context=serializer_context) if serializer.is_valid(): task = serializer.save() task.save() if task: return Response(serializer.data, status.HTTP_201_CREATED) else: return Response(serializer.errors, status.HTTP_400_BAD_REQUEST)
def taskDetailedView(request, id): try: task = Task.objects.get(id=id) except: return Response(False) serializer = TaskSerializer(task, many=False) return Response(serializer.data)
def get(self, request): if 'q' not in request.query_params: return Response( { 'error': 'Parámetros insuficientes', 'q': 'Requerido' }, status=status.HTTP_400_BAD_REQUEST) q = request.query_params.get('q') order_by = 'id' if 'orderby' in request.query_params: # Si se pide ordenamiento order_by = request.query_params.get('orderby') order_choices = [ 'creacion', 'actualizacion', 'descripcion', 'tiempo_estimado', 'tiempo_registrado', 'estatus' ] if not order_by in order_choices: # valida opción return Response( { 'error': f'[{order_by}] Valor inválido', 'orderby': 'opciones: creacion, actualizacion, descripcion, tiempo_estimado, tiempo_registrado, estatus' }, status=status.HTTP_400_BAD_REQUEST) tasks = Task.objects.search(q, order_by) serialized_data = TaskSerializer(tasks, many=True) return Response({'results': serialized_data.data})
def update(self, request, *args, **kwargs): task = self.get_object() task.title = request.data['title'] task.completed = request.data['completed'] task.save() serializer = TaskSerializer(task, many=False) return Response(serializer.data)
def get(self, request, pk, format=None): try: task = get_object(Task, pk) serializer = TaskSerializer(task) return Response(serializer.data) except ResourceNotFoundException as exc: return Response(data=exc.data, status=exc.status_code)
def task_list(request): """ List all tasks, or create a new task. """ if request.method == 'GET': tasks = Task.objects.all() serializer = TaskSerializer(tasks) return Response(serializer.data) elif request.method == 'POST': serializer = TaskSerializer(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 get(self, request, format=None): if 'tasks' not in request.query_params: raise ParseError('`tasks` query parameter required') if 'worker_id' in request.query_params: try: worker_id = str(request.query_params['worker_id']) except ValueError: raise ParseError( '`worker_id` query parameter must be a string') else: raise ParseError('`worker_id` query parameter required') if 'limit' in request.query_params: try: limit = int(request.query_params['limit']) except ValueError: raise ParseError('`limit` query parameter must be an integer') else: limit = 1 if limit < 1 or limit > 10: raise ParseError('`limit` must be between 1 and 10') raw_tasks = queue.get_tasks(request.query_params.getlist('tasks'), request.query_params['worker_id'], limit) tasks = [] for task in raw_tasks: serializer = TaskSerializer(task) tasks.append(serializer.data) return Response(tasks)
def complete_task(self, request, pk=None): live_task = self.get_object() task = live_task.task response = get_ost_kit().transactions.execute( from_user_id=task.user.profile.ost_id, to_user_id=live_task.user.profile.ost_id, action_id=39046, amount=task.reward) if response["success"]: task_reward = TaskReward( transaction_id=response["data"]["transaction"]["id"], user=live_task.user, task=task) task_reward.save() live_task.delete() return Response(TaskSerializer(task).data, status=status.HTTP_200_OK) return Response( { "message": "Something went wrong when completing the task", 'err': response["err"] }, status=status.HTTP_409_CONFLICT)
def retrieve_user_tasks(self, request): user = request.user live_tasks = user.livetask_set.all() completed_tasks = user.taskreward_set.all() return Response( { 'completed_tasks': TaskSerializer( [task_reward.task for task_reward in completed_tasks], many=True).data, 'live_tasks': TaskSerializer([live_task.task for live_task in live_tasks], many=True).data }, status=status.HTTP_200_OK)
def task_list(request): """ List all tasks, or create a new task. """ if request.method == "GET": tasks = Task.objects.all() logging.debug(type(tasks)) logging.debug(tasks) serializer = TaskSerializer(tasks, many=True) return Response(serializer.data) elif request.method == "POST": serializer = TaskSerializer(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)