From b9e7da55e4e1b4242fae124b77b440fea5b24e4b Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Mon, 19 Jan 2026 03:38:16 +0100 Subject: [PATCH] Move javascript code into separate module files. --- src/ledgplom/http.py | 19 +- src/ledgplom/ledger.py | 36 ++- src/templates/_base.tmpl | 4 +- src/templates/_macros.tmpl | 58 +--- src/templates/edit_raw.tmpl | 5 +- src/templates/edit_structured.js | 441 +++++++++++++++++++++++++++++ src/templates/edit_structured.tmpl | 242 +--------------- src/templates/taint.js | 90 ++++++ src/tests/empty.balance | 4 +- src/tests/empty.ledger_raw | 4 +- src/tests/empty.ledger_structured | 4 +- src/tests/full.balance | 4 +- src/tests/full.edit_raw.1 | 60 +--- src/tests/full.edit_raw.4 | 60 +--- src/tests/full.ledger_raw | 4 +- src/tests/full.ledger_structured | 4 +- 16 files changed, 600 insertions(+), 439 deletions(-) create mode 100644 src/templates/edit_structured.js create mode 100644 src/templates/taint.js diff --git a/src/ledgplom/http.py b/src/ledgplom/http.py index e151b0f..3d98991 100644 --- a/src/ledgplom/http.py +++ b/src/ledgplom/http.py @@ -110,7 +110,9 @@ class _Handler(PlomHttpHandler): return ctx = {'unsaved_changes': self.server.ledger.tainted, 'path': self.path} - if self.pagename == PAGENAME_BALANCE: + if self.pagename.endswith('.js'): + self.get_js() + elif self.pagename == PAGENAME_BALANCE: self.get_balance(ctx) elif self.pagename.startswith(_PREFIX_EDIT): self.get_edit(ctx, self.pagename == PAGENAME_EDIT_RAW) @@ -119,6 +121,21 @@ class _Handler(PlomHttpHandler): else: self.get_ledger(ctx, False) + def get_js(self) -> None: + 'Deliver .js module.' + ctx = {} + if self.pagename == PAGENAME_EDIT_STRUCTURED + '.js': + block = self.server.ledger.blocks[int(self.path_toks[2])] + ctx['raw_gap_lines'] = [dl.raw for dl in block.gap.lines] + 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 [] + self.send_http( + bytes(self.server.jinja.get_template(self.pagename).render(**ctx), + encoding='utf8'), + [('Content-Type', 'text/javascript')]) + def get_balance(self, ctx) -> None: 'Display tree of calculated Accounts over blocks up_incl+1.' id_str = self.params.first('up_incl') diff --git a/src/ledgplom/ledger.py b/src/ledgplom/ledger.py index 9a14063..fcacf2f 100644 --- a/src/ledgplom/ledger.py +++ b/src/ledgplom/ledger.py @@ -605,7 +605,7 @@ class Ledger: self.last_save_hash = self._hash_dat_lines() @property - def _blocks(self) -> list[_DatBlock]: + def blocks(self) -> list[_DatBlock]: 'Return blocks chain as list.' blocks = [] block = self._blocks_start @@ -616,9 +616,9 @@ class Ledger: @property def _dat_lines(self) -> list[_DatLine]: - 'From ._blocks build list of current _DatLines.' + 'From .blocks build list of current _DatLines.' lines = [] - for block in self._blocks: + for block in self.blocks: lines += block.lines return [_DatLine(line.code, line.comment) for line in lines] @@ -639,7 +639,7 @@ class Ledger: for acc_name, desc in dat_line.comment_instructions.items(): ensure_accounts(acc_name) accounts[acc_name].desc = desc - for block in [b for b in self._blocks if b.booking]: + for block in [b for b in self.blocks if b.booking]: assert block.booking is not None for acc_name, wealth in block.booking.diffs_targeted.items(): ensure_accounts(acc_name) @@ -657,7 +657,7 @@ class Ledger: def _blocks_valid_up_incl(self, block_id: int) -> bool: 'Whether nothing questionable about blocks until block_id.' - for block in self._blocks[:block_id]: + for block in self.blocks[:block_id]: if block.booking: if block.booking.sink_error: return False @@ -670,11 +670,11 @@ class Ledger: @property def _has_redundant_empty_lines(self) -> bool: 'If any gaps have redunant empty lines.' - return bool([b for b in self._blocks if b.gap.redundant_empty_lines]) + return bool([b for b in self.blocks if b.gap.redundant_empty_lines]) def remove_redundant_empty_lines(self) -> None: - 'From all ._blocks remove redundant empty lines.' - for gap in [b.gap for b in self._blocks + 'From all .blocks remove redundant empty lines.' + for gap in [b.gap for b in self.blocks if b.gap.redundant_empty_lines]: gap.remove_redundant_empty_lines() @@ -685,7 +685,7 @@ class Ledger: def move_block(self, idx_from: int, up: bool) -> int: 'Move _DatBlock of idx_from step up or downwards.' - block = self._blocks[idx_from] + block = self.blocks[idx_from] block.move(up) return block.id_ @@ -707,7 +707,7 @@ class Ledger: lines_gap_pre_booking += [_GapLine.from_dat(dat_line)] else: lines_gap_post_booking += [_GapLine.from_dat(dat_line)] - old_block = self._blocks[old_id] + old_block = self.blocks[old_id] if not lines_booking: if old_block.prev: old_block.prev.gap.add(lines_gap_pre_booking) @@ -718,7 +718,7 @@ class Ledger: return max(0, old_id - 1) new_block = _DatBlock(_Booking(lines_booking), _Gap(lines_gap_post_booking)) - self._blocks[old_id].replace_with(new_block) + self.blocks[old_id].replace_with(new_block) if not new_block.prev: self._blocks_start = _DatBlock(None, _Gap()) self._blocks_start.next = new_block @@ -731,21 +731,21 @@ class Ledger: 'Add new _DatBlock of empty _Booking to end of ledger.' new_block = _DatBlock( _Booking([_IntroLine(dt_date.today().isoformat(), '?')])) - self._blocks[-1].next = new_block + self.blocks[-1].next = new_block new_block.fix_position() return new_block.id_ def copy_block(self, id_: int) -> int: 'Add copy _DatBlock of id_ but with current date.' - copy = self._blocks[id_].copy_to_current_date() + copy = self.blocks[id_].copy_to_current_date() return copy.id_ 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 + id_ = len(self.blocks) - 1 return {'valid': self._blocks_valid_up_incl(id_), - 'block': self._blocks[id_], + 'block': self.blocks[id_], 'roots': sorted([ac for ac in self._calc_accounts().values() if not ac.parent], key=lambda root: root.basename)} @@ -753,7 +753,7 @@ class Ledger: def view_ctx_edit(self, id_: int, raw=True) -> dict[str, Any]: 'All context data relevant for rendering an edit view.' accounts = self._calc_accounts() - block = self._blocks[id_] + block = self.blocks[id_] roots: list[dict[str, Any]] = [] for full_path in sorted(block.booking.diffs_targeted.keys() if block.booking else []): @@ -789,10 +789,6 @@ class Ledger: 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]: diff --git a/src/templates/_base.tmpl b/src/templates/_base.tmpl index 0d7534f..6e600bc 100644 --- a/src/templates/_base.tmpl +++ b/src/templates/_base.tmpl @@ -3,10 +3,8 @@ -