home · contact · privacy
Differentiate copying between "to end of ledger" and "to after copied". master
authorChristian Heller <c.heller@plomlompom.de>
Wed, 29 Jan 2025 03:16:02 +0000 (04:16 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 29 Jan 2025 03:16:02 +0000 (04:16 +0100)
ledger.py
templates/_macros.tmpl

index f61cbec5a046d24e66ee05a29b11c4f4cbdb8b67..60106c740ca833e75c102bf48d6803ff72538f14 100755 (executable)
--- a/ledger.py
+++ b/ledger.py
@@ -261,9 +261,11 @@ class Handler(PlomHttpHandler):
                 self.redirect(Path(self.pagename).joinpath(f'#{id_}'))
                 return
             for key in self.postvars.keys_prefixed('copy_'):
-                id_ = int(key.split('_')[1])
-                self.server.copy_booking(id_)
-                self.redirect(Path('/bookings').joinpath(f'{id_}'))
+                toks = key.split('_', maxsplit=2)
+                id_ = int(toks[1])
+                new_id = self.server.copy_booking(int(toks[1]),
+                                                  to_end=toks[2] == 'to_end')
+                self.redirect(Path('/bookings').joinpath(f'{new_id}'))
                 return
         elif self.pagename == 'edit_structured':
             if self.postvars.first('apply'):
@@ -422,11 +424,20 @@ class Server(PlomHttpServer):
         self._replace_from_to(*self._margin_indices(self.bookings[id_]),
                               new_dat_lines)
 
-    def copy_booking(self, id_: int) -> None:
-        """Append copy of Booking of id_ to end of ledger."""
-        self.dat_lines += [DatLine('')] + [DatLine(dat_line.raw) for dat_line
-                                           in self.bookings[id_].dat_lines]
+    def copy_booking(self, id_: int, to_end: bool) -> int:
+        """Add copy of Booking of id_ to_end of ledger, or after copied."""
+        copied = self.bookings[id_]
+        new_lines = [DatLine('')] + [DatLine(dat_line.raw) for dat_line
+                                     in copied.dat_lines]
+        if to_end or copied is self.bookings[-1]:
+            self.dat_lines += new_lines
+            new_id = self.bookings[-1].id_ + 1
+        else:
+            start = self.dat_lines.index(copied.dat_lines[-1])
+            self._replace_from_to(start + 1, start, new_lines)
+            new_id = copied.id_ + 1
         self._load_bookings()
+        return new_id
 
     @property
     def dat_lines_sans_empty(self) -> list[DatLine]:
index dc7b13d17586c0b4634a53ed9cd5facbbc71d441..f8f932545ec6c553d330574196a19662826d9291 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="move_{{dat_line.booking_id}}_down" value="v"{% if dat_line.booking_id == max_id %} disabled{% endif %}/></td>
   {% elif dat_line.booking_line.idx == 2 %}
-    <td><input type="submit" name="copy_{{dat_line.booking_id}}" value="c" /></td>
+    <td><input type="submit" name="copy_{{dat_line.booking_id}}_here" value="c" /><input type="submit" name="copy_{{dat_line.booking_id}}_to_end" value="C" /></td>
   {% else %}
     <td></td>
   {% endif %}