From: Christian Heller Date: Thu, 15 Jan 2026 23:52:17 +0000 (+0100) Subject: Edit (raw) edit page testing, improve templates. X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/%7B%7Bdb.prefix%7D%7D/test?a=commitdiff_plain;h=HEAD;p=ledgplom Edit (raw) edit page testing, improve templates. --- diff --git a/src/ledgplom/http.py b/src/ledgplom/http.py index ce4bfdd..e151b0f 100644 --- a/src/ledgplom/http.py +++ b/src/ledgplom/http.py @@ -11,16 +11,16 @@ from ledgplom.ledger import DEFAULT_INDENT, Ledger PATH_TEMPLATES = Path('templates') _PREFIX_EDIT = 'edit_' _PREFIX_FILE = 'file_' -_PREFIX_LEDGER = 'ledger_' +PREFIX_LEDGER = 'ledger_' _SERVER_HOST = '127.0.0.1' _SERVER_PORT = 8084 -_TOK_RAW = 'raw' -_TOK_STRUCTURED = 'structured' +_SUFFIX_RAW = 'raw' +_SUFFIX_STRUCTURED = 'structured' PAGENAME_BALANCE = 'balance' -_PAGENAME_EDIT_RAW = f'{_PREFIX_EDIT}{_TOK_RAW}' -_PAGENAME_EDIT_STRUCTURED = f'{_PREFIX_EDIT}{_TOK_STRUCTURED}' -PAGENAME_LEDGER_RAW = f'{_PREFIX_LEDGER}{_TOK_RAW}' -PAGENAME_LEDGER_STRUCTURED = f'{_PREFIX_LEDGER}{_TOK_STRUCTURED}' +PAGENAME_EDIT_RAW = f'{_PREFIX_EDIT}{_SUFFIX_RAW}' +PAGENAME_EDIT_STRUCTURED = f'{_PREFIX_EDIT}{_SUFFIX_STRUCTURED}' +PAGENAME_LEDGER_RAW = f'{PREFIX_LEDGER}{_SUFFIX_RAW}' +PAGENAME_LEDGER_STRUCTURED = f'{PREFIX_LEDGER}{_SUFFIX_STRUCTURED}' class Server(PlomHttpServer): @@ -48,7 +48,7 @@ class _Handler(PlomHttpHandler): elif self.pagename.startswith(_PREFIX_EDIT)\ and self.postvars.first('apply'): redir_target = self.post_edit() - elif self.pagename.startswith(_PREFIX_LEDGER): + elif self.pagename.startswith(PREFIX_LEDGER): redir_target = self.post_ledger_action() self.redirect(redir_target) @@ -63,7 +63,7 @@ class _Handler(PlomHttpHandler): 'Based on postvars, edit targeted Booking.' old_id = int(self.path_toks[2]) new_lines = [] - if self.pagename == _PAGENAME_EDIT_STRUCTURED: + if self.pagename == PAGENAME_EDIT_STRUCTURED: line_keys = self.postvars.keys_prefixed('line_') lineno_to_inputs: dict[int, list[str]] = {} for key in line_keys: @@ -92,8 +92,8 @@ class _Handler(PlomHttpHandler): return Path(self.path) if 'add_booking' in self.postvars.as_dict: id_ = self.server.ledger.add_empty_block() - return Path('/', _PAGENAME_EDIT_STRUCTURED, f'{id_}') - keys_prefixed = self.postvars.keys_prefixed(_PREFIX_LEDGER) + return Path('/', PAGENAME_EDIT_STRUCTURED, f'{id_}') + keys_prefixed = self.postvars.keys_prefixed(PREFIX_LEDGER) action, id_str = keys_prefixed[0].split('_', maxsplit=2)[1:] id_ = int(id_str) if action.startswith('move'): @@ -106,15 +106,15 @@ class _Handler(PlomHttpHandler): 'Route GET requests to respective handlers.' if self.pagename == 'blocks': self.redirect( - Path('/', _PAGENAME_EDIT_STRUCTURED, self.path_toks[2])) + Path('/', PAGENAME_EDIT_STRUCTURED, self.path_toks[2])) return ctx = {'unsaved_changes': self.server.ledger.tainted, 'path': self.path} if self.pagename == PAGENAME_BALANCE: self.get_balance(ctx) elif self.pagename.startswith(_PREFIX_EDIT): - self.get_edit(ctx, self.pagename == _PAGENAME_EDIT_RAW) - elif self.pagename.startswith(_PREFIX_LEDGER): + self.get_edit(ctx, self.pagename == PAGENAME_EDIT_RAW) + elif self.pagename.startswith(PREFIX_LEDGER): self.get_ledger(ctx, self.pagename == PAGENAME_LEDGER_RAW) else: self.get_ledger(ctx, False) @@ -130,7 +130,7 @@ class _Handler(PlomHttpHandler): def get_edit(self, ctx, raw: bool) -> None: 'Display edit form for individual Booking.' self._send_rendered( - _PAGENAME_EDIT_RAW if raw else _PAGENAME_EDIT_STRUCTURED, + PAGENAME_EDIT_RAW if raw else PAGENAME_EDIT_STRUCTURED, ctx | self.server.ledger.view_ctx_edit(int(self.path_toks[2]), raw)) diff --git a/src/ledgplom/ledger.py b/src/ledgplom/ledger.py index 6550f79..9a14063 100644 --- a/src/ledgplom/ledger.py +++ b/src/ledgplom/ledger.py @@ -750,7 +750,7 @@ class Ledger: if not ac.parent], key=lambda root: root.basename)} - def view_ctx_edit(self, id_: int, raw: bool) -> dict[str, Any]: + 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_] diff --git a/src/ledgplom/testing.py b/src/ledgplom/testing.py index 3c7d576..f06f154 100644 --- a/src/ledgplom/testing.py +++ b/src/ledgplom/testing.py @@ -2,14 +2,15 @@ # built-ins from sys import exit as sys_exit from pathlib import Path -from typing import Any, Optional +from typing import Optional # requirements.txt from jinja2 import (Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader) # ourselves from ledgplom.http import ( - PAGENAME_BALANCE, PAGENAME_LEDGER_RAW, PAGENAME_LEDGER_STRUCTURED, - PATH_TEMPLATES) + PAGENAME_BALANCE, PAGENAME_EDIT_RAW, PAGENAME_EDIT_STRUCTURED, + PAGENAME_LEDGER_RAW, PAGENAME_LEDGER_STRUCTURED, + PATH_TEMPLATES, PREFIX_LEDGER) from ledgplom.ledger import Ledger @@ -28,24 +29,33 @@ def run_tests() -> None: jinja = JinjaEnv(loader=JinjaFSLoader(PATH_TEMPLATES), autoescape=True, trim_blocks=True) - templates = {item: jinja.get_template(f'{item}.tmpl') for item in ( - PAGENAME_BALANCE, PAGENAME_LEDGER_RAW, PAGENAME_LEDGER_STRUCTURED)} - for path in [p for p in _PATH_TESTS.iterdir() - if p.parts[-1].endswith(_EXT_DAT)]: - ledger = Ledger(path) - for key, template in templates.items(): - test_path = Path(str(path)[:-len(_EXT_DAT)] + f'.{key}') - if not test_path.exists(): + templates = {item: jinja.get_template(f'{item}.tmpl') + for item in (PAGENAME_BALANCE, + PAGENAME_EDIT_RAW, + PAGENAME_EDIT_STRUCTURED, + PAGENAME_LEDGER_RAW, + PAGENAME_LEDGER_STRUCTURED)} + paths = tuple(_PATH_TESTS.iterdir()) + for dat_path in [p for p in paths if p.parts[-1].endswith(_EXT_DAT)]: + ledger = Ledger(dat_path) + for test_path in [p for p in paths + if p != dat_path + and p.parts[-1].startswith( + str(dat_path.parts[-1])[:-len(_EXT_DAT)] + '.')]: + tmpl_name, id_str = (str(test_path.parts[-1]).split('.') + + ['-1'])[1:3] + if tmpl_name not in templates.keys(): continue + tmpl_name_start = tmpl_name.split("_")[0] + lines_rendered = templates[tmpl_name].render( + **getattr(ledger, f'view_ctx_{tmpl_name_start}')( + **({'raw': False} if tmpl_name == PAGENAME_EDIT_STRUCTURED + else {}) | ({} if tmpl_name_start == PREFIX_LEDGER[:-1] + else {'id_': int(id_str)})) + ).split('\n') with test_path.open('r', encoding='utf8') as f: - lines_expected = [line.rstrip('\n') - for line in f.readlines()] - ctx: dict[str, Any] = {} - if key == PAGENAME_BALANCE: - ctx |= ledger.view_ctx_balance(-1) - else: - ctx |= ledger.view_ctx_ledger() - lines_rendered = template.render(**ctx).split('\n') + lines_expected = tuple(line.rstrip('\n') + for line in f.readlines()) msg_prefix = f'test for {test_path}:' for idx0, line in enumerate(lines_rendered): idx1 = idx0 + 1 diff --git a/src/templates/_macros.tmpl b/src/templates/_macros.tmpl index c2495c5..2f01776 100644 --- a/src/templates/_macros.tmpl +++ b/src/templates/_macros.tmpl @@ -178,70 +178,88 @@ function taint() { {% macro edit_bar(block, here, there) %} -
+ -{{conditional_block_nav('/blocks/','prev',block)}} -{{conditional_block_nav('/blocks/','next',block)}} +{{ conditional_block_nav('/blocks/', 'prev', block) -}} +{{ conditional_block_nav('/blocks/', 'next', block) -}} - - + + -switch to {{there}} +switch to {{ there }} · -balance after +balance after · -in ledger +in ledger
{% if block.date_error or (block.booking and block.booking.sink_error) %} -
block-wide errors: - {{block.date_error}} - {% if block.booking %} - {% if block.date_error %}– and:{% endif %} - {{block.booking.sink_error}} - {% endif %} -
-
+
+ block-wide errors: + {{ block.date_error }} +{##}{% if block.booking %} + {{ "– and:" if block.date_error -}} + {{ block.booking.sink_error }} +{##}{% endif %} +
+
{% endif %} {% endmacro %} -{% macro booking_balance(roots, valid) %} -{% macro booking_balance_account_with_children(node) %} - {% macro td_wealth(wealth_dict) %} +{% macro booking_balance(roots, valid) -%} + +{##}{% macro booking_balance_account_with_children(node) %} +{######}{% macro td_wealth(wealth_dict) %} - - {% for curr, amt in wealth_dict.items() %} - - - - - {% endfor %} -
{{amt}}{{ currency_short(curr) }}
+ +{##########}{% for curr, amt in wealth_dict.items() %} + + + + +{##########}{% endfor %} +
+ {{- amt -}} + + {{- currency_short(curr) -}} +
+ {##} +{######}{% endmacro %} + + + {{- node.name }}{{ ':' if node.children -}} - {% endmacro %} +{# #}{{ td_wealth(node.wealth_before) }} +{# #}{{ td_wealth(node.wealth_diff) }} +{# #}{{ td_wealth(node.wealth_after) }} + +{######}{% for child in node.children %} + {{- booking_balance_account_with_children(child) -}} +{######}{% endfor %} +{##}{% endmacro -%} + + - - {{ td_wealth(node.wealth_before) }} - {{ td_wealth(node.wealth_diff) }} - {{ td_wealth(node.wealth_after) }} + + + + - {% for child in node.children %} - {{ booking_balance_account_with_children(child) }} - {% endfor %} -{% endmacro %} -
- {{node.name}}{% if node.children %}:{% endif %} - accountbeforediffafter
- - - - - - {% for root in roots %} - {{ booking_balance_account_with_children(root) }} + {{- booking_balance_account_with_children(root) -}} {% endfor %}
accountbeforediffafter
-{% endmacro %} + +{%- endmacro %} diff --git a/src/templates/edit_raw.tmpl b/src/templates/edit_raw.tmpl index ac68926..af80ee0 100644 --- a/src/templates/edit_raw.tmpl +++ b/src/templates/edit_raw.tmpl @@ -5,21 +5,27 @@ {% block css %} {{ macros.css_tabular_money() }} {{ macros.css_booking_balance() }} -{% endblock %} +{%- endblock %} {% block script %} -{{macros.js_taint()}} +{{ macros.js_taint() }} {% endblock %} {% block content %} -{{macros.edit_bar(block,'raw','structured')}} - +{{ macros.edit_bar(block, 'raw', 'structured') }} + {{ macros.booking_balance(roots, valid) }} {% endblock %} diff --git a/src/templates/edit_structured.tmpl b/src/templates/edit_structured.tmpl index dca7e0a..9921fbd 100644 --- a/src/templates/edit_structured.tmpl +++ b/src/templates/edit_structured.tmpl @@ -18,9 +18,9 @@ input.amount { {% block script %} -{{macros.js_taint()}} -var raw_gap_lines = {{raw_gap_lines|tojson|safe}}; -var booking_lines = {{booking_lines|tojson|safe}}; +{{ macros.js_taint() }} +var raw_gap_lines = {{ raw_gap_lines|tojson|safe }}; +var booking_lines = {{ booking_lines|tojson|safe }}; function new_booking_line(account='', amount='None', currency='') { return { @@ -253,7 +253,7 @@ window.onload = update_form; {% block content %} -{{macros.edit_bar(block,'structured','raw')}} +{{ macros.edit_bar(block, 'structured', 'raw') }} @@ -268,12 +268,17 @@ to
Gap:
- {% for acc in all_accounts %} -
diff --git a/src/tests/full.edit_raw.1 b/src/tests/full.edit_raw.1 new file mode 100644 index 0000000..8c2a3b1 --- /dev/null +++ b/src/tests/full.edit_raw.1 @@ -0,0 +1,217 @@ + + + + + + + + + +
+ +prev +next + + + + +switch to structured +· +balance after +· +in ledger + +
+ + +
+ + + + + + + + + + + + + + + + + + + +
accountbeforediffafter
bar + + + + + +
0€
+
+ + + + + +
-10€
+
+ + + + + +
-10€
+
foo + + + + + +
0€
+
+ + + + + +
10€
+
+ + + + + +
10€
+
+ + diff --git a/src/tests/full.edit_raw.4 b/src/tests/full.edit_raw.4 new file mode 100644 index 0000000..6e793ee --- /dev/null +++ b/src/tests/full.edit_raw.4 @@ -0,0 +1,369 @@ + + + + + + + + + +
+ +prev +next + + + + +switch to structured +· +balance after +· +in ledger + +
+
+ block-wide errors: + date < previous date + – and: +
+
+ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
accountbeforediffafter
bar: + + + + + + + + + +
1€
0USD
+
+ + + + + + + + + +
-10€
-1USD
+
+ + + + + + + + + +
-9€
-1USD
+
bar:x: + + + + + +
0€
+
+ + + + + +
-10€
+
+ + + + + +
-10€
+
bar:x:y + + + + + +
0€
+
+ + + + + +
-10€
+
+ + + + + +
-10€
+
bar:z + + + + + +
0USD
+
+ + + + + +
-1USD
+
+ + + + + +
-1USD
+
foo: + + + + + + + + + +
10€
0USD
+
+ + + + + + + + + +
10€
1USD
+
+ + + + + + + + + +
20€
1USD
+
foo:x + + + + + + + + + +
0€
0USD
+
+ + + + + + + + + +
10€
1USD
+
+ + + + + + + + + +
10€
1USD
+
+ +