From c3d511ddc442bc474ae36d8ccfe09ededbfe54a8 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Mon, 27 Jan 2025 08:16:51 +0100
Subject: [PATCH] On reload and save button use, always redirect to where we're
 coming from.

---
 ledger.py            | 16 +++++++---------
 templates/_base.tmpl |  4 ++--
 2 files changed, 9 insertions(+), 11 deletions(-)

diff --git a/ledger.py b/ledger.py
index 7a62924..2627237 100755
--- a/ledger.py
+++ b/ledger.py
@@ -251,15 +251,12 @@ class Handler(PlomHttpHandler):
 
     def do_POST(self) -> None:
         # pylint: disable=invalid-name,missing-function-docstring
-        redir_path = Path('/')
         if self.pagename.startswith('edit_'):
             id_ = int(self.path_toks[2])
-            redir_path = redir_path.joinpath(self.pagename).joinpath(str(id_))
-        if self.pagename == 'file':
-            if 'reload' in self.postvars.as_dict:
-                self.server.load()
-            elif 'save' in self.postvars.as_dict:
-                self.server.save()
+        if 'reload_file' in self.postvars.as_dict:
+            self.server.load()
+        elif 'save_file' in self.postvars.as_dict:
+            self.server.save()
         elif self.pagename == 'edit_structured':
             if self.postvars.first('apply'):
                 line_keys = self.postvars.keys_prefixed('line_')
@@ -289,6 +286,7 @@ class Handler(PlomHttpHandler):
                             data += f'  {input_}'
                     new_dat_lines += [
                         DatLine(f'{data} ; {comment}' if comment else data)]
+                # pylint: disable=possibly-used-before-assignment
                 self.server.rewrite_booking(id_, new_dat_lines)
         elif self.pagename == 'edit_raw':
             if self.postvars.first('apply'):
@@ -296,11 +294,11 @@ class Handler(PlomHttpHandler):
                         DatLine(line) for line
                         in self.postvars.first('booking').splitlines()]
                 self.server.rewrite_booking(id_, new_dat_lines)
-        self.redirect(redir_path)
+        self.redirect(Path(self.path))
 
     def do_GET(self) -> None:
         # pylint: disable=invalid-name,missing-function-docstring
-        ctx = {'tainted': self.server.tainted}
+        ctx = {'tainted': self.server.tainted, 'path': self.path}
         if self.pagename == 'booking' or self.pagename.startswith('edit_'):
             ctx['id'] = int(self.path_toks[2])
         if self.pagename == 'balance':
diff --git a/templates/_base.tmpl b/templates/_base.tmpl
index 0ec782d..af5070c 100644
--- a/templates/_base.tmpl
+++ b/templates/_base.tmpl
@@ -16,8 +16,8 @@ span.warning, table.warning tbody tr td, tr.warning td { background-color: #ff88
 </style>
 </head>
 <body>
-<form action="/file" method="POST">
-ledger <a href="/ledger_structured">structured</a> / <a href="/ledger_raw">raw</a> · <a href="/balance">balance</a> · <input type="submit" name="reload" value="reload" />{% if tainted %} · <span class="warning">unsaved changes: <input type="submit" name="save" value="save"></span>{% endif %}
+<form action="{{path}}" method="POST">
+ledger <a href="/ledger_structured">structured</a> / <a href="/ledger_raw">raw</a> · <a href="/balance">balance</a> · <input type="submit" name="reload_file" value="reload" />{% if tainted %} · <span class="warning">unsaved changes: <input type="submit" name="save_file" value="save"></span>{% endif %}
 </form>
 <hr />
 {% block content %}{% endblock %}
-- 
2.30.2