From 175e71facf74ad91347eb9c7a6865a13b8a3cd09 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Fri, 3 Feb 2023 22:19:51 +0000 Subject: [PATCH] Many many bugs fixed for >1 svx file on a wallet --- core/models/wallets.py | 8 +- core/views/scans.py | 4 +- core/views/wallets_edit.py | 161 +++++++++++++++++++++---------------- templates/walletform.html | 9 +-- 4 files changed, 100 insertions(+), 82 deletions(-) diff --git a/core/models/wallets.py b/core/models/wallets.py index 931a1fc..551e3e1 100644 --- a/core/models/wallets.py +++ b/core/models/wallets.py @@ -32,7 +32,8 @@ class Wallet(models.Model): return urljoin(settings.URL_ROOT, reverse("singlewallet", kwargs={"path": re.sub("#", "%23", self.walletname)})) def get_json(self): - """Read the JSON file for the wallet and do stuff""" + """Read the JSON file for the wallet and do stuff + Do it every time it is queried, to be sure the result is fresh""" # jsonfile = Path(self.fpath, 'contents.json') # Get from git repo instead @@ -102,9 +103,8 @@ class Wallet(models.Model): """Reads all the JSON data just to get the JSON date.""" if self.walletdate: return self.walletdate - if not self.get_json(): + if not (jsondata := self.get_json()): # WALRUS return None - jsondata = self.get_json() # use walrus operator? datestr = jsondata["date"] if not datestr: @@ -259,7 +259,7 @@ class Wallet(models.Model): ticks["N"] = "red" else: ticks["N"] = "green" - print(f"{self.walletname} {ticks['N'].upper()} {notes_scanned=} {notes_required=} {waldata['notes not required']=}") + # print(f"{self.walletname} {ticks['N'].upper()} {notes_scanned=} {notes_required=} {waldata['notes not required']=}") # Plan drawing required plan_scanned = reduce(operator.or_, [f.startswith("plan") for f in files], False) diff --git a/core/views/scans.py b/core/views/scans.py index 9555b3f..f0353b9 100644 --- a/core/views/scans.py +++ b/core/views/scans.py @@ -132,7 +132,7 @@ def walletslistperson(request, first_name, last_name): fixsurvextick(w, w.ticks) return manywallets - print("-walletslistperson") + # print("-walletslistperson") try: if last_name: @@ -185,7 +185,7 @@ def walletslistyear(request, year): return manywallets - print("-walletslistyear") + # print("-walletslistyear") if year < 1976 or year > 2050: return render(request, "errors/generic.html", {"message": "Year out of range. Must be between 1976 and 2050"}) diff --git a/core/views/wallets_edit.py b/core/views/wallets_edit.py index fad7f11..7cc651a 100644 --- a/core/views/wallets_edit.py +++ b/core/views/wallets_edit.py @@ -347,7 +347,7 @@ def walletedit(request, path=None): recent_year = recent_name[:4] recent_number = recent_name[5:] - print(f"---identify_most_recent_wallet: {recent_year=} {recent_number=}") + # print(f"---identify_most_recent_wallet: {recent_year=} {recent_number=}") return recent_year, recent_number def create_nav_links(wallet): @@ -481,7 +481,34 @@ def walletedit(request, path=None): return (team == ["Unknown"] or team == [""] or team == "") - + + def empty_string(thing): + return (thing == [""] + or thing == [] + or thing == "" + or thing == "[]" + or thing is None) + + def make_valid_date(date): + datestr = date.replace(".", "-") + try: + samedate = datetime.date.fromisoformat(datestr) + except ValueError: + # probably a single digit day number. HACKUS MAXIMUS. + # clearly we need to fix this when we first import date strings.. + datestr = datestr[:-1] + "0" + datestr[-1] + # datestr = f"{datestr:02d}" + print(f"! - ValueError, trying.. {datestr=} ") + try: + samedate = datetime.date.fromisoformat(datestr) + except: + try: + samedate = datetime.date.fromisoformat(datestr[:10]) + except: + print(f"! - ValueError, FAILED {datestr=} ") + samedate = None + return samedate + def scan_survexblocks(svxfile): """Scans for *team people attached to all the survex blocks in this svxfile This could be rather a lot for some compedious survex files! So would need @@ -493,26 +520,25 @@ def walletedit(request, path=None): try: blocks = SurvexBlock.objects.filter(survexfile=svxfile) for b in blocks: - print(f" - - {b=} {b.scanswallet=} {b.date=}") + # print(f" - - - - {b=} {b.scanswallet.walletname} {b.date=} ") if b.scanswallet: wallet_refs.append(b.scanswallet) # other wallets - if b.scanswallet.walletname == wallet: - if b.date: - dates.append(b.date) - if b.name != b.title: - blocknames.append(str(b.name) + "|" + str(b.title)) - else: - blocknames.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: - print(f" - - {p.personname} ") - team.append(p.personname) + #if b.scanswallet.walletname == wallet: # only if we assume *ref all correct! + if b.date: + dates.append(b.date) + if b.name != b.title: + blocknames.append(str(b.name) + "|" + str(b.title)) + else: + blocknames.append(str(b.name)) + QSpeople = SurvexPersonRole.objects.filter(survexblock=b) + # print(f" - - {QSpeople=}") + for p in QSpeople: + # print(f" - - {p.personname} ") + team.append(p.personname) except: message = " - No associated survex blocks found for this wallet" print(message) - + # print(f" - - - ", wallet_refs, dates, blocknames, team) return wallet_refs, dates, blocknames, team def scan_survexfiles(survex_paths): @@ -534,7 +560,7 @@ def walletedit(request, path=None): if not svxf: # not a blank string continue - print(f" - - {svxf=} ") + # print(f" - - {svxf=} ") svx = Path(svxf) if svx.suffix.lower() != ".svx": svx = svx.with_suffix(".svx") @@ -547,22 +573,26 @@ def walletedit(request, path=None): try: svxfile = SurvexFile.objects.get(path=fpath) - print(f" - {svxfile=}") if svxfile.cave: caves.append(svxfile.cave) - cave_refs.append(svxfile.cave.reference()) + cave_refs.append(svxfile.cave.reference()) # this is a string?! + - wallet_refs, dates, names, team = scan_survexblocks(svxfile) + w, d, n, t = scan_survexblocks(svxfile) + wallet_refs.extend(w) + dates.extend(d) + names.extend(n) + team.extend(t) except: message = "Specified survex file not found - database may be empty." print(message) # This failure will also get picked up by the "S" colour code red or orange - - if len(cave_refs) == 1: - caves = cave_refs[0] + caves = list(set(caves)) + if len(caves) == 1: + caves = caves[0] - elif len(cave_refs) > 1: + elif len(caves) > 1: print( f" - More than one Cave {caves} in this wallet {wallet}. Not managed in this troggle release." ) @@ -576,7 +606,6 @@ def walletedit(request, path=None): ) cave_refs = list(set(cave_refs)) - caves = list(set(caves)) firstdate = None if dates: firstdate = min(dates).isoformat() @@ -613,9 +642,11 @@ def walletedit(request, path=None): # print(f'--- POST processing starts {wallet=} {path=}') if "psg" in request.POST: # handle metadata form formj = WalletForm(request.POST) - # Beware. All fields returned as strings. Must re-type them as lists etc. before using or re-saving - # Also lots of hassle with lists of strings interpreted as a single string - # Unset checkboxes do not return any value, checked ones return "True". So need initialising to False + # Beware. All fields returned as strings. So must re-type them as + # lists etc. before using or re-saving + + # Unset checkboxes do not return any value, checked ones return "True". + # So all need initialising to False if formj.is_valid(): posted = request.POST.copy() posted.pop("csrfmiddlewaretoken") # discard this @@ -625,26 +656,35 @@ def walletedit(request, path=None): # print(f'--- wd ${f}$ - {wd[f]}') for f in posted: wd[xlate[f]] = posted[f].replace("'", '"') - + print(f"'{f}' -{xlate[f]}- {posted[f]}") if posted[f] == "True": wd[xlate[f]] = True - + + newdate = make_valid_date(posted["date"]) wd["people"] = wd["people"][1:-1].replace('"', "").split(",") for i, elem in enumerate(wd["people"]): wd["people"][i] = elem.strip() + if wd["cave"]: + if wd["cave"][0] == "[": + wd["cave"] = wd["cave"][1:-1].replace('"', "").split(",") + for i, elem in enumerate(wd["cave"]): + wd["cave"][i] = elem.strip() - # print(f'--- ${wd["survex file"]}$ - {type(wd["survex file"])}') if wd["survex file"]: # allow for no survex file at all if wd["survex file"][0] == "[": wd["survex file"] = wd["survex file"][1:-1] wd["survex file"] = wd["survex file"].replace('"', "").split(",") for i, elem in enumerate(wd["survex file"]): wd["survex file"][i] = elem.strip() - # print(f'--- {wd["survex file"]} - {type(wd["survex file"])}') + save_json(wd) - # will already exist as creation does not happen here anymore + # walletobject will already exist as creation does not happen here anymore walletobject = make_wallet(wallet) + walletobject.walletdate = newdate # must be valid date + print(f"---Setting VALID new date to db {walletobject} {walletobject.walletdate}") + walletobject.save() + print(f"---Setting VALID new date to db {walletobject} {walletobject.walletdate}") commit_json(wd) else: @@ -756,38 +796,31 @@ def walletedit(request, path=None): print(message) return render(request, "errors/generic.html", {"message": message}) - if no_people(waldata["people"]): - team = [] - names = [] - refs = [] if "survex file" in waldata: date, team, caves, caverefs, wallet_refs, names = scan_survexfiles(waldata["survex file"]) - - print(f"--- date from survexfiles scan {date}") - print(f"--- date from django object {Wallet.objects.filter(walletname=wallet)[0].walletdate}") # Override the discovered values with those in the JSON file: if not waldata["date"]: # either absent or empty string waldata["date"] = date - if "people" in waldata: - people = waldata["people"] # text string - else: + if no_people(waldata["people"]): people = team waldata["people"] = team + else: + people = waldata["people"] # text string - if "cave" in waldata: + if empty_string(waldata["cave"]): + cave = caverefs # a list, potentially + waldata["cave"] = cave + else: cave = waldata["cave"] # text string - else: - cave = caves - waldata["cave"] = caves - if waldata["name"]: - psg = waldata["name"] - else: + if empty_string(waldata["name"]): psg = names waldata["name"] = names + else: + psg = waldata["name"] if "free text" in waldata: freetext = waldata["free text"] @@ -798,23 +831,9 @@ def walletedit(request, path=None): # for a in waldata: # print(f"'{waldata[a]}' {a}") # find trips and survex files of the same date - if date: - datestr = date.replace(".", "-") - try: - samedate = datetime.date.fromisoformat(datestr) - except ValueError: - # probably a single digit day number. HACKUS MAXIMUS. - # clearly we need to fix this when we first import date strings.. - datestr = datestr[:-1] + "0" + datestr[-1] - print(f" - {datestr=} ") - try: - samedate = datetime.date.fromisoformat(datestr) - except: - try: - samedate = datetime.date.fromisoformat(datestr[:10]) - except: - samedate = None - walletobject = make_wallet(wallet) + walletobject = make_wallet(wallet) + if waldata["date"]: + samedate = make_valid_date(waldata["date"]) walletobject.walletdate = samedate walletobject.save() @@ -847,7 +866,7 @@ def walletedit(request, path=None): survexsize = str(min(len(str(waldata["survex file"])), 46)) try: - thiswallet = Wallet.objects.get(walletname=wallet) + thiswallet = walletobject # Wallet.objects.get(walletname=wallet) caveifywallet(thiswallet) thiswallet.ticks = thiswallet.get_ticks() # the complaints in colour form # fixsurvextick(thiswallet, thiswallet.ticks) @@ -865,6 +884,8 @@ def walletedit(request, path=None): "dirs": dirs, "waldata": waldata, "svxfiles": svxfiles, + "survex": waldata["survex file"], + "survexsize": survexsize, "checked": checked, "trips": trips, "manywallets": [thiswallet], @@ -889,8 +910,6 @@ def walletedit(request, path=None): **context, "date": waldata["date"], #'url': waldata["description url"], 'urlsize': str(len(str(waldata["description url"]))), - "survex": waldata["survex file"], - "survexsize": survexsize, "cave": cave, "psg": psg, "freetext": freetext, diff --git a/templates/walletform.html b/templates/walletform.html index 75f3d5f..1583393 100644 --- a/templates/walletform.html +++ b/templates/walletform.html @@ -109,7 +109,6 @@ {% endif %} - {% if not create %} {% if complaints %} @@ -145,7 +144,7 @@
{% endif %} - + @@ -154,7 +153,7 @@ input {font-family: monospace; font-weight: bold; font-size: 100%; padding: 0.5em; }
-
+ {% csrf_token %} + placeholder="['caves-1623/148/148-2007.svx']" value="{{survex}}" />

{% if user.username %}