From 83d943be5ff54bc65c455ef987b6aa203c8777bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Tue, 10 May 2016 18:40:02 +0200 Subject: [PATCH] [WIP] mis_builder refactoring: move the "json" conversion to the matrix mis.report.instance.compute() is now as simple as possible, and rest is nicely factored in manageable methods \o/ --- mis_builder/models/mis_builder.py | 96 ++++++++++++++++--------------- 1 file changed, 51 insertions(+), 45 deletions(-) diff --git a/mis_builder/models/mis_builder.py b/mis_builder/models/mis_builder.py index 5a179be9..42f8ae5e 100644 --- a/mis_builder/models/mis_builder.py +++ b/mis_builder/models/mis_builder.py @@ -55,11 +55,15 @@ class KpiMatrixRow(object): else: return None # TODO style for expanded accounts - def iter_cell_tuples(self, cols): + def iter_cell_tuples(self, cols=None): + if cols is None: + cols = self._matrix.iter_cols() for col in cols: yield col.get_cell_tuple_for_row(self) - def iter_cells(self, subcols): + def iter_cells(self, subcols=None): + if subcols is None: + subcols = self._matrix.iter_subcols() for subcol in subcols: yield subcol.get_cell_for_row(self) @@ -307,6 +311,50 @@ class KpiMatrix(object): self._load_account_names() return self._account_names[account_id] + def as_dict(self): + header = [{'cols': []}, {'cols': []}] + for col in self.iter_cols(): + header[0]['cols'].append({ + 'description': col.description, + 'comment': col.comment, + 'colspan': col.colspan, + }) + for subcol in col.iter_subcols(): + header[1]['cols'].append({ + 'description': subcol.description, + 'comment': subcol.comment, + 'colspan': 1, + }) + + content = [] + for row in self.iter_rows(): + row_data = { + 'row_id': id(row), + 'parent_row_id': row.parent_row and id(row.parent_row) or None, + 'description': row.description, + 'comment': row.comment, + 'style': row.style and row.style.to_css_style() or '', + 'cols': [] + } + for cell in row.iter_cells(): + if cell is None: + row_data['cols'].append({}) + else: + row_data['cols'].append({ + 'val': (cell.val + if cell.val is not AccountingNone else None), + 'val_r': cell.val_rendered, + 'val_c': cell.val_comment, + # TODO FIXME style + # TODO FIXME drilldown + }) + content.append(row_data) + + return { + 'header': header, + 'content': content, + } + def _get_selection_label(selection, value): for v, l in selection: @@ -1336,46 +1384,4 @@ class MisReportInstance(models.Model): for comparison_column in period.comparison_column_ids: kpi_matrix.declare_comparison(period.id, comparison_column.id) kpi_matrix.compute_comparisons() - - header = [{'cols': []}, {'cols': []}] - for col in kpi_matrix.iter_cols(): - header[0]['cols'].append({ - 'description': col.description, - 'comment': col.comment, - 'colspan': col.colspan, - }) - for subcol in col.iter_subcols(): - header[1]['cols'].append({ - 'description': subcol.description, - 'comment': subcol.comment, - 'colspan': 1, - }) - - content = [] - for row in kpi_matrix.iter_rows(): - row_data = { - 'row_id': id(row), - 'parent_row_id': row.parent_row and id(row.parent_row) or None, - 'description': row.description, - 'comment': row.comment, - 'style': row.style and row.style.to_css_style() or '', - 'cols': [] - } - for cell in row.iter_cells(kpi_matrix.iter_subcols()): - if cell is None: - row_data['cols'].append({}) - else: - row_data['cols'].append({ - 'val': (cell.val - if cell.val is not AccountingNone else None), - 'val_r': cell.val_rendered, - 'val_c': cell.val_comment, - # TODO FIXME style - # TODO FIXME drilldown - }) - content.append(row_data) - - return { - 'header': header, - 'content': content, - } + return kpi_matrix.as_dict()