From 2b960865351ff8841c098a5e559847a2f4f909d4 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Mon, 23 Oct 2023 03:24:34 +0300 Subject: [PATCH] caves->wallets at parse time --- core/views/scans.py | 52 ++----------------- parsers/imports.py | 2 + parsers/survex.py | 13 +++++ .../{manywallets.html => walletsall.html} | 14 +---- 4 files changed, 21 insertions(+), 60 deletions(-) rename templates/{manywallets.html => walletsall.html} (84%) diff --git a/core/views/scans.py b/core/views/scans.py index 7cb0b94..fbd5369 100644 --- a/core/views/scans.py +++ b/core/views/scans.py @@ -53,14 +53,15 @@ def populatewallet(w): def caveifywallet(w): """Gets the caves from the list of survex files, + If called from survex parser, we never need to call it from a view too. """ # print(f' - Caveify {w=}') blocknames = [] blocks = SurvexBlock.objects.filter(scanswallet=w) for b in blocks: # NB b.cave is not populated by parser. Use b.survexfile.cave instead, or we could parse b.survexpath - if b.survexfile.cave: - w.caves.add(b.survexfile.cave) + #if b.survexfile.cave: + #w.caves.add(b.survexfile.cave) if b.name: blocknames.append(b.name) @@ -337,52 +338,7 @@ def cavewallets(request, caveid): else: return render(request, "errors/badslug.html", {"badslug": f"{caveid} - from cavewallets()"}) - # 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: - # zcaveid = z.cave() # read JSON - # if zcaveid: - # cleanid = str(zcaveid).strip("' []\"") - # if cleanid.find(',') != -1: - # # it's a list of cave ids - # wurl = f"/walletedit/{z.walletname.replace('#',':')}" - # message = f" ! In {z.walletname} cavewallets, we do not handle lists of cave ids yet '{cleanid}'" - # print(message) - # DataIssue.objects.update_or_create(parser="scans", message=message, url=wurl) - - # # it's a list of cave ids as a string. Identify any orphan caves hidden here - # ids = cleanid.split(',') - # for i in ids: - # i = i.strip("' []'") - # if is_cave(z,i): - # fcave = Gcavelookup[i.strip("' []'")] # just sets it to the last one found. nasty. bug waiting to happen - - # elif cleanid in Gcavelookup: - # fcave = Gcavelookup[cleanid] - # if str(fcave.slug()) == caveid: - # # print(f' - Found one ! {z.walletname=} {zcaveid=}') - # wallets.add(z) - # elif f"1623-{cleanid}" in Gcavelookup: # special hack for all the old wallets which are 1623 - # fcave = Gcavelookup[f"1623-{cleanid}"] - # if str(fcave.slug()) == caveid: - # # print(f' - Found one ! {z.walletname=} {zcaveid=}') - # wallets.add(z) - # elif cleanid in ['surface', 'unknown', '']: - # # message = f" ! In {z.walletname} cavewallets, ignoring '{cleanid}' as not a cave" - # # print(message) - # pass - # else: - # wurl = f"/walletedit/{z.walletname.replace('#',':')}" - # message = f" ! In {z.walletname} cavewallets, there is an unrecognised cave name '{cleanid}', adding to pending list." - # print(message) - # DataIssue.objects.update_or_create(parser="scans", message=message, url=wurl) - # add_cave_to_pending_list(cleanid, z, f"an unrecognised cave name in {z.walletname}") - # or now: wallets = cave.wallets.all() manywallets = list(set(wallets)) @@ -447,5 +403,5 @@ def allscans(request): # manywallets = Wallet.objects.all().prefetch_related('singlescan') fails as the link is defined on 'singlescan' not on 'wallet' expeditions = Expedition.objects.all() return render( - request, "manywallets.html", {"manywallets": manywallets, "settings": settings, "expeditions": expeditions} + request, "walletsall.html", {"manywallets": manywallets, "settings": settings, "expeditions": expeditions} ) diff --git a/parsers/imports.py b/parsers/imports.py index abf4b77..112dd79 100644 --- a/parsers/imports.py +++ b/parsers/imports.py @@ -55,6 +55,8 @@ def import_survex(): with transaction.atomic(): troggle.parsers.survex.LoadSurvexBlocks() print(" - Survex entrances x/y/z Positions") + with transaction.atomic(): + troggle.parsers.survex.caveifywallets() with transaction.atomic(): troggle.parsers.locations.LoadPositions() diff --git a/parsers/survex.py b/parsers/survex.py index e1633ce..a86399f 100644 --- a/parsers/survex.py +++ b/parsers/survex.py @@ -2481,6 +2481,17 @@ def MakeFileRoot(svxpath): return fileroot +def caveifywallets(): + """Gets the caves from the list of survexblocks + """ + wallets = Wallet.objects.all() + for w in wallets: + blocks = SurvexBlock.objects.filter(scanswallet=w) + for b in blocks: + # NB b.cave is not populated by parser. Use b.survexfile.cave instead, or we could parse b.survexpath + if b.survexfile.cave: + w.caves.add(b.survexfile.cave) + def LoadSurvexBlocks(): global dup_includes @@ -2536,4 +2547,6 @@ def LoadSurvexBlocks(): if dup_includes > 0: print(f" - ERROR: There are {dup_includes} duplicate *includes in the final list") print(" - Loaded All Survex Blocks.") + + diff --git a/templates/manywallets.html b/templates/walletsall.html similarity index 84% rename from templates/manywallets.html rename to templates/walletsall.html index a7ed11c..51a326b 100644 --- a/templates/manywallets.html +++ b/templates/walletsall.html @@ -29,7 +29,7 @@ see https://docs.djangoproject.com/en/dev/ref/models/querysets/#prefetch-related --> - + {% for wallet in manywallets %} @@ -46,17 +46,7 @@ see https://docs.djangoproject.com/en/dev/ref/models/querysets/#prefetch-related {% endfor %} - + {% endfor %}
Scans folderFilesSurvex blocksCave (new)Cave (old)
Scans folderFilesSurvex blocksCave
{{wallet.walletname}} - {% for survexblock in wallet.survexblock_set.all %} - {% ifchanged survexblock.survexfile.cave %} - {{survexblock.survexfile.cave.slug}} - {% endifchanged %} - {% empty %} - {% if wallet.cave %} - {{wallet.cave}} - {% endif %} - {% endfor %} -