From c018936a82d6caa32affe9c01af8f1d68e568a70 Mon Sep 17 00:00:00 2001
From: Christian Heller {booking.date_string} {booking.description}{suffix}'
- for booking_line in booking.lines:
+ 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'
'
+ header = 'ledger balance add free add structured
'
footer = ''
def do_POST(self):
length = int(self.headers['content-length'])
postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=1)
- lines = postvars['booking'][0].splitlines()
+ parsed_url = urlparse(self.path)
+
+ if '/add_structured' == parsed_url.path:
+ n_lines = int(len(postvars) / 4)
+ date = postvars['date'][0]
+ description = postvars['description'][0]
+ start_comment = postvars['line_0_comment'][0]
+ lines = [f'{date} {description} ; {start_comment}']
+ for i in range(1, n_lines):
+ account = postvars[f'line_{i}_account'][0]
+ amount = postvars[f'line_{i}_amount'][0]
+ currency = postvars[f'line_{i}_currency'][0]
+ comment = postvars[f'line_{i}_comment'][0]
+ new_main = f'{account} {amount} {currency}'
+ if '' == new_main.rstrip() == comment.rstrip():
+ continue
+ lines += [f'{new_main} ; {comment}']
+ elif '/add_free' == parsed_url.path:
+ lines = postvars['booking'][0].splitlines()
start = int(postvars['start'][0])
end = int(postvars['end'][0])
try:
@@ -262,33 +282,37 @@ class MyServer(BaseHTTPRequestHandler):
if start == end == 0:
db.append(lines)
else:
- # raise HandledException(f'would write from {start} to {end}')
db.replace(start, end, lines)
self.send_response(200)
self.end_headers()
- page = f"{self.header}Success!{self.footer}"
+ page = f'{self.header}Success!{self.footer}'
self.wfile.write(bytes(page, "utf-8"))
except HandledException as e:
self.send_response(400)
self.end_headers()
- page = f"{self.header}{e}{self.footer}"
+ page = f'{self.header}{e}{self.footer}'
self.wfile.write(bytes(page, "utf-8"))
def do_GET(self):
- from urllib.parse import urlparse
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
db = Database()
- page = self.header + ''
parsed_url = urlparse(self.path)
+ page = self.header + ''
if parsed_url.path == '/balance':
page += self.balance_as_html(db)
- elif parsed_url.path == '/add':
+ elif parsed_url.path == '/add_free':
+ params = parse_qs(parsed_url.query)
+ start = int(params.get('start', ['0'])[0])
+ end = int(params.get('end', ['0'])[0])
+ page += self.add_free(db, start, end)
+ elif parsed_url.path == '/add_structured':
params = parse_qs(parsed_url.query)
start = int(params.get('start', ['0'])[0])
end = int(params.get('end', ['0'])[0])
- page += self.add(db, start, end)
+ bonus_lines = int(params.get('bonus_lines', ['0'])[0])
+ page += self.add_structured(db, start, end)
else:
page += self.ledger_as_html(db)
page += self.footer
@@ -365,24 +389,61 @@ class MyServer(BaseHTTPRequestHandler):
lines += [line + line_sep]
for booking in db.bookings:
i = booking.start_line
- suffix = lines[i] # f" {lines[i]}" if len(lines[i]) > 0 else ""
+ suffix = lines[i]
lines[i] = f'
'
+ lines[i] = lines[i][:-len(line_sep)] + f'