From 616e274f6fba5fbc48d6091e0430727522c85dbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Moraes?= Date: Tue, 17 Nov 2026 17:42:16 -0300 Subject: [PATCH] =?UTF-8?q?Retorna=20404=20ao=20incluir=20composi=C3=A7?= =?UTF-8?q?=C3=A3o=20em=20mesa=20diretora=20inexistente?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit /mesa-diretora//composicaomesa/create dava 500 (MesaDiretora.DoesNotExist em get_initial). As demais URLs da mesa já retornavam 404. --- sapl/parlamentares/tests/test_mesadiretora.py | 7 +++++++ sapl/parlamentares/views.py | 5 +++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/sapl/parlamentares/tests/test_mesadiretora.py b/sapl/parlamentares/tests/test_mesadiretora.py index eb93a385d..a1cb445eb 100644 --- a/sapl/parlamentares/tests/test_mesadiretora.py +++ b/sapl/parlamentares/tests/test_mesadiretora.py @@ -1078,3 +1078,10 @@ def test_migration_0048_reversao_com_mesas_da_erro_claro(): baker.make(MesaDiretora) with pytest.raises(IrreversibleError, match='Restaure o backup'): migration.impede_reversao_com_mesas(apps, None) + + +@pytest.mark.django_db(transaction=False) +def test_composicaomesa_create_mesa_inexistente_retorna_404(admin_client): + response = admin_client.get(reverse( + 'sapl.parlamentares:composicaomesa_create', kwargs={'pk': 999999})) + assert response.status_code == 404 diff --git a/sapl/parlamentares/views.py b/sapl/parlamentares/views.py index ce71b278e..a29c99684 100644 --- a/sapl/parlamentares/views.py +++ b/sapl/parlamentares/views.py @@ -11,7 +11,7 @@ from django.db.models import F from django.db.models.aggregates import Count from django.http import Http404, JsonResponse from django.http.response import HttpResponseRedirect -from django.shortcuts import render +from django.shortcuts import get_object_or_404, render from django.templatetags.static import static from django.urls import reverse, reverse_lazy from django.utils import timezone @@ -1102,7 +1102,8 @@ class ComposicaoMesaCrud(MasterDetailCrud): def get_initial(self): initial = super().get_initial() - initial['mesa_diretora'] = MesaDiretora.objects.get(pk=self.kwargs['pk']) + initial['mesa_diretora'] = get_object_or_404( + MesaDiretora, pk=self.kwargs['pk']) return initial