From 5a6e56223e59fb628f88070957b39a42d3b7c945 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Mon, 27 Nov 2023 18:02:41 +0100
Subject: [PATCH] Improve /fix accounting scripts.

---
 calories.py |  3 +--
 ledger.py   | 12 +++++++-----
 plomlib.py  |  3 +++
 unite.py    |  2 +-
 4 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/calories.py b/calories.py
index 4352567..28b1a16 100644
--- a/calories.py
+++ b/calories.py
@@ -251,8 +251,7 @@ class ConsumptionsHandler(PlomHandler):
                 to_delete += [target]
         i = 0
         if 'eatable_uuid' in postvars.keys():
-            for uuid_encoded in postvars['eatable_uuid']:
-                uuid = uuid_encoded
+            for uuid in postvars['eatable_uuid']:
                 if uuid not in to_delete:
                     e = Eatable(decode("title", i, False), decode("cals", i), decode("sugar_g", i), decode("standard_g", i), decode("comments", i, False))
                     db.add_eatable(uuid, e)
diff --git a/ledger.py b/ledger.py
index 87d219e..b6fe064 100755
--- a/ledger.py
+++ b/ledger.py
@@ -610,7 +610,7 @@ class LedgerDB(PlomDB):
             start = end = 0
         desc = head_comment = ''
         if len(bookings) == 0:
-            for i in range(1, 3):
+            for i in range(1, 8):
                 booking_lines += [{'i': i, 'acc': '', 'amt': '', 'curr': '€', 'comment': ''}]
             date=today
         else:
@@ -737,6 +737,7 @@ class LedgerHandler(PlomHandler):
             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)
@@ -775,10 +776,11 @@ class LedgerHandler(PlomHandler):
             # otherwise just re-build editing form
             else:
                 if prefix + '/add_structured' == parsed_url.path: 
-                    edit_content = db.add_structured(db, start, end, temp_lines=lines, add_empty_line=add_empty_line)
+                    edit_content = db.add_structured(start, end, temp_lines=lines, add_empty_line=add_empty_line)
                 else:
-                    edit_content = db.add_free(db, start, end)
-                self.send_HTML(edit_content)
+                    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)
 
@@ -815,7 +817,7 @@ class LedgerHandler(PlomHandler):
             else:
                 page = db.ledger_as_html()
             header = jinja2.Template(html_head).render(prefix=prefix)
-            self.send_HTML(header+ page)
+            self.send_HTML(header + page)
         except PlomException as e:
             self.fail_400(e)
 
diff --git a/plomlib.py b/plomlib.py
index 6a13f05..18c2f18 100644
--- a/plomlib.py
+++ b/plomlib.py
@@ -27,6 +27,9 @@ class PlomDB:
     def backup(self):
         import shutil
         from datetime import datetime, timedelta
+        if not os.path.exists(self.db_file):
+            return
+
         # collect modification times of numbered .bak files
         bak_prefix = f'{self.db_file}.bak.'
         backup_dates = []
diff --git a/unite.py b/unite.py
index 14a7e3c..64a72af 100644
--- a/unite.py
+++ b/unite.py
@@ -3,7 +3,7 @@ from urllib.parse import urlparse
 from income_progress_bars import IncomeProgressHandler 
 from calories import ConsumptionsHandler 
 from ledger import LedgerHandler 
-server_port = 8084
+server_port = 8081
 
 
 class UnitedRequestHandler(PlomHandler):
-- 
2.30.2