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
77 changes: 72 additions & 5 deletions controllers/expedientes.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# -*- coding: utf-8 -*-
-*- coding: utf-8 -*-
__author__ = "María Andrea Vignau (mavignau@gmail.com)"
__copyright__ = "(C) 2016 María Andrea Vignau. GNU GPL 3."


linked_tables = ['movimiento', 'agenda', 'parte']
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


@auth.requires_login()
Expand Down Expand Up @@ -50,7 +56,7 @@ def index():
constraints={
'expediente': (
db.expediente.created_by == auth.user.id)},
linked_tables=linked_tables,
linked_tables=LINKED_TABLES,
buttons_placement='right',
exportclasses=myexport,
advanced_search=False,
Expand All @@ -64,6 +70,62 @@ def index():
return locals()


@auth.requires_login()
def download():
list_temp_directories = []
list_full_directories = []
file_loc_base = None
zip_filename = 'Movimiento.zip'
tempfiles = io.BytesIO()
temparchive = zipfile.ZipFile(tempfiles, 'w', zipfile.ZIP_DEFLATED)
tempdir7 = tempfile.TemporaryDirectory()
#Obtener ID de expediente y guardarlo en una lista.
rowA = db(db.movimiento).select()
expediente_list = [numero.expediente_id for numero in rowA]
expediente_lists = OrderedDict.fromkeys(expediente_list).keys()

#Separar archivos por el numero de expediente.
for expedientes in expediente_lists:
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)
expedient = [numero.numero for numero in rowD]
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)
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(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")

# 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 All @@ -81,11 +143,16 @@ def vista_expediente():
user_signature=True)
links = [A('Expediente', _href=url, _type='button',
_class='btn btn-default')]
for k in linked_tables:
for k in LINKED_TABLES:
args = ['expediente', '%s.expediente_id' % k, request.args[0]]
url = URL('index', args=args, user_signature=True)
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',
_class='btn btn-default'))

return dict(links=links, expte=expte)

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