From b7e1060ea72342cc30d957a1f389c08e920a7eb8 Mon Sep 17 00:00:00 2001 From: Manas Solanki Date: Mon, 3 Jul 2017 12:27:34 +0530 Subject: [PATCH] removed the xlwt and use the openpyxl for sending the auto email report in xlsx format (#3590) --- .../auto_email_report/auto_email_report.json | 4 +-- .../auto_email_report/auto_email_report.py | 18 ++++++---- .../test_auto_email_report.py | 2 +- frappe/patches.txt | 1 + ...ate_format_options_in_auto_email_report.py | 14 ++++++++ frappe/utils/xlsutils.py | 36 ------------------- requirements.txt | 1 - 7 files changed, 29 insertions(+), 47 deletions(-) create mode 100644 frappe/patches/v8_1/update_format_options_in_auto_email_report.py delete mode 100644 frappe/utils/xlsutils.py diff --git a/frappe/email/doctype/auto_email_report/auto_email_report.json b/frappe/email/doctype/auto_email_report/auto_email_report.json index d37cd56def..03fcbc96e8 100644 --- a/frappe/email/doctype/auto_email_report/auto_email_report.json +++ b/frappe/email/doctype/auto_email_report/auto_email_report.json @@ -585,7 +585,7 @@ "label": "Format", "length": 0, "no_copy": 0, - "options": "HTML\nXLS\nCSV", + "options": "HTML\nXLSX\nCSV", "permlevel": 0, "precision": "", "print_hide": 0, @@ -669,7 +669,7 @@ "issingle": 0, "istable": 0, "max_attachments": 0, - "modified": "2017-04-25 03:31:55.214149", + "modified": "2017-06-30 12:54:13.350902", "modified_by": "Administrator", "module": "Email", "name": "Auto Email Report", diff --git a/frappe/email/doctype/auto_email_report/auto_email_report.py b/frappe/email/doctype/auto_email_report/auto_email_report.py index 3b752b4c2b..79aeff9713 100644 --- a/frappe/email/doctype/auto_email_report/auto_email_report.py +++ b/frappe/email/doctype/auto_email_report/auto_email_report.py @@ -8,7 +8,7 @@ from frappe import _ from frappe.model.document import Document from datetime import timedelta import frappe.utils -from frappe.utils.xlsutils import get_xls +from frappe.utils.xlsxutils import make_xlsx from frappe.utils.csvutils import to_csv max_reports_per_user = 3 @@ -43,7 +43,7 @@ class AutoEmailReport(Document): def validate_report_format(self): """ check if user has select correct report format """ - valid_report_formats = ["HTML", "XLS", "CSV"] + valid_report_formats = ["HTML", "XLSX", "CSV"] if self.format not in valid_report_formats: frappe.throw(_("%s is not a valid report format. Report format should \ one of the following %s"%(frappe.bold(self.format), frappe.bold(", ".join(valid_report_formats))))) @@ -70,11 +70,14 @@ class AutoEmailReport(Document): if self.format == 'HTML': return self.get_html_table(columns, data) - elif self.format == 'XLS': - return get_xls(columns, data) + elif self.format == 'XLSX': + spreadsheet_data = self.get_spreadsheet_data(columns, data) + xlsx_file = make_xlsx(spreadsheet_data, "Auto Email Report") + return xlsx_file.getvalue() elif self.format == 'CSV': - return self.get_csv(columns, data) + spreadsheet_data = self.get_spreadsheet_data(columns, data) + return to_csv(spreadsheet_data) else: frappe.throw(_('Invalid Output Format')) @@ -85,7 +88,8 @@ class AutoEmailReport(Document): 'data': data }) - def get_csv(self, columns, data): + @staticmethod + def get_spreadsheet_data(columns, data): out = [[df.label for df in columns], ] for row in data: new_row = [] @@ -93,7 +97,7 @@ class AutoEmailReport(Document): for df in columns: new_row.append(frappe.format(row[df.fieldname], df, row)) - return to_csv(out) + return out def get_file_name(self): return "{0}.{1}".format(self.report.replace(" ", "-").replace("/", "-"), self.format.lower()) diff --git a/frappe/email/doctype/auto_email_report/test_auto_email_report.py b/frappe/email/doctype/auto_email_report/test_auto_email_report.py index 1436626430..70371965de 100644 --- a/frappe/email/doctype/auto_email_report/test_auto_email_report.py +++ b/frappe/email/doctype/auto_email_report/test_auto_email_report.py @@ -33,7 +33,7 @@ class TestAutoEmailReport(unittest.TestCase): data = auto_email_report.get_report_content() self.assertTrue('"Language","Core"' in data) - auto_email_report.format = 'XLS' + auto_email_report.format = 'XLSX' data = auto_email_report.get_report_content() diff --git a/frappe/patches.txt b/frappe/patches.txt index 9f4ff0bccf..a546b49acd 100644 --- a/frappe/patches.txt +++ b/frappe/patches.txt @@ -186,3 +186,4 @@ frappe.patches.v8_0.update_desktop_icons frappe.patches.v8_0.update_gender_and_salutation execute:frappe.db.sql('update tabReport set module="Desk" where name="ToDo"') frappe.patches.v8_1.enable_allow_error_traceback_in_system_settings +frappe.patches.v8_1.update_format_options_in_auto_email_report diff --git a/frappe/patches/v8_1/update_format_options_in_auto_email_report.py b/frappe/patches/v8_1/update_format_options_in_auto_email_report.py new file mode 100644 index 0000000000..1061d18b07 --- /dev/null +++ b/frappe/patches/v8_1/update_format_options_in_auto_email_report.py @@ -0,0 +1,14 @@ +# Copyright (c) 2017, Frappe and Contributors +# License: GNU General Public License v3. See license.txt + +from __future__ import unicode_literals +import frappe + +def execute(): + """ change the XLS option as XLSX in the auto email report """ + + auto_email_list = frappe.get_all("Auto Email Report", filters={"format": "XLS"}) + for auto_email in auto_email_list: + doc = frappe.get_doc("Auto Email Report", auto_email.name) + doc.format = "XLSX" + doc.save() diff --git a/frappe/utils/xlsutils.py b/frappe/utils/xlsutils.py deleted file mode 100644 index bff7807308..0000000000 --- a/frappe/utils/xlsutils.py +++ /dev/null @@ -1,36 +0,0 @@ -from __future__ import unicode_literals - -import frappe, xlwt, StringIO, datetime -from frappe import _ - -def get_xls(columns, data): - '''Convert data to xls''' - stream = StringIO.StringIO() - workbook = xlwt.Workbook() - sheet = workbook.add_sheet(_("Sheet 1")) - - # formats - dateformat = xlwt.easyxf(num_format_str= - (frappe.defaults.get_global_default("date_format") or "yyyy-mm-dd")) - bold = xlwt.easyxf('font: bold 1') - - # header - for i, col in enumerate(columns): - sheet.write(0, i, col.label, bold) - - for i, row in enumerate(data): - for j, df in enumerate(columns): - f = None - - val = row[columns[j].fieldname] - if isinstance(val, (datetime.datetime, datetime.date)): - f = dateformat - - if f: - sheet.write(i+1, j, val, f) - else: - sheet.write(i+1, j, frappe.format(val, df, row)) - - workbook.save(stream) - stream.seek(0) - return stream.read() diff --git a/requirements.txt b/requirements.txt index e0d7ab4b3d..7113ba325b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -37,7 +37,6 @@ cryptography zxcvbn psutil unittest-xml-reporting -xlwt oauthlib PyJWT pypdf