Make separate method for writing results XLSX file

so subclass can customize
This commit is contained in:
Lance Edgar 2021-11-05 15:11:07 -05:00
parent eb76d868ca
commit 2be1d12116

View file

@ -2722,27 +2722,7 @@ class MasterView(View):
def results_xlsx_session(self): def results_xlsx_session(self):
return self.make_isolated_session() return self.make_isolated_session()
def results_xlsx_thread(self, results, user_uuid, progress): def results_write_xlsx(self, path, fields, results, session, progress=None):
"""
Thread target, responsible for actually generating the Excel file which
is to be presented for download.
"""
route_prefix = self.get_route_prefix()
session = self.results_xlsx_session()
try:
# create folder(s) for output; make sure file doesn't exist
path = os.path.join(self.rattail_config.datadir(), 'downloads',
'results-xlsx', route_prefix,
user_uuid[:2], user_uuid[2:])
if not os.path.exists(path):
os.makedirs(path)
path = os.path.join(path, '{}.xlsx'.format(route_prefix))
if os.path.exists(path):
os.remove(path)
results = results.with_session(session).all()
fields = self.get_xlsx_fields()
writer = ExcelWriter(path, fields, sheet_title=self.get_model_title_plural()) writer = ExcelWriter(path, fields, sheet_title=self.get_model_title_plural())
writer.write_header() writer.write_header()
@ -2765,6 +2745,31 @@ class MasterView(View):
self.progress_loop(finalize, [1], progress, self.progress_loop(finalize, [1], progress,
message="Writing Excel file to disk") message="Writing Excel file to disk")
def results_xlsx_thread(self, results, user_uuid, progress):
"""
Thread target, responsible for actually generating the Excel file which
is to be presented for download.
"""
route_prefix = self.get_route_prefix()
session = self.results_xlsx_session()
try:
# create folder(s) for output; make sure file doesn't exist
path = os.path.join(self.rattail_config.datadir(), 'downloads',
'results-xlsx', route_prefix,
user_uuid[:2], user_uuid[2:])
if not os.path.exists(path):
os.makedirs(path)
path = os.path.join(path, '{}.xlsx'.format(route_prefix))
if os.path.exists(path):
os.remove(path)
results = results.with_session(session).all()
fields = self.get_xlsx_fields()
# write output file
self.results_write_xlsx(path, fields, results, session, progress=progress)
except Exception as error: except Exception as error:
msg = "generating XLSX file for download failed!" msg = "generating XLSX file for download failed!"
log.warning(msg, exc_info=True) log.warning(msg, exc_info=True)