From dc2b8ad4310af2a2fe842a991ff8dfdda0099a3f Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Wed, 31 Aug 2022 09:27:14 +0300 Subject: [PATCH] fixes for crashes,svx files in wallets --- core/models/survex.py | 17 +++-- core/views/uploads.py | 148 ++++++++++++++++++++++++++------------ templates/walletform.html | 4 +- 3 files changed, 117 insertions(+), 52 deletions(-) diff --git a/core/models/survex.py b/core/models/survex.py index d165d14..fe39136 100644 --- a/core/models/survex.py +++ b/core/models/survex.py @@ -281,19 +281,24 @@ class Wallet(models.Model): waldata["survex file"] = [waldata["survex file"]] ngood = 0 nbad = 0 - ticks["S"] = "black" - for svx in waldata["survex file"]: - if svx !="": - if (Path(settings.SURVEX_DATA) / svx).is_file(): + ticks["S"] = "purple" + for sx in waldata["survex file"]: + #this logic appears in several places, inc uploads.py). Refactor. + if sx !="": + if Path(sx).suffix.lower() != ".svx": + sx = sx + ".svx" + if (Path(settings.SURVEX_DATA) / sx).is_file(): ngood += 1 else: nbad += 1 if nbad == 0 and ngood >= 1: ticks["S"] = "green" - if nbad >= 1 and ngood >= 1: + elif nbad >= 1 and ngood >= 1: ticks["S"] = "orange" - if nbad >= 1 and ngood == 0: + elif nbad >= 1 and ngood == 0: ticks["S"] = "red" + else: + ticks["S"] = "black" # Cave Description if waldata["description written"]: diff --git a/core/views/uploads.py b/core/views/uploads.py index 765e834..f46db92 100644 --- a/core/views/uploads.py +++ b/core/views/uploads.py @@ -28,7 +28,7 @@ from troggle.parsers.scans import contentsjson from troggle.core.models.troggle import DataIssue from troggle.core.models.troggle import Expedition, Person, PersonExpedition from troggle.core.models.caves import LogbookEntry, QM, Cave, PersonTrip -from troggle.core.models.survex import DrawingFile, Wallet, SurvexBlock, SurvexFile +from troggle.core.models.survex import DrawingFile, Wallet, SurvexBlock, SurvexFile, SurvexPersonRole from troggle.core.views.scans import oldwallet, walletindex from troggle.core.views.caves import getCave @@ -127,13 +127,12 @@ def get_complaints(complaints, waldata, svxfiles, files, wallet, wurl): Loads the survex files names and processes all complaints ''' - # If skipping through the wllets on the upload form, the wallet may not yet exist + # If skipping through the wallets on the upload form, the wallet may not yet exist try: w = Wallet.objects.get(walletname=wallet) except ObjectDoesNotExist: return None, None - ticks = w.get_ticks() # Date if not waldata["date"]: @@ -150,6 +149,7 @@ def get_complaints(complaints, waldata, svxfiles, files, wallet, wurl): if not type(waldata["survex file"])==list: # a string also is a sequence type, so do it this way waldata["survex file"] = [waldata["survex file"]] for sx in waldata["survex file"]: + #this logic appears in several places, inc get_ticks(). Refactor. if sx !="": if Path(sx).suffix.lower() != ".svx": sx = sx + ".svx" @@ -168,6 +168,8 @@ def get_complaints(complaints, waldata, svxfiles, files, wallet, wurl): if survex_complaint: complaints.append(survex_complaint) + ticks = w.get_ticks() + # Notes required if ticks['N'] != "green": @@ -242,6 +244,7 @@ def scanupload(request, path=None): def read_json(): '''Read JSON from the wallet metadata file in the repo or fills with blank data if that files can't be read''' + waldata = {} if contents_path.is_file(): create = False # yes wallet exists because JSON exists, even if no files in the surveyscans folder, or even if that folder does not exist with open(contents_path) as json_file: @@ -256,11 +259,14 @@ def scanupload(request, path=None): print(f'--- No JSON exists, so creating blank copy') waldata = WALLET_BLANK_JSON.copy() if not waldata["survex file"]: - w = Wallet.objects.get(walletname=wallet) - b = SurvexBlock.objects.filter(scanswallet=w) - waldata["survex file"] = [] - for bsf in b: - waldata["survex file"].append(bsf.survexfile.path) + try: + w = Wallet.objects.get(walletname=wallet) + b = SurvexBlock.objects.filter(scanswallet=w) + waldata["survex file"] = [] + for bsf in b: + waldata["survex file"].append(bsf.survexfile.path) + except: + print(f'--- No wallet {wallet} exists in database') return waldata def save_json(jsondict): @@ -446,7 +452,7 @@ def scanupload(request, path=None): dirs = [] # print(f'! - FORM scanupload - start {wallet} {dirpath}') if dirpath.is_dir(): - create = False # wallet exists becausse folder exists, even if nothing in it + create = False # wallet exists because folder exists, even if nothing in it try: for f in dirpath.iterdir(): if f.is_dir(): @@ -474,49 +480,103 @@ def scanupload(request, path=None): checked = {} context = {} if waldata: # should always be true as populated by blank data if json file doesn't exist - if not waldata["people"]: - waldata["people"]=["NOBODY"] - if not type(waldata["people"])==list: - if waldata["people"][0] == '"': - waldata["people"] = waldata["people"][1:-1] - waldata["people"] = list(waldata["people"]) + + # if not type(waldata["people"])==list: + # if waldata["people"][0] == '"': + # waldata["people"] = waldata["people"][1:-1] + # waldata["people"] = list(waldata["people"]) + + + if not waldata["date"] or not waldata["people"] or waldata["people"] == ["Unknown"]: # json file does not exist, blank data, or people not typed into JSON file + # refactor into separate functions for no date set or no people set + # print(f'No date set') + print(f'\n - Incomplete or empty wallet data {wallet} {waldata=}') + refs=[] + dates = [] + team = [] + caverefs = [] + caves =[] + names =[] + svxf = '' + if waldata["survex file"]: + if not type(waldata["survex file"])==list: # a string also is a sequence type, so do it this way + waldata["survex file"] = [waldata["survex file"]] + for svxf in waldata["survex file"]: + print(f' - {svxf=}') + if svxf: + svx = Path(svxf) + if svx.suffix.lower() != ".svx": + svx = svx.with_suffix(".svx") + f = Path(settings.SURVEX_DATA) / svx + if f.is_file(): + path = svx.parent / svx.stem + #print(f' - {path=}') + svxfile = SurvexFile.objects.get(path=path) + print(f' - {svxfile=}') + caves.append(svxfile.cave) + caverefs.append(svxfile.cave.reference()) + blocks = SurvexBlock.objects.filter(survexfile= svxfile) + for b in blocks: + print(f' - - {b=} {b.scanswallet=} {b.date=}') + if b.scanswallet: + refs.append(b.scanswallet) + if b.scanswallet.walletname == wallet: + if b.date: + dates.append(b.date) + if b.name != b.title: + names.append(str(b.name) + "|" + str(b.title)) + else: + names.append(str(b.name)) + # we can use the people, across all blocks that have this *ref + QSpeople = SurvexPersonRole.objects.filter(survexblock=b) + print(f' - - {QSpeople=}') + for p in QSpeople: + team.append(p.personname) + # else: + # print(f' - Wallet not matching *ref {b.scanswallet=} {wallet}') + if dates: + waldata["date"] = min(dates).isoformat() + team = list(set(team)) + waldata["people"] = team + + caverefs = list(set(caverefs)) + caves = list(set(caves)) + + if len(caverefs) == 1: + waldata["cave"] = caverefs[0] + print(f' - Setting wallet cave to {caverefs[0]}') + waldata["description url"] = caves[0] + elif len(caverefs) == 0: + waldata["cave"] = "" + waldata["description url"] = "" + print(f' - No caves in this wallet {wallet}. ') + else: + waldata["cave"] = "several caves" + waldata["description url"] = "several.." + print(f' - More than one Cave {caves} in this wallet {wallet}. Not managed in this troggle release.') + if len(names) == 1: + if waldata["name"] == '': + waldata["name"] = names[0] + print(f' - Setting wallet name to {names[0]}') + elif len(names) == 0: + waldata["name"] = '' + print(f' - Setting wallet name blank') + else: + waldata["name"] = f"several, please edit: {names}" + print(f' - More than one block name is relevant {names} in this wallet {wallet}. Not managed in this troggle release.') - if not waldata["description url"]: - waldata["description url"]="" if waldata["cave"]: cave = waldata["cave"] # text string if waldata["name"]: psg = waldata["name"] - - if not waldata["date"]: # json file does not exist, blank data - # refactor into a separate function.. - print(f'No date set') - refs=[] - dates = [] - for svxf in waldata["survex file"]: - svx = Path(svxf) - if svx.suffix.lower() != ".svx": - svx = svx.with_suffix(".svx") - #print(f'{svx=}') - f = Path(settings.SURVEX_DATA) / svx - if f.is_file(): - path = svx.parent / svx.stem - print(f' - {path=}') - svxfile = SurvexFile.objects.get(path=path) - blocks = SurvexBlock.objects.filter(survexfile= svxfile) - for b in blocks: - print(f' - - {b=} {b.scanswallet=} {b.date=}') - refs.append(b.scanswallet) - dates.append(b.date) - waldata["date"] = min(dates).isoformat() - for w in refs: - if w.walletname != wallet: - print(f'! Not the same wallet {wallet} in *ref {w.walletname=}') + if not waldata["description url"]: + waldata["description url"]="" #Survex and survex complaints complaints, caveobject = get_complaints([], waldata, svxfiles, files, wallet, wurl) + # print(f' - {caveobject=}') + - for f in checkboxes: if waldata[f]: checked[f] = "checked" @@ -538,7 +598,7 @@ def scanupload(request, path=None): 'url': waldata["description url"], 'urlsize': str(len(str(waldata["description url"]))), 'survex': waldata["survex file"], 'survexsize': survexsize, 'cave': cave, 'psg': psg, 'psgsize': str(max(12,len(str(psg))))}) - else: # no wallet here + else: # no wallet data: should never happen as their should be default data in all cases context = {'year': year, 'prev': prev, 'next': next, 'prevy': prevy, 'nexty': nexty, 'files': files, 'dirs': dirs, 'waldata': waldata, 'svxfiles': svxfiles, 'checked': checked, diff --git a/templates/walletform.html b/templates/walletform.html index 5c3e682..66d4955 100644 --- a/templates/walletform.html +++ b/templates/walletform.html @@ -150,7 +150,7 @@ title="Cave id e.g. 2017-DM-01 or 1623/256" placeholder="{{cave}}" value="{{cave}}" />
- + Electronic survey ?
- +