From: Christian Heller Date: Wed, 29 Jan 2025 00:50:36 +0000 (+0100) Subject: To ledger view, add movement of Bookings up and down. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/static/%7Broute%7D?a=commitdiff_plain;h=c4ca973ff2899d919ec0e5ec365756cfb71ba9b2;p=plomledger To ledger view, add movement of Bookings up and down. --- diff --git a/ledger.py b/ledger.py index 098c6f6..0c6ed5e 100755 --- a/ledger.py +++ b/ledger.py @@ -259,6 +259,13 @@ class Handler(PlomHttpHandler): self.server.load() elif 'save_file' in self.postvars.as_dict: self.server.save() + elif self.pagename.startswith('ledger_'): + for key in self.postvars.keys_prefixed('move_'): + toks = key.split('_') + id_ = int(toks[1]) + self.server.move_booking(id_, up=toks[2] == 'up') + self.redirect(Path(self.pagename).joinpath(f'#{id_}')) + return elif self.pagename == 'edit_structured': if self.postvars.first('apply'): line_keys = self.postvars.keys_prefixed('line_') @@ -305,6 +312,8 @@ class Handler(PlomHttpHandler): id_ = int(self.path_toks[2]) if self.pagename.startswith('edit_'): ctx['id'] = id_ + elif self.pagename.startswith('ledger_'): + ctx['max_id'] = self.server.bookings[-1].id_ if self.pagename == 'balance': id_ = int(self.params.first('up_incl') or '-1') valid, balance_roots = self.server.balance_roots(id_) @@ -380,15 +389,41 @@ class Server(PlomHttpServer): '\n'.join([line.raw for line in self.dat_lines]), encoding='utf8') self.load() - def rewrite_booking(self, id_: int, new_dat_lines: list[DatLine]) -> None: - """Rewrite .dat_lines for Booking of .id_ with new_dat_lines.""" - old_booking = self.bookings[id_] - start_idx = self.dat_lines.index(old_booking.dat_lines[0]) - end_idx = self.dat_lines.index(old_booking.dat_lines[-1]) + def _margin_indices(self, booking: Booking) -> tuple[int, int]: + start_idx = self.dat_lines.index(booking.dat_lines[0]) + end_idx = self.dat_lines.index(booking.dat_lines[-1]) + return start_idx, end_idx + + def _replace_from_to(self, + start_idx: int, + end_idx: int, + new_lines: list[DatLine] + ) -> None: self.dat_lines = (self.dat_lines[:start_idx] - + new_dat_lines + self.dat_lines[end_idx+1:]) + + new_lines + + self.dat_lines[end_idx+1:]) self.load_bookings() + def move_booking(self, id_: int, up: bool) -> None: + """Move Booking of id_ one step up or downwards""" + id_other = id_ + (-1 if up else 1) + agent = self.bookings[id_] + other = self.bookings[id_other] + start_agent, end_agent = self._margin_indices(agent) + start_other, end_other = self._margin_indices(other) + gap_lines = self.dat_lines[(end_other if up else end_agent) + 1: + start_agent if up else start_other] + self._replace_from_to(start_other if up else start_agent, + end_agent if up else end_other, + (agent.dat_lines if up else other.dat_lines) + + gap_lines + + (other.dat_lines if up else agent.dat_lines)) + + def rewrite_booking(self, id_: int, new_dat_lines: list[DatLine]) -> None: + """Rewrite .dat_lines for Booking of .id_ with new_dat_lines.""" + self._replace_from_to(*self._margin_indices(self.bookings[id_]), + new_dat_lines) + @property def dat_lines_sans_empty(self) -> list[DatLine]: """Return only those .data_lines with .code or .comment.""" diff --git a/templates/_macros.tmpl b/templates/_macros.tmpl index c88bce6..6c2e57c 100644 --- a/templates/_macros.tmpl +++ b/templates/_macros.tmpl @@ -11,7 +11,8 @@ td.invalid, tr.warning td.invalid { background-color: #ff0000; } table.ledger tr > td:first-child { background-color: white; } {% endmacro %} -{% macro table_dat_lines(dat_lines, raw) %} +{% macro table_dat_lines(dat_lines, max_id, raw) %} +
{% for dat_line in dat_lines %} {% if (not raw) and dat_line.is_intro and loop.index > 1 %} @@ -19,9 +20,9 @@ table.ledger tr > td:first-child { background-color: white; } {% endif %} {% if dat_line.is_intro %} - + {% elif dat_line.booking_line.idx == 1 %} - + {% else %} {% endif %} @@ -56,6 +57,7 @@ table.ledger tr > td:first-child { background-color: white; } {% endif %} {% endfor %}
[#][#][b][b]
+
{% endmacro %} {% macro taint_js() %} diff --git a/templates/edit_structured.tmpl b/templates/edit_structured.tmpl index f85a59d..efa474e 100644 --- a/templates/edit_structured.tmpl +++ b/templates/edit_structured.tmpl @@ -62,10 +62,10 @@ function update_form() { add_td_input('account', dat_line.booking_line.account, 30); // not using input[type=number] cuz no minimal step size, therefore regex test instead const amt_input = add_td_input('amount', dat_line.booking_line.amount == 'None' ? '' : dat_line.booking_line.amount, 12); - amt_input.pattern = '^[0-9]+(\.[0-9]+)?$'; + amt_input.pattern = '^-?[0-9]+(\.[0-9]+)?$'; amt_input.classList.add("number_input"); // ensure integer amounts at least line up with double-digit decimals - if (amt_input.value.match(/^[0-9]+$/)) { amt_input.value += '.00'; } + if (amt_input.value.match(/^-?[0-9]+$/)) { amt_input.value += '.00'; } // imply that POST handler will set '€' currency if unset, but amount set const curr_input = add_td_input('currency', dat_line.booking_line.currency, 3); curr_input.placeholder = '€'; diff --git a/templates/ledger_raw.tmpl b/templates/ledger_raw.tmpl index d11e192..d1e13f0 100644 --- a/templates/ledger_raw.tmpl +++ b/templates/ledger_raw.tmpl @@ -5,9 +5,10 @@ table { font-family: monospace; } {{ macros.css_errors() }} {{ macros.css_ledger_index_col() }} +table.ledger > tbody > tr > td:first-child input[type=submit] { font-size: 0.5em; } {% endblock %} {% block content %} -{{ macros.table_dat_lines(dat_lines, raw=true) }} +{{ macros.table_dat_lines(dat_lines, max_id, raw=true) }} {% endblock %} diff --git a/templates/ledger_structured.tmpl b/templates/ledger_structured.tmpl index 654cf80..8d9b88c 100644 --- a/templates/ledger_structured.tmpl +++ b/templates/ledger_structured.tmpl @@ -7,8 +7,9 @@ {{ macros.css_ledger_index_col() }} table.ledger > tbody > tr > td.date, table.ledger > tbody > tr > td:first-child { font-family: monospace; font-size: 1.3em; text-align: center; } table.ledger > tbody > tr > td { vertical-align: middle; } +table.ledger > tbody > tr > td:first-child { white-space: nowrap; } {% endblock %} {% block content %} -{{ macros.table_dat_lines(dat_lines, raw=false) }} +{{ macros.table_dat_lines(dat_lines, max_id, raw=false) }} {% endblock %}