diff options
author | Philip Sargent <philip.sargent@gmail.com> | 2023-10-04 18:22:54 +0300 |
---|---|---|
committer | Philip Sargent <philip.sargent@gmail.com> | 2023-10-04 18:22:54 +0300 |
commit | bc621efc361e7fc286748ad87a1160956d832c74 (patch) | |
tree | ab0d6901900a4f054c8c2bcdc238382dd4f5f51f /core/views/scans.py | |
parent | 9f4306e367517527d825aacb498eebde1e78c7b2 (diff) | |
download | troggle-bc621efc361e7fc286748ad87a1160956d832c74.tar.gz troggle-bc621efc361e7fc286748ad87a1160956d832c74.tar.bz2 troggle-bc621efc361e7fc286748ad87a1160956d832c74.zip |
wallets-per-person now finding non-survex wallets
Diffstat (limited to 'core/views/scans.py')
-rw-r--r-- | core/views/scans.py | 146 |
1 files changed, 94 insertions, 52 deletions
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}") |