reports: Add column width styles to BaseODS.
This commit is contained in:
		
							parent
							
								
									71d671e493
								
							
						
					
					
						commit
						760e0a8cd9
					
				
					 3 changed files with 45 additions and 36 deletions
				
			
		| 
						 | 
					@ -299,18 +299,6 @@ class AgingODS(core.BaseODS[AccrualPostings, Optional[data.Account]]):
 | 
				
			||||||
        self.date = date
 | 
					        self.date = date
 | 
				
			||||||
        self.logger = logger
 | 
					        self.logger = logger
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def init_styles(self) -> None:
 | 
					 | 
				
			||||||
        super().init_styles()
 | 
					 | 
				
			||||||
        self.style_widecol = self.replace_child(
 | 
					 | 
				
			||||||
            self.document.automaticstyles,
 | 
					 | 
				
			||||||
            odf.style.Style,
 | 
					 | 
				
			||||||
            name='WideCol',
 | 
					 | 
				
			||||||
        )
 | 
					 | 
				
			||||||
        self.style_widecol.setAttribute('family', 'table-column')
 | 
					 | 
				
			||||||
        self.style_widecol.addElement(odf.style.TableColumnProperties(
 | 
					 | 
				
			||||||
            columnwidth='1.25in',
 | 
					 | 
				
			||||||
        ))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def section_key(self, row: AccrualPostings) -> Optional[data.Account]:
 | 
					    def section_key(self, row: AccrualPostings) -> Optional[data.Account]:
 | 
				
			||||||
        if isinstance(row.account, str):
 | 
					        if isinstance(row.account, str):
 | 
				
			||||||
            return row.account
 | 
					            return row.account
 | 
				
			||||||
