home · contact · privacy
Improve ledger.py.
authorChristian Heller <c.heller@plomlompom.de>
Mon, 23 Oct 2023 02:03:15 +0000 (04:03 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Mon, 23 Oct 2023 02:03:15 +0000 (04:03 +0200)
ledger.py

index a1b31eb9f9c4d56739340aa8658721b9eb61368d..60bbf640a4d117ca548a407d727375cc60dbad1d 100755 (executable)
--- a/ledger.py
+++ b/ledger.py
@@ -357,6 +357,7 @@ class MyServer(BaseHTTPRequestHandler):
 body { color: #000000; }
 table { margin-bottom: 2em; }
 th, td { text-align: left }
+input[type=number] { text-align: right; font-family: monospace; }
 .money { font-family: monospace; text-align: right; }
 .comment { font-style: italic; color: #777777; }
 .full_line_comment { display: block; white-space: nowrap; width: 0; }
@@ -468,33 +469,6 @@ th, td { text-align: left }
         content = "\n".join(lines)
         return f"<pre>{content}</pre>"
 
-    # def ledger_as_html(self, db):
-    #     lines = []
-    #     line_sep = '<br />'
-    #     for comment in db.comments:
-    #         line = f' ; {comment}' if comment != '' else ''
-    #         lines += [line + line_sep]
-    #     for booking in db.bookings:
-    #         i = booking.start_line
-    #         suffix = lines[i]
-    #         lines[i] = f'<p>{booking.date_string} {booking.description}{suffix}'
-    #         for booking_line in booking.lines[1:]:
-    #             i += 1
-    #             if booking_line == '':
-    #                 continue
-    #             suffix = f' {lines[i]}' if len(lines[i]) > 0 else ''
-    #             value = f' {booking_line[1]} {booking_line[2]}' if booking_line[1] else ''
-    #             lines[i] = f'{booking_line[0]}{value}{suffix}'
-    #         lines[i] = lines[i][:-len(line_sep)] + f"""</p>
-# edit :
-# <a h ref="/add_structured?start={booking.start_line}&end={i+1}">structured</a>
-# / <a  href="/add_free?start={booking.start_line}&end={i+1}">free</a>
-# | co py:
-# <a h ref="/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 ledger_as_html(self, db):
         lines = []
         for comment in db.comments:
@@ -534,9 +508,17 @@ th, td { text-align: left }
 <input type="submit" name="default">
 </form>"""
 
+    def textarea(self, name, lines, min_rows=1, min_cols=80):
+        safe_content = html.escape(''.join(lines))
+        n_rows = max(min_rows, len(lines))
+        n_cols = min_cols
+        for line in lines:
+            n_cols = len(line) if len(line) > n_cols else n_cols 
+        return f'<textarea name="{name}" rows={n_rows} cols={n_cols}>{safe_content}</textarea>'
+
     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, copy)}'
+        lines = db.get_lines(start, end)
+        return f'{self.header_add_form("add_free")}{self.textarea("booking",lines,10)}{self.footer_add_form(start, end, copy)}'
 
     def add_structured(self, db, start=0, end=0, bonus_lines=10, copy=False):
         import datetime
@@ -545,31 +527,33 @@ th, td { text-align: left }
         if len(bookings) > 1:
             raise HandledException('can only edit single Booking')
         last_line = 0
-        def inpu(name, val="", datalist="", input_type='text'):
+        def inpu(name, val="", datalist="", input_type='text', size=-1):
             val = val if val is not None else ""
             safe_val = html.escape(str(val))
             datalist_string = '' if datalist == '' else f'list="{datalist}"'
-            return f'<input type="{input_type}" name="{name}" value="{safe_val}" {datalist_string}/>'
+            number_step = '' if input_type != 'number' else ' step=0.01'
+            size_string = '' if size < 0 else f' size={size}' 
+            return f'<input type="{input_type}"{number_step} name="{name}"{size_string} value="{safe_val}" {datalist_string}/>'
         input_lines = inpu('add_income_tax', 'add income tax', '', 'submit') + '<br />'
         today = str(datetime.datetime.now())[:10]
         if len(bookings) == 0:
-            input_lines += f'{inpu("date", today)} {inpu("description", "", "descriptions")} ; {inpu("line_0_comment")}<br />'
+            input_lines += f'{inpu("date", today, size=9)} {inpu("description", "", "descriptions")} ; {inpu("line_0_comment")}<br />'
             last_line = 1
         else:
             booking = bookings[0]
             last_line = len(comments)
             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 />'
+            input_lines += f'{inpu("date", date_string, size=9)} {inpu("description", booking.description, "descriptions")} ; {self.textarea("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, "accounts")} {inpu(f"line_{i}_amount", amount)} {inpu(f"line_{i}_currency", currency, "currencies")} ; {inpu(f"line_{i}_comment", comments[i])}<br />'
+                input_lines += f'{inpu(f"line_{i}_account", account, "accounts", size=40)} {inpu(f"line_{i}_amount", amount, "", "number", size=10)} {inpu(f"line_{i}_currency", currency, "currencies", size=3)} ; {self.textarea(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", size=40)} {inpu(f"line_{i}_amount", "", "", "number", size=10)} {inpu(f"line_{i}_currency", "", "currencies", size=3)} ; {self.textarea(f"line_{i}_comment", [""])}<br />'
         datalist_sets = {'descriptions': set(), 'accounts': set(), 'currencies': set()}
         for b in db.bookings:
             datalist_sets['descriptions'].add(b.description)