From: Christian Heller Date: Wed, 22 Jan 2025 23:09:47 +0000 (+0100) Subject: Add basic editing of Bookings, and file saving. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/reset_cookie?a=commitdiff_plain;h=refs%2Fheads%2Fmaster;p=plomledger Add basic editing of Bookings, and file saving. --- diff --git a/ledger.py b/ledger.py index 22e051a..f4dde03 100755 --- a/ledger.py +++ b/ledger.py @@ -221,31 +221,53 @@ class Handler(PlomHttpHandler): def do_POST(self) -> None: # pylint: disable=invalid-name,missing-function-docstring - if self.pagename == 'reload': - self.server.load() - self.redirect(Path('')) + if self.pagename == 'file': + if 'reload' in self.postvars.as_dict: + self.server.load() + elif 'save' in self.postvars.as_dict: + self.server.save() + elif self.pagename == 'edit': + id_ = int(self.path_toks[2]) + old_booking = self.server.bookings[id_] + start_idx = self.server.dat_lines.index(old_booking.dat_lines[0]) + end_idx = self.server.dat_lines.index(old_booking.dat_lines[-1]) + new_dat_lines = [DatLine(line) for line + in self.postvars.first('booking').splitlines()] + self.server.dat_lines = (self.server.dat_lines[:start_idx] + + new_dat_lines + + self.server.dat_lines[end_idx+1:]) + self.server.load_bookings() + self.server.tainted = True + self.redirect(Path('/').joinpath('booking').joinpath(str(id_))) + return + self.redirect(Path('/')) def do_GET(self) -> None: # pylint: disable=invalid-name,missing-function-docstring + ctx = {'tainted': self.server.tainted} + if self.pagename in {'booking', 'edit'}: + ctx['id'] = int(self.path_toks[2]) + ctx['dat_lines'] = self.server.bookings[ctx['id']].dat_lines if self.pagename == 'balance': valid, balance_roots = self.server.balance_roots - self.send_rendered(Path('balance.tmpl'), {'roots': balance_roots, - 'valid': valid}) + self.send_rendered(Path('balance.tmpl'), + ctx | {'roots': balance_roots, 'valid': valid}) elif self.pagename == 'booking': - self.send_rendered( - Path('booking.tmpl'), - {'dat_lines': - self.server.bookings[int(self.path_toks[2])].dat_lines}) + self.send_rendered(Path('booking.tmpl'), ctx) + elif self.pagename == 'edit': + self.send_rendered(Path('edit.tmpl'), ctx) elif self.pagename == 'raw': self.send_rendered(Path('raw.tmpl'), - {'dat_lines': self.server.dat_lines}) + ctx | {'dat_lines': self.server.dat_lines}) else: - self.send_rendered(Path('index.tmpl'), - {'dat_lines': self.server.dat_lines_sans_empty}) + self.send_rendered( + Path('index.tmpl'), + ctx | {'dat_lines': self.server.dat_lines_sans_empty}) class Server(PlomHttpServer): """Extends parent by loading .dat file into database for Handler.""" + bookings: list[Booking] def __init__(self, path_dat: Path, *args, **kwargs) -> None: super().__init__(PATH_TEMPLATES, (SERVER_HOST, SERVER_PORT), Handler) @@ -257,7 +279,12 @@ class Server(PlomHttpServer): self.dat_lines = [ DatLine(line) for line in self._path_dat.read_text(encoding='utf8').splitlines()] - self.bookings: list[Booking] = [] + self.load_bookings() + self.tainted = False + + def load_bookings(self) -> None: + """Read .dat_lines into Bookings / full ledger.""" + self.bookings = [] booking_lines: list[DatLine] = [] last_date = '' for dat_line in self.dat_lines + [DatLine('')]: @@ -272,6 +299,12 @@ class Server(PlomHttpServer): self.bookings += [booking] booking_lines = [] + def save(self) -> None: + """Save current state to ._path_dat.""" + self._path_dat.write_text( + '\n'.join([line.raw for line in self.dat_lines]), encoding='utf8') + self.load() + @property def dat_lines_sans_empty(self) -> list[DatLine]: """Return only those .data_lines with .code or .comment.""" diff --git a/templates/_base.tmpl b/templates/_base.tmpl index 5d80c44..a613a1b 100644 --- a/templates/_base.tmpl +++ b/templates/_base.tmpl @@ -7,13 +7,13 @@ body { background-color: white; font-family: sans-serif; } tr:nth-child(odd) { background-color: #dcdcdc; } td { text-align: left; vertical-align: top; } -table.warning tbody tr td, tr.warning td { background-color: #ff8888; } +span.warning, table.warning tbody tr td, tr.warning td { background-color: #ff8888; } {% block css %}{% endblock %} -
-home · raw · balance · + +home · raw · balance · {% if tainted %} · unsaved changes: {% endif %}

{% block content %}{% endblock %} diff --git a/templates/booking.tmpl b/templates/booking.tmpl index 2e01a75..9470c27 100644 --- a/templates/booking.tmpl +++ b/templates/booking.tmpl @@ -7,6 +7,7 @@ {% endblock %} {% block content %} +edit +
{{ macros.table_dat_lines(dat_lines, single=true, raw=false) }} {% endblock %} -