From: Christian Heller Date: Mon, 23 Oct 2023 02:03:15 +0000 (+0200) Subject: Improve ledger.py. X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/decks/static/task?a=commitdiff_plain;h=6061ff7f1948b83dc601f52df1119a57b6e91649;p=misc Improve ledger.py. --- diff --git a/ledger.py b/ledger.py index a1b31eb..60bbf64 100755 --- 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"
{content}
" - # def ledger_as_html(self, db): - # lines = [] - # line_sep = '
' - # 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'

{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"""

-# edit : -# structured -# / free -# | co py: -# structured -# / free -#
""" -# 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 } """ + 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'' + 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")}{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'' + number_step = '' if input_type != 'number' else ' step=0.01' + size_string = '' if size < 0 else f' size={size}' + return f'' input_lines = inpu('add_income_tax', 'add income tax', '', 'submit') + '
' today = str(datetime.datetime.now())[:10] if len(bookings) == 0: - input_lines += f'{inpu("date", today)} {inpu("description", "", "descriptions")} ; {inpu("line_0_comment")}
' + input_lines += f'{inpu("date", today, size=9)} {inpu("description", "", "descriptions")} ; {inpu("line_0_comment")}
' 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])}
' + input_lines += f'{inpu("date", date_string, size=9)} {inpu("description", booking.description, "descriptions")} ; {self.textarea("line_0_comment", [comments[0]])}
' 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])}
' + 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]])}
' 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")}
' + 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", [""])}
' datalist_sets = {'descriptions': set(), 'accounts': set(), 'currencies': set()} for b in db.bookings: datalist_sets['descriptions'].add(b.description)