From 83d058221dae2d33548ecbceae42e11ec6ccb0e1 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Wed, 6 Sep 2023 13:20:29 +0300 Subject: [PATCH] Fixed survex file list for one or more caves --- core/models/caves.py | 12 +-- core/views/survex.py | 72 ++++++------- ...svxcavesingle404.html => svxcaves404.html} | 0 .../{svxcavesingle.html => svxcaves.html} | 18 ++-- templates/svxcaveseveral.html | 101 ------------------ urls.py | 6 +- 6 files changed, 50 insertions(+), 159 deletions(-) rename templates/errors/{svxcavesingle404.html => svxcaves404.html} (100%) rename templates/{svxcavesingle.html => svxcaves.html} (89%) delete mode 100644 templates/svxcaveseveral.html diff --git a/core/models/caves.py b/core/models/caves.py index d272744..e8cfeea 100644 --- a/core/models/caves.py +++ b/core/models/caves.py @@ -490,10 +490,9 @@ def GetCaveLookup(): Used when parsing wallets contents.json file too in views/uploads.py - Does NOT detect duplicates! Needs fixing. - Needs to be a proper funciton that raises an exception if there is a duplicate. + Needs to be a proper function that raises an exception if there is a duplicate. OR we could set it to return None if there are duplicates, and require the caller to - fall back on doing the actual database query it wants rather thna using this cache shortcut + fall back on doing the actual database query it wants rather than using this cache shortcut """ duplicates = {} @@ -506,10 +505,7 @@ def GetCaveLookup(): else: if cave == Gcavelookup[id]: pass # same id, same cave - else: # same id but different cave - # message = f" - Warning: ignoring alias id '{id:3}'. Caves '{Gcavelookup[id]}' and '{cave}'. " - # print(message) - # DataIssue.objects.create(parser="aliases", message=message) + else: # same id but different cave, e.g. 122 => 1623-122 and 1626-122 duplicates[id] = 1 global Gcavelookup @@ -689,7 +685,7 @@ def GetCaveLookup(): Gcavelookup[i[0]] = Gcavelookup[i[1]] else: message = f" * Coding or cave existence mistake, cave for id '{i[1]}' does not exist. Expecting to set alias '{i[0]}' to it" - # print(message) + print(message) DataIssue.objects.create(parser="aliases", message=message) addmore = {} diff --git a/core/views/survex.py b/core/views/survex.py index bd9df25..3ecd523 100644 --- a/core/views/survex.py +++ b/core/views/survex.py @@ -17,7 +17,7 @@ from django.views.decorators.csrf import ensure_csrf_cookie import troggle.settings as settings from troggle.core.models.logbooks import LogbookEntry -from troggle.core.models.caves import Cave +from troggle.core.models.caves import Cave, GetCaveLookup from troggle.core.models.survex import SurvexFile, SurvexBlock, SurvexDirectory from troggle.core.models.wallets import Wallet from troggle.core.utils import only_commit @@ -670,43 +670,41 @@ def survexdir(request): # print(f, f.cave) return render(request, "survexdir.html", {"survexdirs": sds, "survexfiles": survexfiles}) - -def survexcavesingle(request, survex_cave): +def get_directories(cave): + sds = [] + sfs = cave.survexfile_set.all() + for sf in sfs: + sds.append(sf.survexdirectory) + return list(set(sds)) + + + +def survexcavesingle(request, cave_shortname): """parsing all the survex files of a single cave and showing that it's consistent and can find all - the files and people. Should explicitly fix the kataster number thing. - kataster numbers are not unique across areas. This used to be a db constraint but we need to manage - this ourselves as we don't want the parser aborting with an error message. - - Should use getCave() from models_caves + the files and people. """ - sc = survex_cave - try: - cave = Cave.objects.get(kataster_number=sc) # This may not be unique. - return render(request, "svxcavesingle.html", {"settings": settings, "cave": cave}) - - except ObjectDoesNotExist: - # can get here if the survex file is in a directory labelled with unofficial number not kataster number. - # maybe - and _ mixed up, or CUCC-2017- instead of 2017-CUCC-, or CUCC2015DL01 . Let's not get carried away.. - - # or it might be an exact search for a specific survefile but just missing the '.svx. - if (SVXPATH / Path(survex_cave + ".svx")).is_file(): - return svx(request, survex_cave) - - for unoff in [sc, sc.replace("-", "_"), sc.replace("_", "-"), sc.replace("-", ""), sc.replace("_", "")]: - try: - cave = Cave.objects.get(unofficial_number=unoff) # return on first one we find - return render(request, "svxcavesingle.html", {"settings": settings, "cave": cave}) - except ObjectDoesNotExist: - continue # next attempt in for loop - return render(request, "errors/svxcavesingle404.html", {"settings": settings, "cave": sc}) - - except MultipleObjectsReturned: - caves = Cave.objects.filter(kataster_number=survex_cave) - return render(request, "svxcaveseveral.html", {"settings": settings, "caves": caves}) - - except: - return render(request, "errors/svxcavesingle404.html", {"settings": settings, "cave": sc}) + + Gcavelookup = GetCaveLookup() + if cave_shortname in Gcavelookup: + cave = Gcavelookup[cave_shortname] + # print(f"survexcavesingle {cave_shortname=} => {cave=}") + cave.sds = get_directories(cave) + return render(request, "svxcaves.html", {"settings": settings, "caves": [cave]}) + else: + # not a cave or an ambiguous short name, e.g. "122" + + # if (SVXPATH / Path(cave_shortname + ".svx")).is_file(): + # return svx(request, cave_shortname) + caves = Cave.objects.filter(kataster_number=cave_shortname) + if len(caves) > 0: + # print(f"many {cave_shortname=} => {caves=}") + for cave in caves: + cave.sds = get_directories(cave) + # print(f"many {cave=} => {cave.sds=}") + return render(request, "svxcaves.html", {"settings": settings, "caves": caves}) + else: + return render(request, "errors/svxcaves404.html", {"settings": settings, "cave": cave_shortname}) def check_cave_registered(area, survex_cave): """Checks whether a cave has been properly registered when it is found in the Loser repo @@ -714,8 +712,8 @@ def check_cave_registered(area, survex_cave): Currently Caves are only registered if they are listed in :expoweb: settings.CAVEDESCRIPTIONS so we need to add in any more here. - This function runs but does not seem to be used?! - A serious bodge anyway. + This function is used in survexcaveslist(request) + A serious bodge. """ try: cave = Cave.objects.get(kataster_number=survex_cave) diff --git a/templates/errors/svxcavesingle404.html b/templates/errors/svxcaves404.html similarity index 100% rename from templates/errors/svxcavesingle404.html rename to templates/errors/svxcaves404.html diff --git a/templates/svxcavesingle.html b/templates/svxcaves.html similarity index 89% rename from templates/svxcavesingle.html rename to templates/svxcaves.html index ea992b9..0eed31d 100644 --- a/templates/svxcavesingle.html +++ b/templates/svxcaves.html @@ -3,21 +3,18 @@ {% block title %}List of survex files{% endblock %} {% block content %} +{% for cave in caves %} {% autoescape off %}

Surveys for {{cave.official_name}} - id:{{cave}}

{% endautoescape %} -

Cave description: {{cave.url}}

Wallets: {{cave|safe}}

-{% for survexdirectory in cave.survexdirectory_set.all %} - {{survexdirectory.path}} +{% for survexdirectory in cave.sds %} + {{survexdirectory.path}}   {% empty %}

If you were expecting to see a list of survex files here and a summary table of who did what and when, perhaps because you followed a link from the master caves' survex list page which showed that such survex files clearly existed, and yet there is nothing here but a blank; then this will be because
@@ -42,7 +39,7 @@ to go to a form to correct the online data. Instructions for filling in this form are in this part of the survey handbook.

-{% for survexdirectory in cave.survexdirectory_set.all %} +{% for survexdirectory in cave.sds %}

{{survexdirectory.path}}

@@ -98,8 +95,9 @@ to go to a form to correct the online data. {% endif %} - {% endfor %} - {% endfor %} + {% endfor %} + {% endfor %}
Survex fileBlockDateExplorerslengthTitlesScans
-{% endfor %} +{% endfor %} +{% endfor %} {% endblock %} diff --git a/templates/svxcaveseveral.html b/templates/svxcaveseveral.html deleted file mode 100644 index de45337..0000000 --- a/templates/svxcaveseveral.html +++ /dev/null @@ -1,101 +0,0 @@ - -{% extends "base.html" %} -{% block title %}List of survex files{% endblock %} -{% block content %} - -{% for cave in caves %} - - {% autoescape off %} -

Surveys for {{cave.official_name}} - identifier:{{cave}}

- {% endautoescape %} - -

Cave description: {{cave.url}} -

-

- {% for survexdirectory in cave.survexdirectory_set.all %} - This cave has a linked survexdirectory: {{survexdirectory.path}} in the database. See the table below. - {% empty %} -

If you were expecting to see a list of survex files here and a summary table of who did what and when, perhaps - because you followed a link from the master caves' survex list page which showed that such survex files clearly existed, and yet there is nothing here but a blank; then this will be because the survex (.svx) files have been stored on the server in the - :loser: repository - but whoever was - responsible has not yet created the appropriate entries in the XML file in the - :expoweb: repository. - It is the XML file which registers the cave description and ties together - the survex files with everything else. -

The process for registering a new cave is documented in - this part of the survey handbook. - {% endfor %} -

- -

If you can see a filename here: [ {{cave.survex_file}}  ] - which does not match any in the list below including the directories beginning with caves-162X/ - not just the filename, then (if logged on) you can - click here /{{cave.slug}}_cave_edit - to go to a form to correct the online data. - Instructions for filling in this form are in this part - of the survey handbook. -

- {% for survexdirectory in cave.survexdirectory_set.all %} -

{{survexdirectory.path}}

- - - {% for survexfile in survexdirectory.survexfile_set.all %} - - {% if survexfile.exists %} - - - {% for survexblock in survexfile.survexblock_set.all %} - - - - - - - - - - - - - - - - - {% endfor %} - {% endfor %} -
Survex fileBlockDateExplorerslengthTitlesScans
- {% else %} - - {% endif %} - - {% if survexfile == survexdirectory.primarysurvexfile %} - {% url "svx" survexfile.path %} - {% else %} - {% url "svx" survexfile.path %} - {% endif %} -
{{survexblock.name}} - {% if survexblock.expedition %} - {{survexblock.date|date:"D d M Y"}} - {% else %} - - {% endif %} - - {% for personrole in survexblock.survexpersonrole_set.all %} - {% if personrole.personexpedition %} - {{personrole.personname}} - {% else %} - {{personrole.personname}} - {% endif %} - {% endfor %} - {{survexblock.legslength|stringformat:".1f"}} - {{survexblock.title}} - - {% if survexblock.scanswallet %} - {{survexblock.scanswallet.walletname}} - {% endif %} -

- {% endfor %} -{% endfor %} -{% endblock %} diff --git a/urls.py b/urls.py index 513a1eb..499c612 100644 --- a/urls.py +++ b/urls.py @@ -184,8 +184,8 @@ trogglepatterns = [ # The survexfile pages path('survexdir', survex.survexdir, name="survexdir"), - path('survexfile', survex.survexcavesingle, {'survex_cave': ''}, name="survexcavessingle"), - path('survexfile/', survex.survexcavesingle, {'survex_cave': ''}, name="survexcavessingle"), + path('survexfile', survex.survexcavesingle, {'cave_shortname': ''}, name="survexcavessingle"), + path('survexfile/', survex.survexcavesingle, {'cave_shortname': ''}, name="survexcavessingle"), path('survexfile/caves', survex.survexcaveslist, name="survexcaveslist"), path('survexfile/caves/', survex.survexcaveslist, name="survexcaveslist"), # auto slash not working @@ -193,7 +193,7 @@ trogglepatterns = [ path('survexfile/.3d', survex.threed, name="threed"), path('survexfile/.log', survex.svxlog, name="svxlog"), path('survexfile/.err', survex.err, name="err"), - path('survexfile/', survex.survexcavesingle, name="survexcavessingle"), + path('survexfile/', survex.survexcavesingle, name="survexcavessingle"), path('survexfilewild', statistics.svxfilewild, name="svxfilewild"), path('survexfilewild/', statistics.svxfilewild, name="svxfilewild"),