- 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':
- 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
- self.wfile.write(bytes(page, "utf-8"))
-
- def balance_as_html(self, db):
- account_sums = {}
- for booking in db.bookings:
- for account, changes in booking.account_changes.items():
- for currency, amount in changes.items():
- apply_booking_to_account_balances(account_sums, account, currency, amount)
- account_tree = {}
- def collect_branches(account_name, path):
- node = account_tree
- path_copy = path[:]
- while len(path_copy) > 0:
- step = path_copy.pop(0)
- node = node[step]
- toks = account_name.split(":", maxsplit=1)
- parent = toks[0]
- if parent in node.keys():
- child = node[parent]
- else:
- child = {}
- node[parent] = child
- if len(toks) == 2:
- k, v = collect_branches(toks[1], path + [parent])
- if k not in child.keys():
- child[k] = v
- else:
- child[k].update(v)
- return parent, child
- for account_name in sorted(account_sums.keys()):
- k, v = collect_branches(account_name, [])
- if k not in account_tree.keys():
- account_tree[k] = v
- else:
- account_tree[k].update(v)
- def collect_totals(parent_path, tree_node):
- for k, v in tree_node.items():
- child_path = parent_path + ":" + k
- for currency, amount in collect_totals(child_path, v).items():
- apply_booking_to_account_balances(account_sums, parent_path, currency, amount)
- return account_sums[parent_path]
- for account_name in account_tree.keys():
- account_sums[account_name] = collect_totals(account_name, account_tree[account_name])
- lines = []
- def print_subtree(lines, indent, node, subtree, path):
- line = f"{indent}{node}"
- n_tabs = 5 - (len(line) // 8)
- line += n_tabs * "\t"
- if "€" in account_sums[path + node].keys():
- amount = account_sums[path + node]["€"]
- line += f"{amount:9.2f} €\t"
- else:
- line += f"\t\t"
- for currency, amount in account_sums[path + node].items():
- if currency != '€' and amount > 0:
- line += f"{amount:5.2f} {currency}\t"
- lines += [line]
- indent += " "
- for k, v in sorted(subtree.items()):
- print_subtree(lines, indent, k, v, path + node + ":")
- for k, v in sorted(account_tree.items()):
- print_subtree(lines, "", k, v, "")
- 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 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, 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, 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)}'
-
- 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)
- if len(bookings) > 1:
- raise HandledException('can only edit single Booking')
- input_lines = ''
- last_line = 0
- def inpu(name, val="", datalist=""):
- 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 name="{name}" value="{safe_val}" {datalist_string}/>'
- today = str(datetime.datetime.now())[:10]
- if len(bookings) == 0:
- input_lines += f'{inpu("date", today)} {inpu("description", "", "descriptions")} ; {inpu("line_0_comment")}<br />'
- last_line = 1