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,6 +2722,29 @@ class MasterView(View):
def results_xlsx_session(self):
return self.make_isolated_session()
def results_write_xlsx(self, path, fields, results, session, progress=None):
writer = ExcelWriter(path, fields, sheet_title=self.get_model_title_plural())
writer.write_header()
rows = []
def write(obj, i):
data = self.get_xlsx_row(obj, fields)
row = [data[field] for field in fields]
rows.append(row)
self.progress_loop(write, results, progress,
message="Collecting data for Excel")
def finalize(x, i):
writer.write_rows(rows)
writer.auto_freeze()
writer.auto_filter()
writer.auto_resize()
writer.save()
self.progress_loop(finalize, [1], progress,
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
@ -2743,27 +2766,9 @@ class MasterView(View):
results = results.with_session(session).all()
fields = self.get_xlsx_fields()
writer = ExcelWriter(path, fields, sheet_title=self.get_model_title_plural())
writer.write_header()
rows = []
def write(obj, i):
data = self.get_xlsx_row(obj, fields)
row = [data[field] for field in fields]
rows.append(row)
self.progress_loop(write, results, progress,
message="Collecting data for Excel")
def finalize(x, i):
writer.write_rows(rows)
writer.auto_freeze()
writer.auto_filter()
writer.auto_resize()
writer.save()
self.progress_loop(finalize, [1], progress,
message="Writing Excel file to disk")
# write output file
self.results_write_xlsx(path, fields, results, session, progress=progress)
except Exception as error:
msg = "generating XLSX file for download failed!"