| 
						 | 
					@ -321,7 +309,7 @@ class AgingODS(core.BaseODS[AccrualPostings, Optional[data.Account]]):
 | 
				
			||||||
        for accrual_type in AccrualAccount:
 | 
					        for accrual_type in AccrualAccount:
 | 
				
			||||||
            self.use_sheet(accrual_type.name.title())
 | 
					            self.use_sheet(accrual_type.name.title())
 | 
				
			||||||
            for index in range(self.COL_COUNT):
 | 
					            for index in range(self.COL_COUNT):
 | 
				
			||||||
                stylename = self.style_widecol if index else ''
 | 
					                stylename = self.style_col1_25 if index else ''
 | 
				
			||||||
                self.sheet.addElement(odf.table.TableColumn(stylename=stylename))
 | 
					                self.sheet.addElement(odf.table.TableColumn(stylename=stylename))
 | 
				
			||||||
            self.add_row(*(
 | 
					            self.add_row(*(
 | 
				
			||||||
                self.string_cell(name, stylename=self.style_bold)
 | 
					                self.string_cell(name, stylename=self.style_bold)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -837,16 +837,15 @@ class BaseODS(BaseSpreadsheet[RT, ST], metaclass=abc.ABCMeta):
 | 
				
			||||||
        self.ensure_child(
 | 
					        self.ensure_child(
 | 
				
			||||||
            self.style_bold, odf.style.TextProperties, fontweight='bold',
 | 
					            self.style_bold, odf.style.TextProperties, fontweight='bold',
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
        self.style_starttext: odf.style.Style
 | 
					        self.style_dividerline = self.ensure_child(
 | 
				
			||||||
        self.style_centertext: odf.style.Style
 | 
					            styles, odf.style.Style, name='DividerLine', family='table-cell',
 | 
				
			||||||
        self.style_endtext: odf.style.Style
 | 
					        )
 | 
				
			||||||
        for textalign in ['start', 'center', 'end']:
 | 
					        self.ensure_child(
 | 
				
			||||||
            aligned_style = self.replace_child(
 | 
					            self.style_dividerline,
 | 
				
			||||||
                styles, odf.style.Style, name=f'{textalign.title()}Text',
 | 
					            odf.style.TableCellProperties,
 | 
				
			||||||
            )
 | 
					            borderbottom='1pt solid #0000ff',
 | 
				
			||||||
            aligned_style.setAttribute('family', 'table-cell')
 | 
					        )
 | 
				
			||||||
            aligned_style.addElement(odf.style.ParagraphProperties(textalign=textalign))
 | 
					
 | 
				
			||||||
            setattr(self, f'style_{textalign}text', aligned_style)
 | 
					 | 
				
			||||||
        date_style = self.replace_child(styles, odf.number.DateStyle, name='ISODate')
 | 
					        date_style = self.replace_child(styles, odf.number.DateStyle, name='ISODate')
 | 
				
			||||||
        date_style.addElement(odf.number.Year(style='long'))
 | 
					        date_style.addElement(odf.number.Year(style='long'))
 | 
				
			||||||
        date_style.addElement(odf.number.Text(text='-'))
 | 
					        date_style.addElement(odf.number.Text(text='-'))
 | 
				
			||||||
| 
						 | 
					@ -860,14 +859,31 @@ class BaseODS(BaseSpreadsheet[RT, ST], metaclass=abc.ABCMeta):
 | 
				
			||||||
            family='table-cell',
 | 
					            family='table-cell',
 | 
				
			||||||
            datastylename=date_style,
 | 
					            datastylename=date_style,
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
        self.style_dividerline = self.ensure_child(
 | 
					
 | 
				
			||||||
            styles, odf.style.Style, name='DividerLine', family='table-cell',
 | 
					        self.style_starttext: odf.style.Style
 | 
				
			||||||
        )
 | 
					        self.style_centertext: odf.style.Style
 | 
				
			||||||
        self.ensure_child(
 | 
					        self.style_endtext: odf.style.Style
 | 
				
			||||||
            self.style_dividerline,
 | 
					        for textalign in ['start', 'center', 'end']:
 | 
				
			||||||
            odf.style.TableCellProperties,
 | 
					            aligned_style = self.replace_child(
 | 
				
			||||||
            borderbottom='1pt solid #0000ff',
 | 
					                styles, odf.style.Style, name=f'{textalign.title()}Text',
 | 
				
			||||||
        )
 | 
					            )
 | 
				
			||||||
 | 
					            aligned_style.setAttribute('family', 'table-cell')
 | 
				
			||||||
 | 
					            aligned_style.addElement(odf.style.ParagraphProperties(textalign=textalign))
 | 
				
			||||||
 | 
					            setattr(self, f'style_{textalign}text', aligned_style)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.style_col1: odf.style.Style
 | 
				
			||||||
 | 
					        self.style_col1_25: odf.style.Style
 | 
				
			||||||
 | 
					        self.style_col1_5: odf.style.Style
 | 
				
			||||||
 | 
					        self.style_col1_75: odf.style.Style
 | 
				
			||||||
 | 
					        self.style_col2: odf.style.Style
 | 
				
			||||||
 | 
					        for width in ['1', '1.25', '1.5', '1.75', '2']:
 | 
				
			||||||
 | 
					            width_name = width.replace('.', '_')
 | 
				
			||||||
 | 
					            column_style = self.replace_child(
 | 
				
			||||||
 | 
					                self.document.automaticstyles, odf.style.Style, name=f'col_{width_name}',
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					            column_style.setAttribute('family', 'table-column')
 | 
				
			||||||
 | 
					            column_style.addElement(odf.style.TableColumnProperties(columnwidth=f'{width}in'))
 | 
				
			||||||
 | 
					            setattr(self, f'style_col{width_name}', column_style)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ### Rows and cells
 | 
					    ### Rows and cells
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -261,6 +261,11 @@ def test_ods_currency_style_cache_considers_properties(ods_writer):
 | 
				
			||||||
    assert plain.getAttribute('datastylename') != bold.getAttribute('datastylename')
 | 
					    assert plain.getAttribute('datastylename') != bold.getAttribute('datastylename')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@pytest.mark.parametrize('attr_name,child_type,checked_attr', [
 | 
					@pytest.mark.parametrize('attr_name,child_type,checked_attr', [
 | 
				
			||||||
 | 
					    ('style_col1', odf.style.TableColumnProperties, 'columnwidth'),
 | 
				
			||||||
 | 
					    ('style_col1_25', odf.style.TableColumnProperties, 'columnwidth'),
 | 
				
			||||||
 | 
					    ('style_col1_5', odf.style.TableColumnProperties, 'columnwidth'),
 | 
				
			||||||
 | 
					    ('style_col1_75', odf.style.TableColumnProperties, 'columnwidth'),
 | 
				
			||||||
 | 
					    ('style_col2', odf.style.TableColumnProperties, 'columnwidth'),
 | 
				
			||||||
    ('style_bold', odf.style.TextProperties, 'fontweight'),
 | 
					    ('style_bold', odf.style.TextProperties, 'fontweight'),
 | 
				
			||||||
    ('style_centertext', odf.style.ParagraphProperties, 'textalign'),
 | 
					    ('style_centertext', odf.style.ParagraphProperties, 'textalign'),
 | 
				
			||||||
    ('style_dividerline', odf.style.TableCellProperties, 'borderbottom'),
 | 
					    ('style_dividerline', odf.style.TableCellProperties, 'borderbottom'),
 | 
				
			||||||
| 
						 | 
					@ -268,12 +273,12 @@ def test_ods_currency_style_cache_considers_properties(ods_writer):
 | 
				
			||||||
    ('style_starttext', odf.style.ParagraphProperties, 'textalign'),
 | 
					    ('style_starttext', odf.style.ParagraphProperties, 'textalign'),
 | 
				
			||||||
])
 | 
					])
 | 
				
			||||||
def test_ods_writer_style(ods_writer, attr_name, child_type, checked_attr):
 | 
					def test_ods_writer_style(ods_writer, attr_name, child_type, checked_attr):
 | 
				
			||||||
 | 
					    if child_type is odf.style.TableColumnProperties:
 | 
				
			||||||
 | 
					        root = ods_writer.document.automaticstyles
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
 | 
					        root = ods_writer.document.styles
 | 
				
			||||||
    style = getattr(ods_writer, attr_name)
 | 
					    style = getattr(ods_writer, attr_name)
 | 
				
			||||||
    actual = get_child(
 | 
					    actual = get_child(root, odf.style.Style, name=style.getAttribute('name'))
 | 
				
			||||||
        ods_writer.document.styles,
 | 
					 | 
				
			||||||
        odf.style.Style,
 | 
					 | 
				
			||||||
        name=style.getAttribute('name'),
 | 
					 | 
				
			||||||
    )
 | 
					 | 
				
			||||||
    assert actual is style
 | 
					    assert actual is style
 | 
				
			||||||
    child = get_child(actual, child_type)
 | 
					    child = get_child(actual, child_type)
 | 
				
			||||||
    assert child.getAttribute(checked_attr)
 | 
					    assert child.getAttribute(checked_attr)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue