From 1971f51b5281dc64193105ba1cf4a172efeb4b18 Mon Sep 17 00:00:00 2001
From: Philip Sargent <philip.sargent@klebos.com>
Date: Thu, 22 Sep 2022 00:23:22 +0300
Subject: [PATCH] find more wallets than we thought we had

---
 core/views/scans.py | 39 +++++++++++++++++++++++----------------
 1 file changed, 23 insertions(+), 16 deletions(-)

diff --git a/core/views/scans.py b/core/views/scans.py
index d3fd517..4e256a6 100644
--- a/core/views/scans.py
+++ b/core/views/scans.py
@@ -11,6 +11,7 @@ from django.http import HttpResponse
 
 from troggle.core.models.survex import Wallet, SingleScan, SurvexBlock
 from troggle.core.models.troggle import Person, Expedition
+from troggle.core.models.troggle import DataIssue
 from troggle.core.models.caves import GetCaveLookup
 from troggle.core.views.expo import getmimetype
 #from troggle.parsers.people import GetPersonExpeditionNameLookup
@@ -89,7 +90,7 @@ def fillblankpeople(w):
                 # print(f' - {wp=}')
                 nobody = wp[0].lower()
                 if  nobody == 'unknown' or nobody == 'nobody' or nobody == ' ' or nobody == '':
-                    print(f' - {wp=} {nobody=}')
+                    # print(f' - {wp=} {nobody=}')
                     populatewallet(w)
         
 def fillblankothers(w):       
@@ -100,7 +101,7 @@ def fillblankothers(w):
     c = w.cave()
     if not c or c == "":
         caveifywallet(w)
-
+        
 def fixsurvextick(w, ticks):   
     ticks["S"] = w.fixsurvextick(ticks["S"]) 
 
@@ -181,10 +182,27 @@ def cavewallets(request, caveid):
     else:
         return render(request,'errors/badslug.html', {'badslug': caveid})
 
-    # remove duplication. SOrting is done in the template
-    wallets = set(Wallet.objects.filter(survexblock__survexfile__cave=cave)) # NB a filtered set
-    manywallets = list(wallets)
+    # remove duplication. Sorting is done in the template
+    # But this only gets wallets which have survex files attached..
+    wallets = set(Wallet.objects.filter(survexblock__survexfile__cave=cave))
+    
+    # all the ones without a survexblock attached via a *ref, search for match in JSON
+    zilchwallets = set(Wallet.objects.exclude(survexblock__survexfile__cave=cave))
+    for z in zilchwallets:
+        zcave = z.cave()
+        if zcave:
+            if str(zcave) in Gcavelookup:
+                fcave = Gcavelookup[str(zcave)]
+                if str(fcave.slug()) == caveid:
+                    # print(f' - Found one ! {z.walletname=} {zcave=}')
+                    wallets.add(z)
+            else:
+                wurl = f"/scanupload/{z.walletname.replace('#',':')}"
+                print(f' - Unrecognised cave name \'{zcave}\' in {z.walletname}')
+                message = f" ! In {z.walletname} there is an unrecognised cave name '{zcave}'"
+                DataIssue.objects.update_or_create(parser='scans', message=message, url=wurl)
 
+    manywallets = list(set(wallets))
     for w in manywallets:
         fillblankpeople(w)
         fillblankothers(w)   
@@ -205,21 +223,10 @@ def oldwallet(request, path):
         message = f'Scan folder error or not found \'{path}\' .'
         return render(request, 'errors/generic.html', {'message': message})   
 
-# def walletindex(request, path):
-    # '''All years: special 'wallet' for *scanned* index pages
-    # '''
-    # print(f'! - walletindex path:{path}')
-    # try:
-        # wallet = Wallet.objects.get(walletname=urlunquote(path))  
-        # return render(request, 'walletindex.html', { 'wallet':wallet, 'settings': settings })
-    # except:
-        # message = f'Scan folder (wallet) error or not found \'{path}\' .'
-        # return render(request, 'errors/generic.html', {'message': message})   
 
 def scansingle(request, path, file):
     '''sends a single binary file to the user for display - browser decides how using mimetype
     '''
-
     try:
         wallet = Wallet.objects.get(walletname=urlunquote(path)) 
         singlescan = SingleScan.objects.get(wallet=wallet, name=file)