refactor: zip_and_download (#23844)

This commit is contained in:
Raffael Meyer 2020-11-09 15:47:56 +01:00 committed by GitHub
parent f38a1c305e
commit 4f880e7454
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 10 deletions

View File

@ -155,20 +155,22 @@ def get_header(filters, csv_class):
return header return header
def download_csv_files_as_zip(csv_data_list): def zip_and_download(zip_filename, csv_files):
""" """
Put CSV files in a zip archive and send that to the client. Put CSV files in a zip archive and send that to the client.
Params: Params:
csv_data_list -- list of dicts [{'file_name': 'EXTF_Buchunsstapel.zip', 'csv_data': get_datev_csv()}] zip_filename Name of the zip file
csv_files list of dicts [{'file_name': 'my_file.csv', 'csv_data': 'comma,separated,values'}]
""" """
zip_buffer = BytesIO() zip_buffer = BytesIO()
datev_zip = zipfile.ZipFile(zip_buffer, mode='w', compression=zipfile.ZIP_DEFLATED) zip_file = zipfile.ZipFile(zip_buffer, mode='w', compression=zipfile.ZIP_DEFLATED)
for csv_file in csv_data_list: for csv_file in csv_files:
datev_zip.writestr(csv_file.get('file_name'), csv_file.get('csv_data')) zip_file.writestr(csv_file.get('file_name'), csv_file.get('csv_data'))
datev_zip.close()
zip_file.close()
frappe.response['filecontent'] = zip_buffer.getvalue() frappe.response['filecontent'] = zip_buffer.getvalue()
frappe.response['filename'] = 'DATEV.zip' frappe.response['filename'] = zip_filename
frappe.response['type'] = 'binary' frappe.response['type'] = 'binary'

View File

@ -11,10 +11,11 @@ from __future__ import unicode_literals
import json import json
import frappe import frappe
from frappe import _
from six import string_types from six import string_types
from frappe import _
from erpnext.accounts.utils import get_fiscal_year from erpnext.accounts.utils import get_fiscal_year
from erpnext.regional.germany.utils.datev.datev_csv import download_csv_files_as_zip, get_datev_csv from erpnext.regional.germany.utils.datev.datev_csv import zip_and_download, get_datev_csv
from erpnext.regional.germany.utils.datev.datev_constants import Transactions, DebtorsCreditors, AccountNames from erpnext.regional.germany.utils.datev.datev_constants import Transactions, DebtorsCreditors, AccountNames
COLUMNS = [ COLUMNS = [
@ -344,7 +345,8 @@ def download_datev_csv(filters):
customers = get_customers(filters) customers = get_customers(filters)
suppliers = get_suppliers(filters) suppliers = get_suppliers(filters)
download_csv_files_as_zip([ zip_name = '{} DATEV.zip'.format(frappe.utils.datetime.date.today())
zip_and_download(zip_name, [
{ {
'file_name': 'EXTF_Buchungsstapel.csv', 'file_name': 'EXTF_Buchungsstapel.csv',
'csv_data': get_datev_csv(transactions, filters, csv_class=Transactions) 'csv_data': get_datev_csv(transactions, filters, csv_class=Transactions)