+
+ def view_ctx_balance(self, id_: int = -1) -> dict[str, Any]:
+ 'All context data relevant for rendering a balance view.'
+ if id_ < 0:
+ id_ = len(self._blocks) - 1
+ return {'valid': self._blocks_valid_up_incl(id_),
+ 'block': self._blocks[id_],
+ 'roots': sorted([ac for ac in self._calc_accounts().values()
+ if not ac.parent],
+ key=lambda root: root.basename)}
+
+ def view_ctx_edit(self, id_: int, raw: bool) -> dict[str, Any]:
+ 'All context data relevant for rendering an edit view.'
+ accounts = self._calc_accounts()
+ block = self._blocks[id_]
+ roots: list[dict[str, Any]] = []
+ for full_path in sorted(block.booking.diffs_targeted.keys()
+ if block.booking else []):
+ assert block.booking is not None
+ node_children: list[dict[str, Any]] = roots
+ for path, _ in _Account.path_to_steps(full_path):
+ already_listed = False
+ for n in [n for n in node_children if path == n['name']]:
+ node_children = n['children']
+ already_listed = True
+ break
+ if already_listed:
+ continue
+ diff = block.booking.diffs_inheriting[path].moneys
+ if (targeted := full_path == path) or diff:
+ pre, post = accounts[path].get_wealth_at_excl_incl(id_)
+ displayed_currs = set(diff.keys())
+ for wealth in pre, post:
+ wealth.ensure_currencies(displayed_currs)
+ wealth.purge_currencies_except(displayed_currs)
+ node: dict[str, Any] = {
+ 'name': path,
+ 'direct_target': targeted,
+ 'wealth_before': pre.moneys,
+ 'wealth_diff': diff,
+ 'wealth_after': post.moneys,
+ 'children': []}
+ node_children += [node]
+ node_children = node['children']
+ ctx = {'block': block,
+ 'valid': self._blocks_valid_up_incl(id_),
+ 'roots': roots}
+ if not raw:
+ ctx['raw_gap_lines'] = [dl.raw for dl in block.gap.lines]
+ ctx['all_accounts'] = sorted(accounts.keys())
+ ctx['booking_lines'] = (
+ [block.booking.intro_line.as_dict]
+ + [tf_line.as_dict for tf_line in block.booking.transfer_lines]
+ ) if block.booking else []
+ return ctx
+
+ def view_ctx_ledger(self) -> dict[str, Any]:
+ 'All context data relevant for rendering a ledger view.'
+ return {key: getattr(self, f'_{key}')
+ for key in ('blocks', 'has_redundant_empty_lines')}