From: Christian Heller Date: Wed, 21 Jan 2026 06:25:38 +0000 (+0100) Subject: Reorganize template filenames in their extension suffixes. X-Git-Url: https://plomlompom.com/repos/booking/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/%7B%7Bprefix%7D%7D/index.html?a=commitdiff_plain;h=36b6471ded2e2fe90ac8ccbde855dd5b13d380af;p=ledgplom Reorganize template filenames in their extension suffixes. --- diff --git a/src/ledgplom/http.py b/src/ledgplom/http.py index 5766433..59da038 100644 --- a/src/ledgplom/http.py +++ b/src/ledgplom/http.py @@ -11,16 +11,13 @@ 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 -_SUFFIX_RAW = 'raw' +SUFFIX_HTML = '.html' +SUFFIX_JS = '.js' _SUFFIX_STRUCTURED = 'structured' -PAGENAME_BALANCE = 'balance' -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}' +_PAGENAME_EDIT_STRUCTURED = f'{_PREFIX_EDIT}_{_SUFFIX_STRUCTURED}' def render_ctx_on_path(ledger, short_path_toks): @@ -29,14 +26,14 @@ def render_ctx_on_path(ledger, short_path_toks): prefix, suffix = (pagename + '_').split('_')[:2] method = getattr(ledger, f'view_ctx_{prefix}') method_kwargs = {} - if prefix != PREFIX_LEDGER: + if prefix != _PREFIX_LEDGER: method_kwargs['id_'] = -1 if len(short_path_toks) > 1: id_str = short_path_toks[1] or '-1' if id_str.isdigit() or (id_str[1:].isdigit() and id_str[0] == '-'): method_kwargs['id_'] = int(id_str) if prefix == _PREFIX_EDIT: - method_kwargs['raw'] = suffix == _SUFFIX_RAW + method_kwargs['raw'] = suffix != _SUFFIX_STRUCTURED return method(**method_kwargs) @@ -54,9 +51,6 @@ class _Handler(PlomHttpHandler): 'Handles HTTP requests.' mapper = PlomQueryMap - def _send_rendered(self, tmpl_name: str, ctx: dict[str, Any]) -> None: - self.send_rendered(Path(f'{tmpl_name}.tmpl'), ctx) - def do_POST(self) -> None: # pylint: disable=invalid-name 'Route POST requests to respective handlers.' redir_target = Path(self.path) @@ -65,7 +59,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) @@ -80,7 +74,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: @@ -109,8 +103,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'): @@ -123,11 +117,11 @@ 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: dict[str, Any] = {} - if self.pagename.endswith('.js'): - if self.pagename.startswith(PAGENAME_EDIT_STRUCTURED): + if self.pagename.endswith(SUFFIX_JS): + if self.pagename.startswith(_PAGENAME_EDIT_STRUCTURED): 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'] = ( @@ -144,4 +138,4 @@ class _Handler(PlomHttpHandler): ctx['path'] = self.path ctx['unsaved_changes'] = self.server.ledger.tainted ctx |= render_ctx_on_path(self.server.ledger, self.path_toks[1:]) - self._send_rendered(self.pagename, ctx) + self.send_rendered(Path(f'{self.pagename}{SUFFIX_HTML}'), ctx) diff --git a/src/ledgplom/testing.py b/src/ledgplom/testing.py index 2c17d85..4abdbf6 100644 --- a/src/ledgplom/testing.py +++ b/src/ledgplom/testing.py @@ -2,15 +2,13 @@ # built-ins from sys import exit as sys_exit from pathlib import Path -from typing import Optional +from typing import Any, Optional # requirements.txt from jinja2 import (Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader) # ourselves -from ledgplom.http import ( - PAGENAME_BALANCE, PAGENAME_EDIT_RAW, PAGENAME_EDIT_STRUCTURED, - PAGENAME_LEDGER_RAW, PAGENAME_LEDGER_STRUCTURED, PATH_TEMPLATES, - render_ctx_on_path) +from ledgplom.http import (PATH_TEMPLATES, SUFFIX_HTML, SUFFIX_JS, + render_ctx_on_path) from ledgplom.ledger import Ledger @@ -29,12 +27,6 @@ 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_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) @@ -43,8 +35,15 @@ def run_tests() -> None: and p.parts[-1].startswith( str(dat_path.parts[-1])[:-len(_EXT_DAT)] + '.')]: path_toks = str(test_path.parts[-1]).split('.')[1:] - lines_rendered = templates[path_toks[0]].render( - render_ctx_on_path(ledger, path_toks)).split('\n') + tmpl_name = path_toks[0] + ctx: dict[str, Any] = {} + if tmpl_name.endswith(SUFFIX_JS.replace('.', '_')): + tmpl_name = tmpl_name[:-len(SUFFIX_JS)] + SUFFIX_JS + else: + tmpl_name += SUFFIX_HTML + ctx |= render_ctx_on_path(ledger, path_toks) + lines_rendered = jinja.get_template(tmpl_name).render(ctx + ).split('\n') with test_path.open('r', encoding='utf8') as f: lines_expected = tuple(line.rstrip('\n') for line in f.readlines()) diff --git a/src/templates/balance.html b/src/templates/balance.html new file mode 100644 index 0000000..684fa45 --- /dev/null +++ b/src/templates/balance.html @@ -0,0 +1,103 @@ +{% extends '_base.tmpl' %} + + + +{% block css %} +{{ macros.css_tabular_money() }} +{{ macros.css_balance() }} +td.money table { + float: left; +} +summary::marker { +{{ macros.css_noninput_monospace() -}} +} +summary { + list-style-type: "[…]"; +} +details[open] > summary { + list-style-type: "[^]"; +} +span.indent { + letter-spacing: 3em; +} +{% endblock css %} + + + +{% macro account_with_children(block_id, account, name_indent) %} + +{%- macro _(code_indent=1) %} +{{- " " * 4 * code_indent -}} +{% endmacro %} + +{%- macro tr_money_balance(amount, currency, i=4) %} +{{_(i)}} +{{_(i)}} + {{- amount -}} + +{{_(i)}} + {{- macros.currency_short(currency) -}} + +{{_(i)}} +{% endmacro %} + +{%- if account.get_wealth_at(block_id).moneys | length > 0 %} +{{_()}} +{{_()}} +{##}{% if account.get_wealth_at(block_id).moneys | length == 1 %} +{{_()}} +{######}{% for cur, amt in account.get_wealth_at(block_id).moneys.items() %} + {{- tr_money_balance(amt, cur) -}} +{######}{% endfor %} +{{_()}}
+{##}{% else %} +{{_()}}
+{{_()}} +{{_()}} +{######}{% for cur, amt in account.get_wealth_at(block_id).moneys.items() %} + {{- tr_money_balance(amt, cur, i=6) + if 1 == loop.index -}} +{######}{% endfor %} +{{_()}}
+{{_()}}
+{{_()}} +{######}{% for cur, amt in account.get_wealth_at(block_id).moneys.items() %} + {{- tr_money_balance(amt, cur, 5) if 1 < loop.index -}} +{######}{% endfor %} +{{_()}}
+{{_()}}
+{##}{% endif %} +{{_()}} +{{_()}} +{{_()}} {{ ' '|safe * name_indent }} + {{- ":" if account.parent }} + {{- account.basename }} + {{- ":" if account.children }} +{{_()}} +{{_()}} {{ account.desc }} +{{_()}} +{##}{% for child in account.children_euro_sorted_at(block_id) %} + {{- account_with_children(block_id, child, name_indent=name_indent+1) -}} +{##}{% endfor %} +{% endif %} +{% endmacro %} + + + +{% block content %} +

+ {{ macros.conditional_block_nav('/balance/', 'prev', block) -}}{##} + {{ macros.conditional_block_nav('/balance/', 'next', block) }}{##} + | + balance after {# -#} + {# -#} + booking {{block.id_}} {# -#} + ({{ block.booking.date }}: {{ block.booking.target }}){# -#} + +

+ +{% for root in roots %} +{{ account_with_children(block.id_, root, name_indent=0) -}} +{% endfor %} +
+{% endblock %} diff --git a/src/templates/balance.tmpl b/src/templates/balance.tmpl deleted file mode 100644 index 684fa45..0000000 --- a/src/templates/balance.tmpl +++ /dev/null @@ -1,103 +0,0 @@ -{% extends '_base.tmpl' %} - - - -{% block css %} -{{ macros.css_tabular_money() }} -{{ macros.css_balance() }} -td.money table { - float: left; -} -summary::marker { -{{ macros.css_noninput_monospace() -}} -} -summary { - list-style-type: "[…]"; -} -details[open] > summary { - list-style-type: "[^]"; -} -span.indent { - letter-spacing: 3em; -} -{% endblock css %} - - - -{% macro account_with_children(block_id, account, name_indent) %} - -{%- macro _(code_indent=1) %} -{{- " " * 4 * code_indent -}} -{% endmacro %} - -{%- macro tr_money_balance(amount, currency, i=4) %} -{{_(i)}} -{{_(i)}} - {{- amount -}} - -{{_(i)}} - {{- macros.currency_short(currency) -}} - -{{_(i)}} -{% endmacro %} - -{%- if account.get_wealth_at(block_id).moneys | length > 0 %} -{{_()}} -{{_()}} -{##}{% if account.get_wealth_at(block_id).moneys | length == 1 %} -{{_()}} -{######}{% for cur, amt in account.get_wealth_at(block_id).moneys.items() %} - {{- tr_money_balance(amt, cur) -}} -{######}{% endfor %} -{{_()}}
-{##}{% else %} -{{_()}}
-{{_()}} -{{_()}} -{######}{% for cur, amt in account.get_wealth_at(block_id).moneys.items() %} - {{- tr_money_balance(amt, cur, i=6) - if 1 == loop.index -}} -{######}{% endfor %} -{{_()}}
-{{_()}}
-{{_()}} -{######}{% for cur, amt in account.get_wealth_at(block_id).moneys.items() %} - {{- tr_money_balance(amt, cur, 5) if 1 < loop.index -}} -{######}{% endfor %} -{{_()}}
-{{_()}}
-{##}{% endif %} -{{_()}} -{{_()}} -{{_()}} {{ ' '|safe * name_indent }} - {{- ":" if account.parent }} - {{- account.basename }} - {{- ":" if account.children }} -{{_()}} -{{_()}} {{ account.desc }} -{{_()}} -{##}{% for child in account.children_euro_sorted_at(block_id) %} - {{- account_with_children(block_id, child, name_indent=name_indent+1) -}} -{##}{% endfor %} -{% endif %} -{% endmacro %} - - - -{% block content %} -

- {{ macros.conditional_block_nav('/balance/', 'prev', block) -}}{##} - {{ macros.conditional_block_nav('/balance/', 'next', block) }}{##} - | - balance after {# -#} - {# -#} - booking {{block.id_}} {# -#} - ({{ block.booking.date }}: {{ block.booking.target }}){# -#} - -

- -{% for root in roots %} -{{ account_with_children(block.id_, root, name_indent=0) -}} -{% endfor %} -
-{% endblock %} diff --git a/src/templates/edit_raw.html b/src/templates/edit_raw.html new file mode 100644 index 0000000..f23c182 --- /dev/null +++ b/src/templates/edit_raw.html @@ -0,0 +1,32 @@ +{% extends '_base.tmpl' %} + + + +{% block css %} +{{ macros.css_tabular_money() }} +{{ macros.css_booking_balance() }} +{%- endblock %} + + + +{% block script %} + +{% endblock %} + + + +{% block content %} +{{ macros.edit_bar(block, 'raw', 'structured') }} + + +{{ macros.booking_balance(roots, valid) }} +{% endblock %} diff --git a/src/templates/edit_raw.tmpl b/src/templates/edit_raw.tmpl deleted file mode 100644 index f23c182..0000000 --- a/src/templates/edit_raw.tmpl +++ /dev/null @@ -1,32 +0,0 @@ -{% extends '_base.tmpl' %} - - - -{% block css %} -{{ macros.css_tabular_money() }} -{{ macros.css_booking_balance() }} -{%- endblock %} - - - -{% block script %} - -{% endblock %} - - - -{% block content %} -{{ macros.edit_bar(block, 'raw', 'structured') }} - - -{{ macros.booking_balance(roots, valid) }} -{% endblock %} diff --git a/src/templates/edit_structured.html b/src/templates/edit_structured.html new file mode 100644 index 0000000..59e52d8 --- /dev/null +++ b/src/templates/edit_structured.html @@ -0,0 +1,58 @@ +{% extends '_base.tmpl' %} + + + +{% block css %} +{{ macros.css_tabular_money() }} +{{ macros.css_booking_balance() }} +input.amount { + text-align: right; + font-family: monospace; +} +#date_input { + margin-right: 0.3em; + font-family: monospace; +} +{% endblock %} + + + +{% block script %} + +{% endblock %} + + + +{% block content %} +{{ macros.edit_bar(block, 'structured', 'raw') }} + + + +| + +from + +to + + +
+ +
+
Gap:
+ + + +{% for acc in all_accounts %} + +
+{{ macros.booking_balance(roots, valid) }} +{% endblock %} diff --git a/src/templates/edit_structured.tmpl b/src/templates/edit_structured.tmpl deleted file mode 100644 index 59e52d8..0000000 --- a/src/templates/edit_structured.tmpl +++ /dev/null @@ -1,58 +0,0 @@ -{% extends '_base.tmpl' %} - - - -{% block css %} -{{ macros.css_tabular_money() }} -{{ macros.css_booking_balance() }} -input.amount { - text-align: right; - font-family: monospace; -} -#date_input { - margin-right: 0.3em; - font-family: monospace; -} -{% endblock %} - - - -{% block script %} - -{% endblock %} - - - -{% block content %} -{{ macros.edit_bar(block, 'structured', 'raw') }} - - - -| - -from - -to - - -
- -
-
Gap:
- - - -{% for acc in all_accounts %} - -
-{{ macros.booking_balance(roots, valid) }} -{% endblock %} diff --git a/src/templates/ledger_raw.html b/src/templates/ledger_raw.html new file mode 100644 index 0000000..d421cc2 --- /dev/null +++ b/src/templates/ledger_raw.html @@ -0,0 +1,30 @@ +{% extends '_base.tmpl' %} + + + +{% block css %} +{{ macros.css_ledger() }} +table { + font-family: monospace; +} +{% endblock %} + + + +{% block content %} +
+{{ macros.ledger_empty_lines_fix(has_redundant_empty_lines) -}} + +{% for block in blocks %} +{{ macros.ledger_block_columns('raw', block) -}} +{##}{% for line in block.lines %} + {# -#} + + {{- line.raw }} {# -#} + {# -#} + +{##}{% endfor %} +{% endfor %} +
+
+{% endblock %} diff --git a/src/templates/ledger_raw.tmpl b/src/templates/ledger_raw.tmpl deleted file mode 100644 index d421cc2..0000000 --- a/src/templates/ledger_raw.tmpl +++ /dev/null @@ -1,30 +0,0 @@ -{% extends '_base.tmpl' %} - - - -{% block css %} -{{ macros.css_ledger() }} -table { - font-family: monospace; -} -{% endblock %} - - - -{% block content %} -
-{{ macros.ledger_empty_lines_fix(has_redundant_empty_lines) -}} - -{% for block in blocks %} -{{ macros.ledger_block_columns('raw', block) -}} -{##}{% for line in block.lines %} - {# -#} - - {{- line.raw }} {# -#} - {# -#} - -{##}{% endfor %} -{% endfor %} -
-
-{% endblock %} diff --git a/src/templates/ledger_structured.html b/src/templates/ledger_structured.html new file mode 100644 index 0000000..b045164 --- /dev/null +++ b/src/templates/ledger_structured.html @@ -0,0 +1,50 @@ +{% extends '_base.tmpl' %} + + + +{% block css %} +{{ macros.css_ledger() }} +{{ macros.css_tabular_money() }} +{% endblock %} + + + +{% block content %} +
+{{ macros.ledger_empty_lines_fix(has_redundant_empty_lines) -}} + +{% for block in blocks %} +{{ macros.ledger_block_columns('structured', block) -}} +{##}{% if block.booking %} + + + + +{####}{% for line in block.booking.transfer_lines %} + + + + + {{- line.account -}} + + + +{####}{% endfor %} +{##}{% endif %} +{##}{% for line in block.gap.lines %} + + + +{##}{% endfor %} +{% endfor %} +
+ {{- block.booking.date }} {{ block.booking.target -}} + {{ block.booking.intro_line.comment }}
+ {{- line.amount_short -}} + + {{- macros.currency_short(line.currency) -}} + {{ line.comment }}
{{ line.raw }} 
+ +
+{% endblock %} diff --git a/src/templates/ledger_structured.tmpl b/src/templates/ledger_structured.tmpl deleted file mode 100644 index b045164..0000000 --- a/src/templates/ledger_structured.tmpl +++ /dev/null @@ -1,50 +0,0 @@ -{% extends '_base.tmpl' %} - - - -{% block css %} -{{ macros.css_ledger() }} -{{ macros.css_tabular_money() }} -{% endblock %} - - - -{% block content %} -
-{{ macros.ledger_empty_lines_fix(has_redundant_empty_lines) -}} - -{% for block in blocks %} -{{ macros.ledger_block_columns('structured', block) -}} -{##}{% if block.booking %} - - - - -{####}{% for line in block.booking.transfer_lines %} - - - - - {{- line.account -}} - - - -{####}{% endfor %} -{##}{% endif %} -{##}{% for line in block.gap.lines %} - - - -{##}{% endfor %} -{% endfor %} -
- {{- block.booking.date }} {{ block.booking.target -}} - {{ block.booking.intro_line.comment }}
- {{- line.amount_short -}} - - {{- macros.currency_short(line.currency) -}} - {{ line.comment }}
{{ line.raw }} 
- -
-{% endblock %}