diff --git a/src/tupan/alertas/urls.py b/src/tupan/alertas/urls.py index 1ad748b..3868381 100644 --- a/src/tupan/alertas/urls.py +++ b/src/tupan/alertas/urls.py @@ -3,7 +3,7 @@ urlpatterns = [ path('', AlertasView.as_view(), name='alertas'), - path('', AlertasDetalhesView.as_view(), name='alertas-detalhes'), + path('/', AlertasDetalhesView.as_view(), name='alertas-detalhes'), path('historicos', HistoricoAlertaView.as_view(), name='historico-alertas'), path('medicoes', MedicaoView.as_view(), name='medicoes'), path('medicoes/', MedicaoDetalhesView.as_view(), name='medicoes-detalhes') diff --git a/src/tupan/alertas/views.py b/src/tupan/alertas/views.py index b1976e8..2d9dce0 100644 --- a/src/tupan/alertas/views.py +++ b/src/tupan/alertas/views.py @@ -59,7 +59,6 @@ def post(self, request, *args, **kwargs): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - except json.JSONDecodeError: return JsonResponse({'error': 'Dados inválidos'}, status=400) @@ -77,18 +76,13 @@ def get(self, request, id, *args, **kwargs): def put(self, request, id, *args, **kwargs): try: - data = json.loads(request.body) alerta = Alerta.objects.filter(id=id, ativo=True).first() if alerta: - alerta.nome = data.get('nome', alerta.nome) - alerta.condicao = data.get('condicao', alerta.condicao) - alerta.save() - return JsonResponse({ - 'id': alerta.pk, - 'nome': alerta.nome, - 'condicao': alerta.condicao, - 'ativo': alerta.ativo - }, status=200) + serializer = AlertaSerializer(alerta, data=request.data) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data, status=status.HTTP_200_OK) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) else: return JsonResponse({'error': 'Alerta não encontrado ou inativo'}, status=404) except json.JSONDecodeError: