Changeset - b59056dcab46
[Not reviewed]
0 1 0
Lance Edgar (lance) - 3 years ago 2022-01-26 16:24:13
lance@edbob.org
Allow report output fields to vary based on params
1 file changed with 14 insertions and 7 deletions:
0 comments (0 inline, 0 general)
rattail/reporting/excel.py
Show inline comments
 
@@ -2,7 +2,7 @@
 
################################################################################
 
#
 
#  Rattail -- Retail Software Framework
 
#  Copyright © 2010-2021 Lance Edgar
 
#  Copyright © 2010-2022 Lance Edgar
 
#
 
#  This file is part of Rattail.
 
#
 
@@ -59,11 +59,17 @@ class ExcelReport(Report):
 
    def make_filename(self, session, params, data, **kwargs):
 
        return "{}.xlsx".format(self.name)
 

	
 
    def make_excel_writer(self, path, **kwargs):
 
    def get_output_fields(self, params):
 
        return list(self.output_fields)
 

	
 
    def make_excel_writer(self, path, params={}, **kwargs):
 
        """
 
        Create the Excel writer instance, for the given path.
 
        """
 
        fields = kwargs.pop('fields', self.output_fields)
 
        if 'fields' in kwargs:
 
            fields = kwargs.pop('fields')
 
        else:
 
            fields = self.get_output_fields(params)
 
        kwargs.setdefault('number_formats', self.number_formats)
 
        kwargs.setdefault('sheet_title', "Report Data")
 
        return ExcelWriter(path, fields, **kwargs)
 
@@ -74,11 +80,12 @@ class ExcelReport(Report):
 
        Write the primary data sheet for the Excel output file.
 
        """
 
        writer.write_header()
 
        fields = self.get_output_fields(params)
 

	
 
        # convert data to Excel-compatible rows
 
        data_rows = data if isinstance(data, list) else data['rows']
 
        xlrows = [
 
            [row[field] for field in self.output_fields]
 
            [row[field] for field in fields]
 
            for row in data_rows]
 

	
 
        # write main data rows
 
@@ -92,7 +99,7 @@ class ExcelReport(Report):
 

	
 
            # create totals row data
 
            rowdata = []
 
            for field in self.output_fields:
 
            for field in fields:
 
                if field in totals:
 
                    rowdata.append(totals[field])
 
                else:
 
@@ -105,7 +112,7 @@ class ExcelReport(Report):
 
            fill_totals = PatternFill(patternType='solid',
 
                                      fgColor='ffee88',
 
                                      bgColor='ffee88')
 
            for col, field in enumerate(self.output_fields, 1):
 
            for col, field in enumerate(fields, 1):
 
                cell = writer.sheet.cell(row=len(data_rows) + 2, column=col)
 
                cell.fill = fill_totals
 

	
 
@@ -148,7 +155,7 @@ class ExcelReport(Report):
 
        Write a basic Excel output file with the given data.  Requires at least
 
        the ``output_fields`` attribute to be set to work correctly.
 
        """
 
        writer = self.make_excel_writer(path)
 
        writer = self.make_excel_writer(path, params=params)
 

	
 
        self.write_data_sheet(writer, session, params, data, progress=progress)
 

	
0 comments (0 inline, 0 general)