From ed21ddf65324afb6cdce3aec0b97449e6c5c2829 Mon Sep 17 00:00:00 2001
From: Christian Heller
'
- footer = ''
+ header = """
+
+
+ledger
+balance
+add free
+add structured
+
+"""
+ footer = "\n"
def do_POST(self):
length = int(self.headers['content-length'])
postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=1)
parsed_url = urlparse(self.path)
-
if '/add_structured' == parsed_url.path:
n_lines = int(len(postvars) / 4)
date = postvars['date'][0]
@@ -270,7 +280,7 @@ class MyServer(BaseHTTPRequestHandler):
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():
+ if '' == new_main.rstrip() == comment.rstrip(): # don't write empty lines
continue
lines += [f'{new_main} ; {comment}']
elif '/add_free' == parsed_url.path:
@@ -283,14 +293,13 @@ class MyServer(BaseHTTPRequestHandler):
db.append(lines)
else:
db.replace(start, end, lines)
- self.send_response(200)
+ self.send_response(301)
+ self.send_header('Location', '/')
self.end_headers()
- 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}ERROR: {e}{self.footer}'
self.wfile.write(bytes(page, "utf-8"))
def do_GET(self):
@@ -300,17 +309,14 @@ class MyServer(BaseHTTPRequestHandler):
db = Database()
parsed_url = urlparse(self.path)
page = self.header + ''
+ params = parse_qs(parsed_url.query)
+ start = int(params.get('start', ['0'])[0])
+ end = int(params.get('end', ['0'])[0])
if parsed_url.path == '/balance':
page += self.balance_as_html(db)
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])
bonus_lines = int(params.get('bonus_lines', ['0'])[0])
page += self.add_structured(db, start, end)
else:
@@ -398,52 +404,55 @@ class MyServer(BaseHTTPRequestHandler):
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'