Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download zip separated folders #41

Merged
merged 11 commits into from
Jul 13, 2021
67 changes: 52 additions & 15 deletions controllers/expedientes.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
Juerodriguez marked this conversation as resolved.
Show resolved Hide resolved
__author__ = "María Andrea Vignau (mavignau@gmail.com)"
__copyright__ = "(C) 2016 María Andrea Vignau. GNU GPL 3."

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
Expand Down Expand Up @@ -69,26 +72,60 @@ 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)
list_temp_directories = []
list_full_directories = []
file_loc_base = None
zip_filename = 'Movimiento.zip'
Juerodriguez marked this conversation as resolved.
Show resolved Hide resolved
tempfiles = io.BytesIO()
temparchive = zipfile.ZipFile(tempfiles, 'w', zipfile.ZIP_DEFLATED)
tempdir7 = tempfile.TemporaryDirectory()
Juerodriguez marked this conversation as resolved.
Show resolved Hide resolved
#Obtener ID de expediente y guardarlo en una lista.
Juerodriguez marked this conversation as resolved.
Show resolved Hide resolved
rowA = db(db.movimiento).select()
Juerodriguez marked this conversation as resolved.
Show resolved Hide resolved
expediente_list = [numero.expediente_id for numero in rowA]
expediente_lists = OrderedDict.fromkeys(expediente_list).keys()

#Separar archivos por el numero de expediente.
Juerodriguez marked this conversation as resolved.
Show resolved Hide resolved
for expedientes in expediente_lists:
Juerodriguez marked this conversation as resolved.
Show resolved Hide resolved
rowB = db(db.movimiento.expediente_id == expedientes).select(db.movimiento.archivo, db.movimiento.texto, db.movimiento.titulo)
rowD = db(db.expediente.id == expedientes).select(db.expediente.numero)
Juerodriguez marked this conversation as resolved.
Show resolved Hide resolved
expedient = [numero.numero for numero in rowD]
Juerodriguez marked this conversation as resolved.
Show resolved Hide resolved
try:
for file_id in rowB:
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[0]) + "/" + file_name)
Juerodriguez marked this conversation as resolved.
Show resolved Hide resolved
finally:
for text_id in rowB:
text_single_text = text_id.texto
text_single_title = text_id.titulo
status, result_file = convert_html_to_pdf(text_single_text, tempdir7.name + "/" + text_single_title + ".pdf")
temparchive.write(result_file.name, "upload/" + str(expedient[0]) + "/" + text_single_title + ".pdf")
del tempdir7
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)
result_file = open(output_filename, "w+b")
Juerodriguez marked this conversation as resolved.
Show resolved Hide resolved

# convert HTML to PDF
pisa_status = pisa.CreatePDF(
source_html, # the HTML to convert
dest=result_file) # file handle to recieve result

# close output file
result_file.close()
# 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,
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pytest==6.2.4
playwright==1.11.2
pytest-playwright==0.1.1
pytest-html==3.1.1
pytest-html==3.1.1
xhtml2pdf==0.2.5