- 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>
-<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):
- 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):
- 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)}'
-
- def add_structured(self, db, start=0, end=0, bonus_lines=10):
- 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=""):
- safe_val = html.escape(str(val))
- return f'<input name="{name}" value="{safe_val}" />'
- if len(bookings) == 0:
- today = str(datetime.datetime.now())[:10]
- input_lines += f'{inpu("date", today)} {inpu("description")} ; {inpu("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("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("line_{i}_account", account)} {inpu("line_{i}_amount", amount)} {inpu("line_{i}_currency", currency)} ; {inpu("line_{i}_comment", comments[i])}<br />'
- for j in range(bonus_lines):
- i = j + last_line
- input_lines += f'{inpu("line_{i}_account")} {inpu("line_{i}_amount")} {inpu("line_{i}_currency")} ; {inpu("line_{i}_comment")}<br />'
- return f'{self.header_add_form("add_structured")}{input_lines}{self.footer_add_form(start, end)}'
-
-
-db = Database()
-if __name__ == "__main__":
- webServer = HTTPServer((hostName, serverPort), MyServer)
- print(f"Server started http://{hostName}:{serverPort}")
- try:
- webServer.serve_forever()
- except KeyboardInterrupt:
- pass
- webServer.server_close()
- print("Server stopped.")