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.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]:
|
||||
if isinstance(row.account, str):
|
||||
return row.account
|
||||
|
@ -321,7 +309,7 @@ class AgingODS(core.BaseODS[AccrualPostings, Optional[data.Account]]):
|
|||
for accrual_type in AccrualAccount:
|
||||
self.use_sheet(accrual_type.name.title())
|
||||
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.add_row(*(
|
||||
self.string_cell(name, stylename=self.style_bold)
|
||||
|
|
|
@ -837,16 +837,15 @@ class BaseODS(BaseSpreadsheet[RT, ST], metaclass=abc.ABCMeta):
|
|||
self.ensure_child(
|
||||
self.style_bold, odf.style.TextProperties, fontweight='bold',
|
||||
)
|
||||
self.style_starttext: odf.style.Style
|
||||
self.style_centertext: odf.style.Style
|
||||
self.style_endtext: odf.style.Style
|
||||
for textalign in ['start', 'center', 'end']:
|
||||
aligned_style = self.replace_child(
|
||||
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_dividerline = self.ensure_child(
|
||||
styles, odf.style.Style, name='DividerLine', family='table-cell',
|
||||
)
|
||||
self.ensure_child(
|
||||
self.style_dividerline,
|
||||
odf.style.TableCellProperties,
|
||||
borderbottom='1pt solid #0000ff',
|
||||
)
|
||||
|
||||
date_style = self.replace_child(styles, odf.number.DateStyle, name='ISODate')
|
||||
date_style.addElement(odf.number.Year(style='long'))
|
||||
date_style.addElement(odf.number.Text(text='-'))
|
||||
|
@ -860,14 +859,31 @@ class BaseODS(BaseSpreadsheet[RT, ST], metaclass=abc.ABCMeta):
|
|||
family='table-cell',
|
||||
datastylename=date_style,
|
||||
)
|
||||
self.style_dividerline = self.ensure_child(
|
||||
styles, odf.style.Style, name='DividerLine', family='table-cell',
|
||||
)
|
||||
self.ensure_child(
|
||||
self.style_dividerline,
|
||||
odf.style.TableCellProperties,
|
||||
borderbottom='1pt solid #0000ff',
|
||||
)
|
||||
|
||||
self.style_starttext: odf.style.Style
|
||||
self.style_centertext: odf.style.Style
|
||||
self.style_endtext: odf.style.Style
|
||||
for textalign in ['start', 'center', 'end']:
|
||||
aligned_style = self.replace_child(
|
||||
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
|
||||
|
||||
|
|
|
@ -261,6 +261,11 @@ def test_ods_currency_style_cache_considers_properties(ods_writer):
|
|||
assert plain.getAttribute('datastylename') != bold.getAttribute('datastylename')
|
||||
|
||||
@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_centertext', odf.style.ParagraphProperties, 'textalign'),
|
||||
('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'),
|
||||
])
|
||||
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)
|
||||
actual = get_child(
|
||||
ods_writer.document.styles,
|
||||
odf.style.Style,
|
||||
name=style.getAttribute('name'),
|
||||
)
|
||||
actual = get_child(root, odf.style.Style, name=style.getAttribute('name'))
|
||||
assert actual is style
|
||||
child = get_child(actual, child_type)
|
||||
assert child.getAttribute(checked_attr)
|
||||
|
|
Loading…
Reference in a new issue