home · contact · privacy
To ledger.py, add entry copying.
authorChristian Heller <c.heller@plomlompom.de>
Mon, 16 Oct 2023 23:42:57 +0000 (01:42 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Mon, 16 Oct 2023 23:42:57 +0000 (01:42 +0200)
ledger.py

index 7771f7f7e458ec4848cf0c19535034c18cda7556..5610293becb20bcdff004447b7ba18b5a91ff277 100755 (executable)
--- a/ledger.py
+++ b/ledger.py
@@ -315,13 +315,17 @@ class MyServer(BaseHTTPRequestHandler):
         params = parse_qs(parsed_url.query)
         start = int(params.get('start', ['0'])[0])
         end = int(params.get('end', ['0'])[0])
+        bonus_lines = int(params.get('bonus_lines', ['0'])[0])
         if parsed_url.path == '/balance':
             page += self.balance_as_html(db)
         elif parsed_url.path == '/add_free':
             page += self.add_free(db, start, end)
         elif parsed_url.path == '/add_structured':
-            bonus_lines = int(params.get('bonus_lines', ['0'])[0])
             page += self.add_structured(db, start, end)
+        elif parsed_url.path == '/copy_free':
+            page += self.add_free(db, start, end, copy=True)
+        elif parsed_url.path == '/copy_structured':
+            page += self.add_structured(db, start, end, copy=True)
         else:
             page += self.ledger_as_html(db)
         page += self.footer
@@ -411,24 +415,29 @@ class MyServer(BaseHTTPRequestHandler):
 edit:
 <a href="/add_structured?start={booking.start_line}&end={i+1}">structured</a>
 / <a href="/add_free?start={booking.start_line}&end={i+1}">free</a>
+| copy:
+<a href="/copy_structured?start={booking.start_line}&end={i+1}">structured</a>
+/ <a href="/copy_free?start={booking.start_line}&end={i+1}">free</a>
 <br />"""
         return '\n'.join(lines)
 
     def header_add_form(self, action):
         return f"<form method=\"POST\" action=\"/{action}\">\n"
 
-    def footer_add_form(self, start, end):
+    def footer_add_form(self, start, end, copy):
+        if copy:
+            start = end = 0
         return f"""
 <input type="hidden" name="start" value={start} />
 <input type="hidden" name="end" value={end} />
 <input type="submit">
 </form>"""
 
-    def add_free(self, db, start=0, end=0):
+    def add_free(self, db, start=0, end=0, copy=False):
         content = html.escape(''.join(db.get_lines(start, end)))
-        return f'{self.header_add_form("add_free")}<textarea name="booking" rows="8" cols="80">{content}</textarea>{self.footer_add_form(start, end)}'
+        return f'{self.header_add_form("add_free")}<textarea name="booking" rows="8" cols="80">{content}</textarea>{self.footer_add_form(start, end, copy)}'
 
-    def add_structured(self, db, start=0, end=0, bonus_lines=10):
+    def add_structured(self, db, start=0, end=0, bonus_lines=10, copy=False):
         import datetime
         lines = db.get_lines(start, end) 
         bookings, comments = parse_lines(lines) 
@@ -441,14 +450,15 @@ edit:
             safe_val = html.escape(str(val))
             datalist_string = '' if datalist == '' else f'list="{datalist}"'
             return f'<input name="{name}" value="{safe_val}" {datalist_string}/>'
+        today = str(datetime.datetime.now())[:10]
         if len(bookings) == 0:
-            today = str(datetime.datetime.now())[:10]
             input_lines += f'{inpu("date", today)} {inpu("description", "", "descriptions")} ; {inpu("line_0_comment")}<br />' 
             last_line = 1 
         else:
             booking = bookings[0]
             last_line = len(comments) 
-            input_lines += f'{inpu("date", booking.date_string)} {inpu("description", booking.description, "descriptions")} ; {inpu("line_0_comment", comments[0])}<br />' 
+            date_string = today if copy else booking.date_string
+            input_lines += f'{inpu("date", date_string)} {inpu("description", booking.description, "descriptions")} ; {inpu("line_0_comment", comments[0])}<br />' 
             for i in range(1, len(comments)):
                 account = amount = currency = ''
                 if i < len(booking.lines) and booking.lines[i] != '':
@@ -458,7 +468,7 @@ edit:
                 input_lines += f'{inpu(f"line_{i}_account", account, "accounts")} {inpu(f"line_{i}_amount", amount)} {inpu(f"line_{i}_currency", currency, "currencies")} ; {inpu(f"line_{i}_comment", comments[i])}<br />' 
         for j in range(bonus_lines):
             i = j + last_line 
-            input_lines += f'{inpu(f"line_{i}_account", "", "accounts")} {inpu(f"line_{i}_amount", "", "amounts")} {inpu(f"line_{i}_currency", "", "currencies")} ; {inpu(f"line_{i}_comment")}<br />'
+            input_lines += f'{inpu(f"line_{i}_account", "", "accounts")} {inpu(f"line_{i}_amount", "", "amounts")} {inpu(f"line_{i}_currency", "", "currencies")} ; {inpu(f"line_{i}_comment")}<br />'
         datalist_sets = {'descriptions': set(), 'accounts': set(), 'currencies': set()} 
         for b in db.bookings:
             datalist_sets['descriptions'].add(b.description) 
@@ -475,7 +485,7 @@ edit:
         datalists = build_datalist('descriptions')
         datalists += build_datalist('accounts')
         datalists += build_datalist('currencies')
-        return f'{self.header_add_form("add_structured")}{input_lines}{datalists}{self.footer_add_form(start, end)}'
+        return f'{self.header_add_form("add_structured")}{input_lines}{datalists}{self.footer_add_form(start, end, copy)}'
 
 
 db = Database()