diff --git a/core/models/caves.py b/core/models/caves.py index 12bb0f9..2769cdb 100644 --- a/core/models/caves.py +++ b/core/models/caves.py @@ -701,12 +701,12 @@ def GetCaveLookup(): if ldup: message = f" - Ambiguous aliases removed: {ldup}" print(message) - DataIssue.objects.create(parser="aliases ok", message=message) + DataIssue.objects.update_or_create(parser="aliases ok", message=message) for c in Gcave_count: if Gcave_count[c] > 1: message = f" ** Duplicate cave id count={Gcave_count[c]} id:'{Gcavelookup[c]}' cave __str__:'{c}'" print(message) - DataIssue.objects.create(parser="aliases", message=message) + DataIssue.objects.update_or_create(parser="aliases", message=message) return Gcavelookup diff --git a/core/views/scans.py b/core/views/scans.py index 7e25862..2f3d811 100644 --- a/core/views/scans.py +++ b/core/views/scans.py @@ -7,15 +7,14 @@ from django.http import HttpResponse from django.shortcuts import render from troggle.core.models.caves import GetCaveLookup -from troggle.core.models.survex import SingleScan, SurvexBlock +from troggle.core.models.survex import SingleScan, SurvexBlock, SurvexPersonRole from troggle.core.models.wallets import Wallet -from troggle.core.models.troggle import DataIssue, Expedition, Person +from troggle.core.models.troggle import DataIssue, Expedition, Person, PersonExpedition from troggle.core.views.expo import getmimetype -from troggle.parsers.survex import set_walletdate from troggle.parsers.caves import add_cave_to_pending_list +from troggle.parsers.people import GetPersonExpeditionNameLookup +from troggle.parsers.survex import set_walletdate -# from troggle.parsers.people import GetPersonExpeditionNameLookup -# import parsers.surveys """one of these views serves files as binary blobs, and simply set the mime type based on the file extension, as does the urls.py dispatcher which sends them here. Here they should actually have the filetype checked @@ -37,21 +36,21 @@ add this file in to the todo list thinggy. def populatewallet(w): """Copy survex data here just for display, not permanently - Only gets data from the survex file when it was parsed on import.. - so doesn't work if there is no *ref value + Only gets data from the survex file when it was parsed on import, or edited (& thus parsed) online, + so doesn't work if there was no *ref value """ - survexpeople = [] + slugpeople = [] blocks = SurvexBlock.objects.filter(scanswallet=w) for b in blocks: for personrole in b.survexpersonrole_set.all(): - survexpeople.append(personrole.personname) - w.persons = list(set(survexpeople)) + slugpeople.append(personrole.person) # Person objects, not the names anymore + w.slugpeople = list(set(slugpeople)) - def caveifywallet(w): """Gets the cave from the list of survex files, only selects one of them though. Only used for display. + FIX THIS to display many caves """ # print(f' - Caveify {w=}') blocknames = [] @@ -73,18 +72,41 @@ def caveifywallet(w): def fillblankpeople(w): - # this isn't working..? why? Because it needs a *ref and an import - wp = w.people() - w.persons = wp - if not wp: + """Find people attached to a wallet via the survex files + if no one explicitly attached. + the JSON string which may OR MAY NOT be formatted as a list. + + w.slugpeople is set only if there is no explicit string of people's name in the wallet + w.persons is set only if there is an explicit list of peoples' names in the wallet + The template choses which to display depending on whether w.slugpeople exists or not. + """ + def nobody(wplist): + if len(wplist) > 1: + return False + + nobod = wp[0].lower() + if nobod == "unknown" or nobod == "nobody" or nobod == " " or nobod == "": + return True + else: + return False + + wp = w.people() # just a list of names as strings, direct from JSON. Replace with list of Person objects in parser? + if not isinstance(wp, list): # might be None + print(f"{w} NOT A LIST {type(wp)}: {wp}") populatewallet(w) + return + + if not wp: # e.g. empty list + # print(f"{w} {wp=}") + populatewallet(w) # sets w.slugpeople + return + + + if nobody(wp): + populatewallet(w) # sets w.slugpeople else: - if len(wp) == 1: - # print(f' - {wp=}') - nobody = wp[0].lower() - if nobody == "unknown" or nobody == "nobody" or nobody == " " or nobody == "": - # print(f' - {wp=} {nobody=}') - populatewallet(w) + w.persons = wp + return def is_cave(wallet, id): if not id: @@ -145,41 +167,61 @@ def fixsurvextick(w, ticks): def walletslistperson(request, slug): """Page which displays a list of all the wallets for a specific person HORRIBLE linear search through everything. Index and do SQL query properly + Currently ONLY getting wallets with survex files attached, not free-text searching the wallet.people list """ - # This is where we face having to re-do everything to do with names properly, rather than the horrible series of hacks over 20 years.. # GetPersonExpeditionNameLookup - def tickspersonwallet(p): - manywallets = [] - wallets = Wallet.objects.all() - for w in wallets: - w.persons = w.people() # ephemeral attribute for web page + + # Remember that 'personexpedition__expedition' is interpreted by Django to mean the + # 'expedition' object which is connected by a foreign key to the 'personexpedition' + # object, which is a field of the PersonLogEntry object: + # PersonLogEntry.objects.filter(personexpedition__expedition=expo) + + def personwallet(p): + manywallets = set() + + sps = SurvexPersonRole.objects.filter(person=p) + for sp in sps: + w = sp.survexblock.scanswallet + if w: + manywallets.add(w) + + pes = PersonExpedition.objects.filter(person=p) + for person_expo in pes: + expo = person_expo.expedition + year = expo.year + + wallets = Wallet.objects.filter(walletyear__year=year) + for w in wallets: + if w in manywallets: # already seen it + continue + w.persons = w.people() # ephemeral attribute 'persons' for web page + crew = GetPersonExpeditionNameLookup(expo) + for n in w.persons: + if n.lower() in crew: + if crew[n.lower()] == person_expo: + manywallets.add(w) + # print(f"{w} Found a non-survex wallet for {person_expo}") + else: + nobod = n.lower() + if nobod == "unknown" or nobod == "nobody" or nobod == " " or nobod == "": + pass + else: + wurl = f"/walletedit/{w.walletname.replace('#',':')}" + message = f"{w} name '{n}' NOT found with GetPersonExpeditionNameLookup on {year} ?!" + print(message) + DataIssue.objects.update_or_create(parser="scans", message=message, url=wurl) + + for w in manywallets: fillblankpeople(w) - if w.persons: - if p.fullname in w.persons: - manywallets.append(w) - fillblankothers(w) - w.ticks = w.get_ticks() # the complaints in colour form - fixsurvextick(w, w.ticks) + fillblankothers(w) + w.ticks = w.get_ticks() # the complaints in colour form + fixsurvextick(w, w.ticks) return manywallets # print("-walletslistperson") p = Person.objects.get(slug=slug) - # try: - - # if last_name: - # p = Person.objects.get(fullname=f"{first_name} {last_name}") - # else: - # # special Wookey-hack - # p = Person.objects.get(first_name=f"{first_name}") - # except: - # # raise - # return render( - # request, - # "errors/generic.html", - # {"message": f'Unrecognised name of a expo person: "{first_name} {last_name}"'}, - # ) - - manywallets = tickspersonwallet(p) + + manywallets = personwallet(p) expeditions = Expedition.objects.all() print("--") return render( @@ -269,7 +311,7 @@ def cavewallets(request, caveid): if cleanid.find(',') != -1: # it's a list of cave ids wurl = f"/walletedit/{z.walletname.replace('#',':')}" - message = f" ! In {z.walletname} we do not handle lists of cave ids yet '{cleanid}'" + 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) @@ -291,12 +333,12 @@ def cavewallets(request, caveid): # print(f' - Found one ! {z.walletname=} {zcaveid=}') wallets.add(z) elif cleanid in ['surface', 'unknown', '']: - message = f" ! In {z.walletname} ignore '{cleanid}' " + 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} there is an unrecognised cave name '{cleanid}', adding to pending list." + 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}") diff --git a/parsers/people.py b/parsers/people.py index 01998e1..0e4ca30 100644 --- a/parsers/people.py +++ b/parsers/people.py @@ -170,6 +170,20 @@ def who_is_this(year, possibleid): return None +def when_on_expo(name): + """Returns a list of PersonExpedition objects for the string, if recognised as a name + """ + person_expos = [] + expos = Expedition.objects.all() + for expo in expos: + expoers = GetPersonExpeditionNameLookup(expo) + if name in expoers: + person_expos.append(expoers[name]) + print(f"{name} => {expoers[name]}") + + return person_expos + + global foreign_friends foreign_friends = [ "Aiko", @@ -207,7 +221,8 @@ def known_foreigner(id): # Refactor. The dict GetPersonExpeditionNameLookup(expo) indexes by name and has values of personexpedition -# This is convoluted, the whole personexpedition concept is unnecessary? +# This is convoluted, the personexpedition concept is unnecessary, should it just retunr person?? +# Or better, query with a string and return a list of personexpeditions Gpersonexpeditionnamelookup = {} diff --git a/parsers/scans.py b/parsers/scans.py index 4cfb7ee..d091a3d 100644 --- a/parsers/scans.py +++ b/parsers/scans.py @@ -30,6 +30,8 @@ def load_all_scans(): It does NOT read or validate anything in the JSON data attached to each wallet. Those checks are done at runtime, when a wallet is accessed, not at import time. + + Loads people as a simple string of fullnames. We should replace this with a list of Person slugs. """ print(" - Loading Survey Scans") diff --git a/parsers/survex.py b/parsers/survex.py index 056487d..70a0962 100644 --- a/parsers/survex.py +++ b/parsers/survex.py @@ -153,7 +153,7 @@ def get_team_on_trip(survexblock): def get_people_on_trip(survexblock): """Gets the displayable names of the people on a survexbock trip. Only used for complete team.""" - qpeople = get_team_on_trip(survexblock) + qpeople = get_team_on_trip(survexblock) # qpeople is a Query List people = [] for p in qpeople: diff --git a/templates/cavewallets.html b/templates/cavewallets.html index b803737..a24dcf2 100644 --- a/templates/cavewallets.html +++ b/templates/cavewallets.html @@ -15,7 +15,7 @@ traced to produce Tunnel or Therion drawings and eventually the final complete c
This lists all the files in a wallet, some of which may not be for this specific cave.
See also wallets