diff --git a/conftest.py b/conftest.py index ff062ea..8959532 100644 --- a/conftest.py +++ b/conftest.py @@ -80,3 +80,14 @@ def _handle_page_goto( if not (url.startswith("http://") or url.startswith("https://")): url = base_url + url return page._goto(url, *args, **kwargs) # type: ignore + + +@pytest.fixture +def login(page): + page.goto("") + page.click("text=Log In") + page.click(":nth-match(:text(\"Log In\"), 2)") + page.fill("input[name=\"email\"]", "example@example.com") + page.press("input[name=\"email\"]", "Tab") + page.fill("input[name=\"password\"]", "openlex1234") + page.click("input:has-text(\"Log In\")") diff --git a/controllers/expedientes.py b/controllers/expedientes.py index f189557..9681d0f 100644 --- a/controllers/expedientes.py +++ b/controllers/expedientes.py @@ -4,6 +4,9 @@ import zipfile import io +import tempfile +from collections import OrderedDict +from xhtml2pdf import pisa LINKED_TABLES = ['movimiento', 'agenda', 'parte'] ZIP_FILENAME = 'Movimiento.zip' CHUNK_SIZE = 4096 @@ -69,26 +72,57 @@ def index(): @auth.requires_login() def download(): - tempfile = io.BytesIO() - temparchive = zipfile.ZipFile(tempfile, 'w', zipfile.ZIP_DEFLATED) - rows = db(db.movimiento.archivo != None).select() - try: - for file_id in rows: - file_single = file_id.archivo - if file_single: - file_loc = db.movimiento.archivo.retrieve_file_properties(file_single)['path']+ '/' + file_single - file_name = db.movimiento.archivo.retrieve_file_properties(file_single)['filename'] - temparchive.write(file_loc, file_name) - finally: - temparchive.close() - tempfile.seek(0) + tempfiles = io.BytesIO() + temparchive = zipfile.ZipFile(tempfiles, 'w', zipfile.ZIP_DEFLATED) + temp_dir = tempfile.TemporaryDirectory() + # Obtener ID de expediente y guardarlo en una lista. + movimiento_full_row = db(db.movimiento).select() + expediente_list = [numero.expediente_id for numero in movimiento_full_row] + expediente_lists = OrderedDict.fromkeys(expediente_list).keys() + # Separar archivos por el numero de expediente. + for expedient_single_id in expediente_lists: + movimiento_filter_row = db(db.movimiento.expediente_id == expedient_single_id).select( + db.movimiento.archivo, + db.movimiento.texto, + db.movimiento.titulo) + expedient_number_row = db(db.expediente.id == expedient_single_id).select(db.expediente.numero) + expedient_name = [numero.numero for numero in expedient_number_row] + try: + for file_id in movimiento_filter_row: + file_single = file_id.archivo + if file_single: + file_loc = db.movimiento.archivo.retrieve_file_properties(file_single)['path'] + '/' + file_single + file_name = db.movimiento.archivo.retrieve_file_properties(file_single)['filename'] + temparchive.write(file_loc, "upload/" + str(expedient_name[0]) + "/" + file_name) + except Exception as err: + response.flash = DIV(T('Fallo en la descarga de archivos'), PRE(str(err))) + finally: + for text_id in movimiento_filter_row: + text_single_text = text_id.texto + text_single_title = text_id.titulo + status, result_file = convert_html_to_pdf(text_single_text, temp_dir.name + "/" + text_single_title + ".pdf") + temparchive.write(result_file.name, "upload/" + str(expedient_name[0]) + "/" + text_single_title + ".pdf") + del temp_dir + temparchive.close() + tempfiles.seek(0) response.headers['Content-Type'] = 'application/zip' response.headers['Content-Disposition'] = 'attachment; filename = %s' % ZIP_FILENAME - res = response.stream(tempfile, CHUNK_SIZE) + res = response.stream(tempfiles, CHUNK_SIZE) return res +def convert_html_to_pdf(source_html, output_filename): + # open output file for writing (truncated binary) + with open(output_filename, "w+b") as result_file: + # convert HTML to PDF + pisa_status = pisa.CreatePDF( + source_html, # the HTML to convert + dest=result_file) # file handle to recieve result + # return False on success and True on errors + return pisa_status, result_file + + def vista_expediente(): 'muestra un panel a la izquierda que tiene los datos del expediente y permite navegar en él' expte = SQLFORM(db.expediente, @@ -112,9 +146,9 @@ def vista_expediente(): text = SPAN(k.capitalize() + 's', _class='buttontext button') links.append(A(text, _href=url, _type='button', _class='btn btn-default')) - url = URL('download', args='movimiento.archivo') #Boton de descarga - text1="Descarga" - links.append(A(text1, _href=url, _type='button', + url = URL('download', args='movimiento.archivo') # Boton de descarga + text_download = "Descarga" + links.append(A(text_download, _href=url, _type='button', _class='btn btn-default')) return dict(links=links, expte=expte) diff --git a/requirements.txt b/requirements.txt index f94c749..621cedf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ pytest==6.2.4 playwright==1.11.2 pytest-playwright==0.1.1 -pytest-html==3.1.1 \ No newline at end of file +pytest-html==3.1.1 +xhtml2pdf==0.2.5 diff --git a/tests/test_a_user.py b/tests/test_a_user.py deleted file mode 100644 index c1d5689..0000000 --- a/tests/test_a_user.py +++ /dev/null @@ -1,64 +0,0 @@ -import pytest - - -def login(page): - page.click("text=Log In") - page.click(":nth-match(:text(\"Log In\"), 2)") - page.fill("input[name=\"email\"]", "example@example.com") - page.press("input[name=\"email\"]", "Tab") - page.fill("input[name=\"password\"]", "openlex1234") - page.click("input:has-text(\"Log In\")") - - -def test_register(page): - # ir a la página de inicio (ver pytest.ini para la url base) - page.goto("http://127.0.0.1:8020/OpenLex/") - # desplegar el menu, ir a la página de registración (y confirmar url) - page.click("text=Log In") - page.click("text=Registrarse") - assert page.url.endswith("OpenLex/default/user/register?_next=/OpenLex/default/index") - - # completar el formulario de registro: - page.click("input[name=\"first_name\"]") - page.fill("input[name=\"first_name\"]", "Juan") - page.press("input[name=\"first_name\"]", "Tab") - page.fill("input[name=\"last_name\"]", "Perez") - page.press("input[name=\"last_name\"]", "Tab") - page.fill("input[name=\"email\"]", "example@example.com") - page.press("input[name=\"email\"]", "Tab") - page.fill("input[name=\"password\"]", "openlex1234") - page.press("input[name=\"password\"]", "Tab") - page.fill("input[name=\"password_two\"]", "openlex1234") - page.click("input:has-text(\"Registrarse\")") - - # confirmar que se tomó el formulario y pasó a la página principal: - assert page.url.endswith("/dashboard/view#") - # confirmar que se creó el usuario: - assert page.inner_text("text=Bienvenido, Juan Perez") - - -def test_login(page): - # ADVERTENCIA: este test depende del anterior (sería bueno que sea independiente) - # ir a la página de inicio (ver pytest.ini para la url base) - page.goto("") - # desplegar el menu, ir a la página de registración (y confirmar url) - login(page) - # confirmar - assert page.url.endswith("/dashboard/view#") - - -@pytest.mark.skip(reason="Este test funciona en local pero falla en github, no toma el evento download") -def test_download(page): - page.goto("") - login(page) - page.goto("expedientes/index") - page.click("text=Movimientos") - with page.expect_download() as download_info: - # Perform the action that initiates download - page.click("text=Descarga") - download = download_info.value - # Wait for the download process to complete - name = download.suggested_filename - assert "Movimiento.zip" == name - - diff --git a/tests/test_b_expediente_updated.py b/tests/test_b_expediente_updated.py index 2b26f7e..c010c7e 100644 --- a/tests/test_b_expediente_updated.py +++ b/tests/test_b_expediente_updated.py @@ -1,7 +1,5 @@ -def test_upload_expedientes(page): +def test_upload_expedientes(page, login): pattern = "movimiento.expediente_id" - page.goto("http://127.0.0.1:8020/OpenLex/") - login(page) page.click("css=[alt=Expedientes]") assert page.url.endswith("/expedientes/index") page.click("text=Agregar") @@ -9,11 +7,3 @@ def test_upload_expedientes(page): page.press("input[name=\"numero\"]", "Tab") page.fill("input[name=\"caratula\"]", "ssdd") page.click("input:has-text(\"Enviar\")") - -def login(page): - page.click("text=Log In") - page.click(":nth-match(:text(\"Log In\"), 2)") - page.fill("input[name=\"email\"]", "example@example.com") - page.press("input[name=\"email\"]", "Tab") - page.fill("input[name=\"password\"]", "openlex1234") - page.click("input:has-text(\"Log In\")") diff --git a/tests/test_download.py b/tests/test_download.py new file mode 100644 index 0000000..f1fe867 --- /dev/null +++ b/tests/test_download.py @@ -0,0 +1,15 @@ +import pytest + +@pytest.mark.skip(reason="Este test funciona en local pero falla en github, no toma el evento download") +def test_download(page, login): + page.goto("expedientes/index") + page.click("text=Movimientos") + with page.expect_download() as download_info: + # Perform the action that initiates download + page.click("text=Descarga") + download = download_info.value + # Wait for the download process to complete + name = download.suggested_filename + assert "Movimiento.zip" == name + +