From ebc562af576469da5f0011e10f80c7b0490ed959 Mon Sep 17 00:00:00 2001 From: Christian Heller <c.heller@plomlompom.de> Date: Sun, 29 Oct 2023 06:04:55 +0100 Subject: [PATCH] Improve ledger.py --- ledger.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/ledger.py b/ledger.py index 0785b1a..ee6ed80 100755 --- a/ledger.py +++ b/ledger.py @@ -422,10 +422,11 @@ input[type=number] { text-align: right; font-family: monospace; } if 'save' in postvars.keys(): if start == end == 0: db.append(lines) + redir_url = f'/#last' else: db.replace(start, end, lines) + redir_url = f'/#{start}' self.send_response(301) - redir_url = '/' self.send_header('Location', redir_url) self.end_headers() else: @@ -495,7 +496,7 @@ input[type=number] { text-align: right; font-family: monospace; } def ledger2_as_html(self, db): single_c_tmpl = jinja2.Template('<span class="comment">{{c|e}}</span><br />') booking_tmpl = jinja2.Template(""" -<p>{{date}} {{desc}} <span class="comment">{{head_comment|e}}</span> +<p id="{{start}}"><a href="#{{start}}">{{date}}</a> {{desc}} <span class="comment">{{head_comment|e}}</span> [edit: <a href="/add_structured?start={{start}}&end={{end}}">structured</a> / <a href="/add_free?start={{start}}&end={{end}}">free</a> | copy:<a href="/copy_structured?start={{start}}&end={{end}}">structured</a> @@ -558,7 +559,7 @@ input[type=number] { text-align: right; font-family: monospace; } def ledger_as_html(self, db): single_c_tmpl = jinja2.Template('<span class="comment">{{c|e}}</span><br />') booking_tmpl = jinja2.Template(""" -<p>{{date}} {{desc}} <span class="comment">{{head_comment|e}}</span> +<p id="{{start}}"><a {% if last %}id="last"{% endif %} href="#{{start}}">{{date}}</a> {{desc}} <span class="comment">{{head_comment|e}}</span> [edit: <a href="/add_structured?start={{start}}&end={{end}}">structured</a> / <a href="/add_free?start={{start}}&end={{end}}">free</a> | copy:<a href="/copy_structured?start={{start}}&end={{end}}">structured</a> @@ -575,6 +576,7 @@ input[type=number] { text-align: right; font-family: monospace; } """) elements_to_write = [] last_i = i = 0 + last_start = db.bookings[-1].start_line for booking in db.bookings: i = booking.start_line elements_to_write += [single_c_tmpl.render(c=c) for c in db.comments[last_i:i] if c != ''] @@ -592,6 +594,7 @@ input[type=number] { text-align: right; font-family: monospace; } money = f'{booking_line[1]} {booking_line[2]}' booking_lines += [{'acc': booking_line[0], 'money':money, 'comment':comment}] elements_to_write += [booking_tmpl.render( + last=booking.start_line == last_start, start=booking.start_line, end=booking_end, date=booking.date_string, @@ -603,7 +606,7 @@ input[type=number] { text-align: right; font-family: monospace; } def add_free(self, db, start=0, end=0, copy=False): tmpl = jinja2.Template(""" -<form method="POST" action="{{action}}"> +<form method="POST" action="{{action|e}}"> <textarea name="booking" rows=10 cols=80> {% for line in lines %}{{ line }} {% endfor %} @@ -616,7 +619,7 @@ input[type=number] { text-align: right; font-family: monospace; } lines = db.get_lines(start, end) if copy: start = end = 0 - return tmpl.render(start=start, end=end, lines=lines) + return tmpl.render(action='add_free', start=start, end=end, lines=lines) def add_structured(self, db, start=0, end=0, copy=False, temp_lines=[], add_empty_line=None): tmpl = jinja2.Template(""" -- 2.30.2