Browse Source

Corrige redirecionamento do PDF de acessórios de documento administrativo

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
pull/3865/head
Edward Ribeiro 2 weeks ago
parent
commit
126015b2c1
  1. 43
      sapl/protocoloadm/tests/test_docadm_acesso.py
  2. 15
      sapl/protocoloadm/views.py

43
sapl/protocoloadm/tests/test_docadm_acesso.py

@ -1,3 +1,6 @@
from io import BytesIO
from PyPDF4 import PdfFileWriter
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
@ -8,7 +11,8 @@ from model_bakery import baker
import pytest import pytest
from sapl.base.models import AppConfig from sapl.base.models import AppConfig
from sapl.protocoloadm.models import DocumentoAdministrativo from sapl.protocoloadm.models import (DocumentoAcessorioAdministrativo,
DocumentoAdministrativo)
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
@ -84,23 +88,54 @@ def test_doc_texto_integral_acesso(client, media_tmp, comportamento,
assert response.status_code == (200 if pode_ver else 404) assert response.status_code == (200 if pode_ver else 404)
def _pdf():
writer = PdfFileWriter()
writer.addBlankPage(72, 72)
data = BytesIO()
writer.write(data)
return data.getvalue()
@pytest.mark.django_db(transaction=False) @pytest.mark.django_db(transaction=False)
@pytest.mark.parametrize('comportamento,restrito,perfil,pode_ver', ACESSO) @pytest.mark.parametrize('comportamento,restrito,perfil,pode_ver', ACESSO)
def test_pdf_docacessorios_acesso(client, media_tmp, comportamento, def test_pdf_docacessorios_acesso(client, media_tmp, monkeypatch,
restrito, perfil, pode_ver): comportamento, restrito, perfil, pode_ver):
# create_pdf_docacessorios usa o MEDIA_ROOT importado de sapl.settings
monkeypatch.setattr('sapl.protocoloadm.views.MEDIA_ROOT', str(media_tmp))
_config(comportamento) _config(comportamento)
documento = baker.make(DocumentoAdministrativo, restrito=restrito) documento = baker.make(DocumentoAdministrativo, restrito=restrito)
acessorio = baker.make(DocumentoAcessorioAdministrativo,
documento=documento)
acessorio.arquivo = SimpleUploadedFile(
'acessorio.pdf', _pdf(), content_type='application/pdf')
acessorio.save()
client = _client(client, perfil) client = _client(client, perfil)
response = client.get(reverse('sapl.protocoloadm:merge_docacessorios', response = client.get(reverse('sapl.protocoloadm:merge_docacessorios',
kwargs={'pk': documento.pk})) kwargs={'pk': documento.pk}))
if pode_ver: if pode_ver:
assert response.status_code != 404 assert response.status_code == 200
assert response['Content-Type'] == 'application/pdf'
assert response.content.startswith(b'%PDF')
else: else:
assert response.status_code == 404 assert response.status_code == 404
@pytest.mark.django_db(transaction=False)
def test_pdf_docacessorios_sem_acessorios_volta_para_lista(client):
_config('O')
documento = baker.make(DocumentoAdministrativo, restrito=False)
response = client.get(reverse('sapl.protocoloadm:merge_docacessorios',
kwargs={'pk': documento.pk}))
assert response.status_code == 302
assert response['Location'] == reverse(
'sapl.protocoloadm:documentoacessorioadministrativo_list',
kwargs={'pk': documento.pk})
@pytest.mark.django_db(transaction=False) @pytest.mark.django_db(transaction=False)
@pytest.mark.parametrize('url_name', ['doc_texto_integral', @pytest.mark.parametrize('url_name', ['doc_texto_integral',
'merge_docacessorios']) 'merge_docacessorios'])

15
sapl/protocoloadm/views.py

@ -133,21 +133,24 @@ def get_pdf_docacessorios(request, pk):
logger.error("user= {}.Não há arquivos cadastrados".format(username)) logger.error("user= {}.Não há arquivos cadastrados".format(username))
msg = _('Não há arquivos cadastrados nesses documentos acessórios.') msg = _('Não há arquivos cadastrados nesses documentos acessórios.')
messages.add_message(request, messages.ERROR, msg) messages.add_message(request, messages.ERROR, msg)
return redirect(reverse('sapl.materia:documentoacessorio_list', return redirect(reverse(
kwargs={'pk': pk})) 'sapl.protocoloadm:documentoacessorioadministrativo_list',
kwargs={'pk': pk}))
except Exception as e: except Exception as e:
logger.error("user= {}.Um erro inesperado ocorreu na criação do pdf de documentos acessorios: {}" logger.error("user= {}.Um erro inesperado ocorreu na criação do pdf de documentos acessorios: {}"
.format(username, str(e))) .format(username, str(e)))
msg = _('Um erro inesperado ocorreu. Entre em contato com o suporte do SAPL.') msg = _('Um erro inesperado ocorreu. Entre em contato com o suporte do SAPL.')
messages.add_message(request, messages.ERROR, msg) messages.add_message(request, messages.ERROR, msg)
return redirect(reverse('sapl.materia:documentoacessorio_list', return redirect(reverse(
kwargs={'pk': pk})) 'sapl.protocoloadm:documentoacessorioadministrativo_list',
kwargs={'pk': pk}))
if not data: if not data:
msg = _('Não há nenhum documento acessório PDF cadastrado.') msg = _('Não há nenhum documento acessório PDF cadastrado.')
messages.add_message(request, messages.ERROR, msg) messages.add_message(request, messages.ERROR, msg)
return redirect(reverse('sapl.materia:documentoacessorio_list', return redirect(reverse(
kwargs={'pk': pk})) 'sapl.protocoloadm:documentoacessorioadministrativo_list',
kwargs={'pk': pk}))
response = HttpResponse(data, content_type='application/pdf') response = HttpResponse(data, content_type='application/pdf')
response['Content-Disposition'] = ('attachment; filename="%s"' response['Content-Disposition'] = ('attachment; filename="%s"'

Loading…
Cancel
Save