add: subTotals support
This commit is contained in:
		
							parent
							
								
									1a58bb60c6
								
							
						
					
					
						commit
						0efbc5db1b
					
				
					 3 changed files with 42 additions and 1 deletions
				
			
		| 
						 | 
					@ -140,6 +140,12 @@ class Report(Template):
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    def getTotalsFields(self):
 | 
					    def getTotalsFields(self):
 | 
				
			||||||
        return [f for f in self.fields if 'totals' in f.executionSteps]
 | 
					        return [f for f in self.fields if 'totals' in f.executionSteps]
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    def getSubTotalsFields(self):
 | 
				
			||||||
 | 
					        result = []
 | 
				
			||||||
 | 
					        for gf in self.getGroupFields():
 | 
				
			||||||
 | 
					            result.append([f for f in self.fields if gf.name in f.totals])
 | 
				
			||||||
 | 
					        return result
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class BaseQueryCriteria(Component):
 | 
					class BaseQueryCriteria(Component):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -78,6 +78,7 @@ class Field(Component):
 | 
				
			||||||
    outputWith = ()
 | 
					    outputWith = ()
 | 
				
			||||||
    style = TableCellStyle()
 | 
					    style = TableCellStyle()
 | 
				
			||||||
    cssClass = ''
 | 
					    cssClass = ''
 | 
				
			||||||
 | 
					    totals = []
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    executionSteps = ['query', 'sort', 'output']    # , 'totals']
 | 
					    executionSteps = ['query', 'sort', 'output']    # , 'totals']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -75,6 +75,16 @@ class GroupHeaderRow(BaseRow):
 | 
				
			||||||
                if f.name == col.name:
 | 
					                if f.name == col.name:
 | 
				
			||||||
                    fields[idx] = col
 | 
					                    fields[idx] = col
 | 
				
			||||||
        return fields
 | 
					        return fields
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					class SubTotalsRow(BaseRow):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def getRawValue(self, attr):
 | 
				
			||||||
 | 
					        return self.data.get(attr, u'')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Lazy
 | 
				
			||||||
 | 
					    def displayedColumns(self):
 | 
				
			||||||
 | 
					        return self.parent.context.getActiveOutputFields()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ResultSet(object):
 | 
					class ResultSet(object):
 | 
				
			||||||
| 
						 | 
					@ -103,6 +113,12 @@ class ResultSet(object):
 | 
				
			||||||
                headerColumn.cssClass = c.cssClass
 | 
					                headerColumn.cssClass = c.cssClass
 | 
				
			||||||
                headerRow.headerColumns.append(headerColumn)
 | 
					                headerRow.headerColumns.append(headerColumn)
 | 
				
			||||||
        return headerRow
 | 
					        return headerRow
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    def getSubTotalsRow(self, row, columns, values):
 | 
				
			||||||
 | 
					        subTotalsRow = SubTotalsRow(None, self)
 | 
				
			||||||
 | 
					        for idx, c in enumerate(columns):
 | 
				
			||||||
 | 
					            subTotalsRow.data[c.name] = values[idx]
 | 
				
			||||||
 | 
					        return subTotalsRow
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
    def getResult(self):
 | 
					    def getResult(self):
 | 
				
			||||||
        result = [self.rowFactory(item, self) for item in self.data]
 | 
					        result = [self.rowFactory(item, self) for item in self.data]
 | 
				
			||||||
| 
						 | 
					@ -113,13 +129,26 @@ class ResultSet(object):
 | 
				
			||||||
        if self.groupColumns:
 | 
					        if self.groupColumns:
 | 
				
			||||||
            res = []
 | 
					            res = []
 | 
				
			||||||
            groupValues = [None for f in self.groupColumns]
 | 
					            groupValues = [None for f in self.groupColumns]
 | 
				
			||||||
 | 
					            subTotals = [[0.0 for f in stc] for stc in self.subTotalsColumns]
 | 
				
			||||||
            for row in result:
 | 
					            for row in result:
 | 
				
			||||||
 | 
					                subTotalsRows = []
 | 
				
			||||||
 | 
					                headerRows = []
 | 
				
			||||||
                for idx, f in enumerate(self.groupColumns):
 | 
					                for idx, f in enumerate(self.groupColumns):
 | 
				
			||||||
                    value = f.getRawValue(row)
 | 
					                    value = f.getRawValue(row)
 | 
				
			||||||
                    if value != groupValues[idx]:
 | 
					                    if value != groupValues[idx]:
 | 
				
			||||||
                        groupValues[idx] = value
 | 
					                        groupValues[idx] = value
 | 
				
			||||||
                        res.append(self.getHeaderRow(row, (f,) + f.outputWith))
 | 
					                        headerRows.append(self.getHeaderRow(row, (f,) + f.outputWith))
 | 
				
			||||||
 | 
					                        subTotalsRows.append(self.getSubTotalsRow(
 | 
				
			||||||
 | 
					                            row, self.subTotalsColumns[idx], subTotals[idx]))
 | 
				
			||||||
 | 
					                        subTotals[idx] = [0.0 for f in self.subTotalsColumns[idx]]
 | 
				
			||||||
 | 
					                for subTotalsRow in reversed(subTotalsRows):
 | 
				
			||||||
 | 
					                    res.append(subTotalsRow)
 | 
				
			||||||
 | 
					                for headerRow in headerRows:
 | 
				
			||||||
 | 
					                    res.append(headerRow)
 | 
				
			||||||
                res.append(row)
 | 
					                res.append(row)
 | 
				
			||||||
 | 
					                for idx, sc in enumerate(self.subTotalsColumns):
 | 
				
			||||||
 | 
					                    for idx2, f in enumerate(sc):
 | 
				
			||||||
 | 
					                        subTotals[idx][idx2] += f.getValue(row)
 | 
				
			||||||
            result = res
 | 
					            result = res
 | 
				
			||||||
        if self.limits:
 | 
					        if self.limits:
 | 
				
			||||||
            start, stop = self.limits
 | 
					            start, stop = self.limits
 | 
				
			||||||
| 
						 | 
					@ -147,3 +176,8 @@ class ResultSet(object):
 | 
				
			||||||
    @Lazy
 | 
					    @Lazy
 | 
				
			||||||
    def groupColumns(self):
 | 
					    def groupColumns(self):
 | 
				
			||||||
        return self.context.getGroupFields()
 | 
					        return self.context.getGroupFields()
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    @Lazy
 | 
				
			||||||
 | 
					    def subTotalsColumns(self):
 | 
				
			||||||
 | 
					        return self.context.getSubTotalsFields()
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue