home · contact · privacy
Refactor and fix various smaller bugs. master
authorChristian Heller <c.heller@plomlompom.de>
Mon, 3 Feb 2025 08:23:23 +0000 (09:23 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Mon, 3 Feb 2025 08:23:23 +0000 (09:23 +0100)
ledger.py
templates/_macros.tmpl

index f994721f1ce346a2bacfab20f9f852a6e0da3cec..2907ba3f2906e36a5cf013ab0688e50a7c8db8b7 100755 (executable)
--- a/ledger.py
+++ b/ledger.py
@@ -17,6 +17,7 @@ PATH_TEMPLATES = Path('templates')
 
 PREFIX_LEDGER = 'ledger_'
 PREFIX_EDIT = 'edit_'
+PREFIX_FILE = 'file_'
 TOK_STRUCT = 'structured'
 TOK_RAW = 'raw'
 EDIT_STRUCT = f'{PREFIX_EDIT}{TOK_STRUCT}'
@@ -327,63 +328,80 @@ class Handler(PlomHttpHandler):
 
     def do_POST(self) -> None:
         # pylint: disable=invalid-name,missing-function-docstring
-        prefix_file = 'file_'
-        if (file_prefixed := self.postvars.keys_prefixed(prefix_file)):
-            getattr(self.server, file_prefixed[0][len(prefix_file):])()
+        redir_target = Path(self.path)
+        if (file_prefixed := self.postvars.keys_prefixed(PREFIX_FILE)):
+            self.post_file_action(file_prefixed[0])
         elif (self.pagename.startswith(PREFIX_EDIT)
               and self.postvars.first('apply')):
-            booking = self.server.bookings[int(self.path_toks[2])]
-            new_lines = []
-            if self.pagename == EDIT_STRUCT:
-                line_keys = self.postvars.keys_prefixed('line_')
-                lineno_to_inputs: dict[int, list[str]] = {}
-                for key in line_keys:
-                    toks = key.split('_', maxsplit=2)
-                    lineno = int(toks[1])
-                    if lineno not in lineno_to_inputs:
-                        lineno_to_inputs[lineno] = []
-                    lineno_to_inputs[lineno] += [toks[2]]
-                indent = '  '
-                for lineno, input_names in lineno_to_inputs.items():
-                    data = ''
-                    comment = self.postvars.first(f'line_{lineno}_comment')
-                    for name in input_names:
-                        input_ = self.postvars.first(f'line_{lineno}_{name}'
-                                                     ).strip()
-                        if name == 'date':
-                            data = input_
-                        elif name == 'target':
-                            data += f' {input_}'
-                        elif name == 'error':
-                            data = f'{indent}{input_}'
-                        elif name == 'account':
-                            data = f'{indent}{input_}'
-                        elif name in {'amount', 'currency'}:
-                            data += f'  {input_}'
-                    new_lines += [
-                        DatLine(f'{data} ; {comment}' if comment else data)]
-            else:  # edit_raw
-                new_lines += [DatLine(line) for line
-                              in self.postvars.first('booking').splitlines()]
-            new_id = self.server.rewrite_booking(booking.id_, new_lines)
-            self.redirect(Path('/bookings').joinpath(f'{new_id}'))
-            return
+            redir_target = self.post_edit()
         elif self.pagename.startswith(PREFIX_LEDGER):
-            action, id_str, dir_ =\
-                self.postvars.keys_prefixed(PREFIX_LEDGER)[0].split('_')[1:]
-            new_id = getattr(self.server, f'{action}_booking')(
-                int(id_str), dir_ == 'up' if action == 'move' else 'to_end')
-            self.redirect(Path(self.path).joinpath(f'#{new_id}'))
-            return
-        self.redirect(Path(self.path))
+            redir_target = self.post_ledger_action()
+        self.redirect(redir_target)
+
+    def post_file_action(self, file_prefixed: str) -> None:
+        """Based on file_prefixed name, trigger .server.load or .save."""
+        if file_prefixed == f'{PREFIX_FILE}load':
+            self.server.load()
+        elif file_prefixed == f'{PREFIX_FILE}save':
+            self.server.save()
+
+    def post_edit(self) -> Path:
+        """Based on postvars, edit targeted Booking."""
+        booking = self.server.bookings[int(self.path_toks[2])]
+        new_lines = []
+        if self.pagename == EDIT_STRUCT:
+            line_keys = self.postvars.keys_prefixed('line_')
+            lineno_to_inputs: dict[int, list[str]] = {}
+            for key in line_keys:
+                toks = key.split('_', maxsplit=2)
+                lineno = int(toks[1])
+                if lineno not in lineno_to_inputs:
+                    lineno_to_inputs[lineno] = []
+                lineno_to_inputs[lineno] += [toks[2]]
+            indent = '  '
+            for lineno, input_names in lineno_to_inputs.items():
+                data = ''
+                comment = self.postvars.first(f'line_{lineno}_comment')
+                for name in input_names:
+                    input_ = self.postvars.first(f'line_{lineno}_{name}'
+                                                 ).strip()
+                    if name == 'date':
+                        data = input_
+                    elif name == 'target':
+                        data += f' {input_}'
+                    elif name == 'error':
+                        data = f'{indent}{input_}'
+                    elif name == 'account':
+                        data = f'{indent}{input_}'
+                    elif name in {'amount', 'currency'}:
+                        data += f'  {input_}'
+                new_lines += [
+                    DatLine(f'{data} ; {comment}' if comment else data)]
+        else:  # edit_raw
+            new_lines += [DatLine(line) for line
+                          in self.postvars.first('booking').splitlines()]
+        new_id = self.server.rewrite_booking(booking.id_, new_lines)
+        return Path('/bookings').joinpath(f'{new_id}')
+
+    def post_ledger_action(self) -> Path:
+        """Based on trigger postvar call .server.(move|copy)_booking."""
+        keys_prefixed = self.postvars.keys_prefixed(PREFIX_LEDGER)
+        action, id_str, dir_ = keys_prefixed[0].split('_', maxsplit=3)[1:]
+        id_ = int(id_str)
+        if action == 'move':
+            id_ = self.server.move_booking(id_, dir_ == 'up')
+        elif action == 'copy':
+            id_ = self.server.copy_booking(id_, dir_ == 'to_end')
+        return Path(self.path).joinpath(f'#{id}')
 
     def do_GET(self) -> None:
         # pylint: disable=invalid-name,missing-function-docstring
-        ctx = {'tainted': self.server.tainted, 'path': self.path}
         if self.pagename == 'bookings':
             self.redirect(
                 Path('/').joinpath(EDIT_STRUCT).joinpath(self.path_toks[2]))
-        elif self.pagename == 'balance':
+            return
+        ctx = {'tainted': self.server.tainted, 'path': self.path}
+        if self.pagename == 'balance':
             self.get_balance(ctx)
         elif self.pagename.startswith(PREFIX_EDIT):
             self.get_edit(ctx, self.pagename == EDIT_RAW)
@@ -395,8 +413,7 @@ class Handler(PlomHttpHandler):
     def get_balance(self, ctx) -> None:
         """Display tree of calculated Accounts over .bookings[:up_incl+1]."""
         id_ = int(self.params.first('up_incl') or '-1')
-        valid = True
-        account_names = set()
+        valid, account_names = True, set()
         to_balance = (self.server.bookings[:id_ + 1] if id_ >= 0
                       else self.server.bookings)
         for booking in to_balance:
@@ -568,9 +585,9 @@ class Server(PlomHttpServer):
             intro_comment = copied.booked_lines[0].comment
             intro = DatLine(
                 f'{dt_date.today().isoformat()} {copied.target}'
-                + ' ; {intro_comment}' if intro_comment else '')
+                + (f' ; {intro_comment}' if intro_comment else ''))
             new_booking = Booking(new_id,
-                                  [intro] + copied.booked_lines_copied[:1],
+                                  [intro] + copied.booked_lines_copied[1:],
                                   copied.gap_lines_copied)
             self.bookings += [new_booking]
         else:
index 5cba91aa5bafa6549ea12f62fe6df9fc24c6aa98..a7ed6196b1c1cb1e5869b7ea376ce0e1f4996509 100644 (file)
@@ -24,7 +24,7 @@ table.ledger tr > td:first-child { background-color: white; }
   {% elif dat_line.booking_line.idx == 1 %}
     <td><a href="/balance?up_incl={{dat_line.booking_id}}">[b]</a><input type="submit" name="ledger_move_{{dat_line.booking_id}}_down" value="v"{% if not dat_line.booking.can_move(0) %} disabled{% endif %}/></td>
   {% elif dat_line.booking_line.idx == 2 %}
-    <td><input type="submit" name="ledger_copy_{{dat_line.booking_id}}_here" value="c" /><input type="submit" name="copy_{{dat_line.booking_id}}_to_end" value="C" /></td>
+    <td><input type="submit" name="ledger_copy_{{dat_line.booking_id}}_here" value="c" /><input type="submit" name="ledger_copy_{{dat_line.booking_id}}_to_end" value="C" /></td>
   {% else %}
     <td></td>
   {% endif %}