Browse Source

removed the xlwt and use the openpyxl for sending the auto email report in xlsx format (#3590)

version-14
Manas Solanki 8 years ago
committed by Rushabh Mehta
parent
commit
b7e1060ea7
7 changed files with 29 additions and 47 deletions
  1. +2
    -2
      frappe/email/doctype/auto_email_report/auto_email_report.json
  2. +11
    -7
      frappe/email/doctype/auto_email_report/auto_email_report.py
  3. +1
    -1
      frappe/email/doctype/auto_email_report/test_auto_email_report.py
  4. +1
    -0
      frappe/patches.txt
  5. +14
    -0
      frappe/patches/v8_1/update_format_options_in_auto_email_report.py
  6. +0
    -36
      frappe/utils/xlsutils.py
  7. +0
    -1
      requirements.txt

+ 2
- 2
frappe/email/doctype/auto_email_report/auto_email_report.json View File

@@ -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",


+ 11
- 7
frappe/email/doctype/auto_email_report/auto_email_report.py View File

@@ -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())


+ 1
- 1
frappe/email/doctype/auto_email_report/test_auto_email_report.py View File

@@ -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()


+ 1
- 0
frappe/patches.txt View File

@@ -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

+ 14
- 0
frappe/patches/v8_1/update_format_options_in_auto_email_report.py View File

@@ -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()

+ 0
- 36
frappe/utils/xlsutils.py View File

@@ -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()

+ 0
- 1
requirements.txt View File

@@ -37,7 +37,6 @@ cryptography
zxcvbn
psutil
unittest-xml-reporting
xlwt
oauthlib
PyJWT
pypdf


Loading…
Cancel
Save