def _write_invoice_on_disk(request, document, destdir, prefix, key): """ Write an invoice on disk :param obj request: The current env request instance :param obj document: The document instance (invoice/cancelinvoice) :param str destdir: An existing dest directory :param str prefix: The prefix to add before dest file name :param str key: A key used to make the name unique """ from autonomie.views.task.views import ( html, ) from autonomie.utils.pdf import buffer_pdf from pyramid_layout.config import create_layout_manager class A: def __init__(self, req): self.request = req logger = logging.getLogger(__name__) dest_file = u"%s_%s_%s.pdf" % (prefix, document.official_number, key) filepath = os.path.join(destdir, dest_file) logger.debug("Writing : %s" % filepath) request.context = document create_layout_manager(A(request)) html_str = html(request, tasks=[document], bulk=True) with file(filepath, 'wb') as fbuf: fbuf.write(buffer_pdf(html_str).read())
def invoices_pdf_view(request): """ Bulk pdf output : output a large amount of invoices/cancelinvoices """ # We retrieve the form query_form = get_invoice_pdf_export_form(request) if 'submit' in request.params: try: appstruct = query_form.validate(request.params.items()) except ValidationFailure as e: # Form validation failed, the error contains the form with the error # messages query_form = e appstruct = None if appstruct is not None: # The form has been validated, we can query for documents start_number = appstruct["start"] end_number = appstruct["end"] year = appstruct['year'] documents = query_documents_for_export(start_number, end_number, year) # We've got some documents to export if documents: # Getting the html output html_string = html(request, documents, bulk=True) filename = u"factures_{0}_{1}_{2}.pdf".format( year, start_number, end_number, ) try: # Placing the pdf datas in the request write_pdf(request, filename, html_string) return request.response except BaseException as e: import traceback traceback.print_exc() request.session.flash(u"Erreur à l'export des factures, \ essayez de limiter le nombre de factures à exporter. Prévenez \ votre administrateur si le problème persiste.", queue="error") else: # There were no documents to export, we send a message to the # end user request.session.flash(u"Aucune facture n'a pu être retrouvée", queue="error") gotolist_btn = ViewLink(u"Liste des factures", "admin_invoices", path="invoices") request.actionmenu.add(gotolist_btn) return dict( title=u"Export massif de factures au format PDF", form=query_form.render(), )
def _write_invoice_on_disk(request, document, destdir, prefix, key): """ Write an invoice on disk :param obj request: The current env request instance :param obj document: The document instance (invoice/cancelinvoice) :param str destdir: An existing dest directory :param str prefix: The prefix to add before dest file name :param str key: A key used to make the name unique """ from autonomie.views.task.views import ( html, ) from autonomie.utils.pdf import buffer_pdf from pyramid_layout.config import create_layout_manager class A: def __init__(self, req): self.request = req logger = logging.getLogger(__name__) dest_file = u"%s_%s_%s.pdf" % (prefix, document.official_number, key) filepath = os.path.join(destdir, dest_file) logger.debug("Writing : %s" % filepath) request.context = document create_layout_manager(A(request)) html_str = html(request, tasks=[document], bulk=True) with file(filepath, 'wb') as fbuf: fbuf.write(buffer_pdf(html_str).read())
def invoices_pdf_view(request): """ Bulk pdf output : output a large amount of invoices/cancelinvoices """ # We retrieve the form query_form = get_invoice_pdf_export_form(request) if 'submit' in request.params: try: appstruct = query_form.validate(request.params.items()) except ValidationFailure as e: # Form validation failed, the error contains the form with the error # messages query_form = e appstruct = None if appstruct is not None: # The form has been validated, we can query for documents start_number = appstruct["start"] end_number = appstruct["end"] year = appstruct['year'] documents = query_documents_for_export( start_number, end_number, year ) # We've got some documents to export if documents: # Getting the html output html_string = html(request, documents, bulk=True) filename = u"factures_{0}_{1}_{2}.pdf".format( year, start_number, end_number, ) try: # Placing the pdf datas in the request write_pdf(request, filename, html_string) return request.response except BaseException as e: import traceback traceback.print_exc() request.session.flash(u"Erreur à l'export des factures, \ essayez de limiter le nombre de factures à exporter. Prévenez \ votre administrateur si le problème persiste.", queue="error") else: # There were no documents to export, we send a message to the # end user request.session.flash( u"Aucune facture n'a pu être retrouvée", queue="error" ) gotolist_btn = ViewLink( u"Liste des factures", "admin_invoices", path="invoices" ) request.actionmenu.add(gotolist_btn) return dict( title=u"Export massif de factures au format PDF", form=query_form.render(), )