home · contact · privacy
In ledger.py, to structured booking adding add datalists based on previous inputs.
authorChristian Heller <c.heller@plomlompom.de>
Mon, 16 Oct 2023 23:33:16 +0000 (01:33 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Mon, 16 Oct 2023 23:33:16 +0000 (01:33 +0200)
ledger.py

index 276b1cc87a07d5dd4b30323871736886405e217c..7771f7f7e458ec4848cf0c19535034c18cda7556 100755 (executable)
--- a/ledger.py
+++ b/ledger.py
@@ -436,29 +436,46 @@ edit:
             raise HandledException('can only edit single Booking')
         input_lines = ''
         last_line = 0
-        def inpu(name, val=""):
+        def inpu(name, val="", datalist=""):
             val = val if val is not None else "" 
             safe_val = html.escape(str(val))
-            return f'<input name="{name}" value="{safe_val}" />'
+            datalist_string = '' if datalist == '' else f'list="{datalist}"'
+            return f'<input name="{name}" value="{safe_val}" {datalist_string}/>'
         if len(bookings) == 0:
             today = str(datetime.datetime.now())[:10]
-            input_lines += f'{inpu("date", today)} {inpu("description")} ; {inpu("line_0_comment")}<br />' 
+            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)} ; {inpu("line_0_comment", comments[0])}<br />' 
+            input_lines += f'{inpu("date", booking.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] != '':
                     account = booking.lines[i][0]
                     amount = booking.lines[i][1]
                     currency = booking.lines[i][2]
-                input_lines += f'{inpu(f"line_{i}_account", account)} {inpu(f"line_{i}_amount", amount)} {inpu(f"line_{i}_currency", currency)} ; {inpu(f"line_{i}_comment", comments[i])}<br />' 
+                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")} {inpu(f"line_{i}_amount")} {inpu(f"line_{i}_currency")} ; {inpu(f"line_{i}_comment")}<br />'
-        return f'{self.header_add_form("add_structured")}{input_lines}{self.footer_add_form(start, end)}'
+            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) 
+            for account, moneys in b.account_changes.items(): 
+                datalist_sets['accounts'].add(account)
+                for currency in moneys.keys():
+                    datalist_sets['currencies'].add(currency)
+        def build_datalist(name):
+            datalist = f'<datalist id="{name}">' + "\n" 
+            for item in datalist_sets[name]:
+                safe_item = html.escape(item) 
+                datalist += f'<option value="{safe_item}">{safe_item}</option>' + "\n"
+            return f"{datalist}</datalist>\n"
+        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)}'
 
 
 db = Database()