From c8163ab0cd08682eae783544b69f31755b85857f Mon Sep 17 00:00:00 2001
From: Philip Sargent <philip.sargent@klebos.com>
Date: Mon, 3 Oct 2022 21:18:35 +0300
Subject: [PATCH] fix bug for wallet with empty fpath

---
 core/models/survex.py       | 16 ++++++++++++----
 parsers/scans.py            |  3 ++-
 templates/logbookentry.html | 16 ++++++++--------
 3 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/core/models/survex.py b/core/models/survex.py
index 1071537..b7c0b9b 100644
--- a/core/models/survex.py
+++ b/core/models/survex.py
@@ -277,15 +277,23 @@ class Wallet(models.Model):
     def get_fnames(self):
         '''Filenames without the suffix, i.e. without the ".jpg"
         '''
-        dirpath = Path(settings.SCANS_ROOT, self.fpath)
+        dirpath = Path(settings.SCANS_ROOT, self.fpath) # does nowt as fpath is a rooted path already
         files = []
-        if dirpath.is_dir():
+        if not self.fpath:
+            files.append(f"Incorrect path to wallet contents: '{self.fpath}'")
+            return files
+        if not dirpath.is_dir():
+            files.append(f"Incorrect path to wallet contents: '{self.fpath}'")
+            return files
+        else:
             try:
                 for f in dirpath.iterdir():
                     if f.is_file():
-                        if f.name != 'contents.json' and f.name != 'walletindex.html':
-                            files.append(Path(f.name).stem)
+                        files.append(Path(f.name).stem)
+                    else:
+                        files.append(f"-{Path(f.name).stem}-")
             except FileNotFoundError:
+                files.append("FileNotFoundError")
                 pass
         return files
         
diff --git a/parsers/scans.py b/parsers/scans.py
index 9cbeeab..272a78f 100644
--- a/parsers/scans.py
+++ b/parsers/scans.py
@@ -126,7 +126,8 @@ def load_all_scans():
                             print("\n   ", end='')
 
                         print(f"{walletname} ", end='')
-                        wallet, created = Wallet.objects.update_or_create(walletname=walletname)
+                        fpath = Path(settings.SCANS_ROOT, str(yeardir.stem), walletname) 
+                        wallet, created = Wallet.objects.update_or_create(walletname=walletname, fpath=fpath)
                         wallets[walletname] = wallet
                         # could now also load the json and use it. check &ref is correct or missing too..
                         if not created:
diff --git a/templates/logbookentry.html b/templates/logbookentry.html
index d8a4817..9090928 100644
--- a/templates/logbookentry.html
+++ b/templates/logbookentry.html
@@ -85,15 +85,15 @@
         {% if wallets %}<u>Wallets</u> on this date:<br>
                 <span style="font-size: 70%; ">
                 {% for item in wallets %}
-                &nbsp;&nbsp;&nbsp;&nbsp;<a href="/survey_scans/{{item.walletname|urlencode}}/">{{item.walletname|safe}}</a>
-                {% if item.name %}
-                    {{item.name|safe}} <br/>
-                {% else %}
-                    {{item.get_fnames|safe}}
-                    <br/>
-                {% endif %}
+                    &nbsp;&nbsp;&nbsp;&nbsp;<a href="/survey_scans/{{item.walletname|urlencode}}/">{{item.walletname|safe}}</a>
+                    {% if item.name %}
+                        {{item.name|safe}} <br/>
+                    {% else %}
+                        {{item.get_fnames|safe}}
+                        <br/>
+                    {% endif %}
                 {% empty %}
-                <em>None found for this date.</em><br>
+                    <em>None found for this date.</em><br>
                 {% endfor %}
                 </span>
         {% else %}