X-Git-Url: https://plomlompom.com/repos/condition_titles?a=blobdiff_plain;ds=inline;f=ledger.py;h=9ff0d3eaabc723170071ffbb0e3a4726a6a747d0;hb=92c86eae1c6c758defda091b628a259d84f39b18;hp=b6fe0644074f07566e2a0aabe2d6fab30bdfbf19;hpb=5a6e56223e59fb628f88070957b39a42d3b7c945;p=misc
diff --git a/ledger.py b/ledger.py
index b6fe064..9ff0d3e 100755
--- a/ledger.py
+++ b/ledger.py
@@ -65,6 +65,10 @@ add_structured_html = """
+
+
+
+
@@ -350,24 +354,28 @@ class LedgerDB(PlomDB):
return self.real_lines[start:end]
def write_db(self, text, mode='w'):
+ if text[-1] != '\n':
+ text += '\n'
self.write_text_to_db(text)
def insert_at_date(self, lines, date):
- start_at = len(self.real_lines)
- print("DEBUG triggered insert_at_date", date, start_at)
- for b in self.bookings:
- if b.date_string == date:
- start_at = b.start_line
- print("DEBUG setting start_at to", start_at)
- break
- elif b.date_string > date:
- break
- if start_at == len(self.real_lines):
- lines = [''] + lines
+ print("DEBUG insert_at_date")
+ start_at = 0
+ if len(self.bookings) > 0:
+ if date >= self.bookings[-1].date_string:
+ start_at = len(self.real_lines)
+ lines = [''] + lines
+ else:
+ for b in self.bookings:
+ if b.date_string == date:
+ start_at = b.start_line
+ elif b.date_string > date:
+ start_at = b.start_line
+ break
return self.write_lines_in_total_lines_at(self.real_lines, start_at, lines)
def update(self, start, end, lines, date):
- print("DEBUG update", date)
+ print("DEBUG update")
total_lines = self.real_lines[:start] + self.real_lines[end:]
n_original_lines = end - start
start_at = len(total_lines)
@@ -516,6 +524,14 @@ class LedgerDB(PlomDB):
ret += [f' {acc_buffer} {-final_minus} ⬠; assume as to earn in year: {acc_buffer} + {12 - months_passed - 1} * this = {year_needed}']
return ret
+ def add_mirror(self, lines):
+ ret = []
+ bookings, _ = parse_lines(lines)
+ booking = bookings[0]
+ for line in booking.lines[1:]:
+ ret += [f'? {-line[1]} {line[2]}']
+ return ret
+
def ledger_as_html(self):
booking_tmpl = jinja2.Template(booking_html)
single_c_tmpl = jinja2.Template('
') ##
@@ -610,15 +626,12 @@ class LedgerDB(PlomDB):
start = end = 0
desc = head_comment = ''
if len(bookings) == 0:
- for i in range(1, 8):
- booking_lines += [{'i': i, 'acc': '', 'amt': '', 'curr': 'â¬', 'comment': ''}]
date=today
else:
booking = bookings[0]
desc = booking.description
date = today if copy else booking.date_string
head_comment=comments[0]
- last_line = len(comments)
for i in range(1, len(comments)):
account = amount = currency = ''
if i < len(booking.lines) and booking.lines[i] != '':
@@ -632,6 +645,8 @@ class LedgerDB(PlomDB):
'curr': currency if currency else 'â¬',
'comment': comments[i],
'comm_cols': len(comments[i])}]
+ for i in range(len(comments), len(comments) + 8):
+ booking_lines += [{'i': i, 'acc': '', 'amt': '', 'curr': 'â¬', 'comment': ''}]
content += tmpl.render(
action=action,
date=date,
@@ -660,12 +675,12 @@ class LedgerDB(PlomDB):
next_booking = b
break
start_at = next_booking.start_line + len(next_booking.lines) - (end - start) + 1
- self.make_move(start, end, start_at)
+ self.make_move(start, end, start_at-1)
return redir_nth
def make_move(self, start, end, start_at):
lines = self.get_lines(start, end)
- total_lines = self.real_lines[:start] + self.real_lines[end:]
+ total_lines = self.real_lines[:start-1] + self.real_lines[end:] # +1 because we reduce the original position's two empty border lines to in-between line
self.write_lines_in_total_lines_at(total_lines, start_at, lines)
def booking_lines_from_postvars(self, postvars):
@@ -712,10 +727,15 @@ class LedgerDB(PlomDB):
lines += [f'Assets {amount:.2f} {currency}']
except PlomException:
pass
- if 'add_taxes' in postvars.keys():
+ elif 'add_taxes' in postvars.keys():
lines += self.add_taxes(lines, finish=False)
elif 'add_taxes2' in postvars.keys():
lines += self.add_taxes(lines, finish=True)
+ elif 'replace' in postvars.keys():
+ for i, line in enumerate(lines):
+ lines[i] = line.replace(postvars['replace_from'][0], postvars['replace_to'][0])
+ elif 'add_mirror' in postvars.keys():
+ lines += self.add_mirror(lines)
return lines, add_empty_line
@@ -729,97 +749,90 @@ class LedgerHandler(PlomHandler):
return 'ledger', default_path
def do_POST(self):
- self.forward_posts()
+ self.try_do(self.forward_posts)
def forward_posts(self):
- try:
- prefix = self.apps['ledger'] if hasattr(self, 'apps') else ''
- parsed_url = urlparse(self.path)
- length = int(self.headers['content-length'])
- postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=1)
- print("DEBUG", postvars['start'], postvars['end'])
- start = int(postvars['start'][0])
- end = int(postvars['end'][0])
- db = LedgerDB(prefix)
- add_empty_line = None
- lines = []
- # get inputs
- if prefix + '/add_structured' == parsed_url.path and not 'revert' in postvars.keys():
- lines, add_empty_line = db.booking_lines_from_postvars(postvars)
- elif prefix + '/add_free' == parsed_url.path and not 'revert' in postvars.keys():
- lines = postvars['booking'][0].splitlines()
- # validate where appropriate
- if ('save' in postvars.keys()) or ('check' in postvars.keys()):
- _, _ = parse_lines(lines)
- # if saving, process where to and where to redirect after
- if 'save' in postvars.keys():
- last_date = str(datetime.now())[:10]
- if len(db.bookings) > 0:
- last_date = db.bookings[-1].date_string
- target_date = last_date[:]
- first_line_tokens = lines[0].split() if len(lines) > 0 else ''
- first_token = first_line_tokens[0] if len(first_line_tokens) > 0 else ''
- try:
- datetime.strptime(first_token, '%Y-%m-%d')
- target_date = first_token
- except ValueError:
- pass
- if start == end == 0:
- start = db.insert_at_date(lines, target_date)
- nth = db.get_nth_for_booking_of_start_line(start)
- else:
- new_start = db.update(start, end, lines, target_date)
- nth = db.get_nth_for_booking_of_start_line(new_start)
- if new_start > start:
- nth -= 1
- self.redirect(prefix + f'/#{nth}')
- # otherwise just re-build editing form
+ prefix = self.apps['ledger'] if hasattr(self, 'apps') else ''
+ parsed_url = urlparse(self.path)
+ length = int(self.headers['content-length'])
+ postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=1)
+ start = int(postvars['start'][0])
+ end = int(postvars['end'][0])
+ db = LedgerDB(prefix)
+ add_empty_line = None
+ lines = []
+ # get inputs
+ if prefix + '/add_structured' == parsed_url.path and not 'revert' in postvars.keys():
+ lines, add_empty_line = db.booking_lines_from_postvars(postvars)
+ elif prefix + '/add_free' == parsed_url.path and not 'revert' in postvars.keys():
+ lines = postvars['booking'][0].splitlines()
+ # validate where appropriate
+ if ('save' in postvars.keys()) or ('check' in postvars.keys()):
+ _, _ = parse_lines(lines)
+ # if saving, process where to and where to redirect after
+ if 'save' in postvars.keys():
+ last_date = str(datetime.now())[:10]
+ if len(db.bookings) > 0:
+ last_date = db.bookings[-1].date_string
+ target_date = last_date[:]
+ first_line_tokens = lines[0].split() if len(lines) > 0 else ''
+ first_token = first_line_tokens[0] if len(first_line_tokens) > 0 else ''
+ try:
+ datetime.strptime(first_token, '%Y-%m-%d')
+ target_date = first_token
+ except ValueError:
+ pass
+ if start == end == 0:
+ start = db.insert_at_date(lines, target_date)
+ nth = db.get_nth_for_booking_of_start_line(start)
else:
- if prefix + '/add_structured' == parsed_url.path:
- edit_content = db.add_structured(start, end, temp_lines=lines, add_empty_line=add_empty_line)
- else:
- edit_content = db.add_free(start, end)
- header = jinja2.Template(html_head).render(prefix=prefix)
- self.send_HTML(header + edit_content)
- except PlomException as e:
- self.fail_400(e)
+ new_start = db.update(start, end, lines, target_date)
+ nth = db.get_nth_for_booking_of_start_line(new_start)
+ if new_start > start:
+ nth -= 1
+ self.redirect(prefix + f'/#{nth}')
+ # otherwise just re-build editing form
+ else:
+ if prefix + '/add_structured' == parsed_url.path:
+ edit_content = db.add_structured(start, end, temp_lines=lines, add_empty_line=add_empty_line)
+ else:
+ edit_content = db.add_free(start, end)
+ header = jinja2.Template(html_head).render(prefix=prefix)
+ self.send_HTML(header + edit_content)
def do_GET(self):
- self.forward_gets()
+ self.try_do(self.forward_gets)
def forward_gets(self):
- try:
- prefix = self.apps['ledger'] if hasattr(self, 'apps') else ''
- parsed_url = urlparse(self.path)
- params = parse_qs(parsed_url.query)
- start = int(params.get('start', ['0'])[0])
- end = int(params.get('end', ['0'])[0])
- db = LedgerDB(prefix=prefix)
- if parsed_url.path == prefix + '/balance':
- stop = params.get('stop', [None])[0]
- page = db.balance_as_html(stop)
- elif parsed_url.path == prefix + '/add_free':
- page = db.add_free(start, end)
- elif parsed_url.path == prefix + '/add_structured':
- page = db.add_structured(start, end)
- elif parsed_url.path == prefix + '/copy_free':
- page = db.add_free(start, end, copy=True)
- elif parsed_url.path == prefix + '/copy_structured':
- page = db.add_structured(start, end, copy=True)
- elif parsed_url.path == prefix + '/move_up':
- nth = db.move_up(start, end)
- self.redirect(prefix + f'/#{nth}')
- return
- elif parsed_url.path == prefix + '/move_down':
- nth = db.move_down(start, end)
- self.redirect(prefix + f'/#{nth}')
- return
- else:
- page = db.ledger_as_html()
- header = jinja2.Template(html_head).render(prefix=prefix)
- self.send_HTML(header + page)
- except PlomException as e:
- self.fail_400(e)
+ prefix = self.apps['ledger'] if hasattr(self, 'apps') else ''
+ parsed_url = urlparse(self.path)
+ params = parse_qs(parsed_url.query)
+ start = int(params.get('start', ['0'])[0])
+ end = int(params.get('end', ['0'])[0])
+ db = LedgerDB(prefix=prefix)
+ if parsed_url.path == prefix + '/balance':
+ stop = params.get('stop', [None])[0]
+ page = db.balance_as_html(stop)
+ elif parsed_url.path == prefix + '/add_free':
+ page = db.add_free(start, end)
+ elif parsed_url.path == prefix + '/add_structured':
+ page = db.add_structured(start, end)
+ elif parsed_url.path == prefix + '/copy_free':
+ page = db.add_free(start, end, copy=True)
+ elif parsed_url.path == prefix + '/copy_structured':
+ page = db.add_structured(start, end, copy=True)
+ elif parsed_url.path == prefix + '/move_up':
+ nth = db.move_up(start, end)
+ self.redirect(prefix + f'/#{nth}')
+ return
+ elif parsed_url.path == prefix + '/move_down':
+ nth = db.move_down(start, end)
+ self.redirect(prefix + f'/#{nth}')
+ return
+ else:
+ page = db.ledger_as_html()
+ header = jinja2.Template(html_head).render(prefix=prefix)
+ self.send_HTML(header + page)