2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2024-11-25 08:41:51 +00:00

Many many bugs fixed for >1 svx file on a wallet

This commit is contained in:
Philip Sargent 2023-02-03 22:19:51 +00:00
parent c1231ff451
commit 175e71facf
4 changed files with 100 additions and 82 deletions

View File

@ -32,7 +32,8 @@ class Wallet(models.Model):
return urljoin(settings.URL_ROOT, reverse("singlewallet", kwargs={"path": re.sub("#", "%23", self.walletname)})) return urljoin(settings.URL_ROOT, reverse("singlewallet", kwargs={"path": re.sub("#", "%23", self.walletname)}))
def get_json(self): 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') # jsonfile = Path(self.fpath, 'contents.json')
# Get from git repo instead # Get from git repo instead
@ -102,9 +103,8 @@ class Wallet(models.Model):
"""Reads all the JSON data just to get the JSON date.""" """Reads all the JSON data just to get the JSON date."""
if self.walletdate: if self.walletdate:
return self.walletdate return self.walletdate
if not self.get_json(): if not (jsondata := self.get_json()): # WALRUS
return None return None
jsondata = self.get_json() # use walrus operator?
datestr = jsondata["date"] datestr = jsondata["date"]
if not datestr: if not datestr:
@ -259,7 +259,7 @@ class Wallet(models.Model):
ticks["N"] = "red" ticks["N"] = "red"
else: else:
ticks["N"] = "green" 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 drawing required
plan_scanned = reduce(operator.or_, [f.startswith("plan") for f in files], False) plan_scanned = reduce(operator.or_, [f.startswith("plan") for f in files], False)

View File

@ -132,7 +132,7 @@ def walletslistperson(request, first_name, last_name):
fixsurvextick(w, w.ticks) fixsurvextick(w, w.ticks)
return manywallets return manywallets
print("-walletslistperson") # print("-walletslistperson")
try: try:
if last_name: if last_name:
@ -185,7 +185,7 @@ def walletslistyear(request, year):
return manywallets return manywallets
print("-walletslistyear") # print("-walletslistyear")
if year < 1976 or year > 2050: if year < 1976 or year > 2050:
return render(request, "errors/generic.html", {"message": "Year out of range. Must be between 1976 and 2050"}) return render(request, "errors/generic.html", {"message": "Year out of range. Must be between 1976 and 2050"})

View File

@ -347,7 +347,7 @@ def walletedit(request, path=None):
recent_year = recent_name[:4] recent_year = recent_name[:4]
recent_number = recent_name[5:] 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 return recent_year, recent_number
def create_nav_links(wallet): def create_nav_links(wallet):
@ -481,7 +481,34 @@ def walletedit(request, path=None):
return (team == ["Unknown"] return (team == ["Unknown"]
or team == [""] or team == [""]
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): def scan_survexblocks(svxfile):
"""Scans for *team people attached to all the survex blocks in this 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 This could be rather a lot for some compedious survex files! So would need
@ -493,26 +520,25 @@ def walletedit(request, path=None):
try: try:
blocks = SurvexBlock.objects.filter(survexfile=svxfile) blocks = SurvexBlock.objects.filter(survexfile=svxfile)
for b in blocks: for b in blocks:
print(f" - - {b=} {b.scanswallet=} {b.date=}") # print(f" - - - - {b=} {b.scanswallet.walletname} {b.date=} ")
if b.scanswallet: if b.scanswallet:
wallet_refs.append(b.scanswallet) # other wallets wallet_refs.append(b.scanswallet) # other wallets
if b.scanswallet.walletname == wallet: #if b.scanswallet.walletname == wallet: # only if we assume *ref all correct!
if b.date: if b.date:
dates.append(b.date) dates.append(b.date)
if b.name != b.title: if b.name != b.title:
blocknames.append(str(b.name) + "|" + str(b.title)) blocknames.append(str(b.name) + "|" + str(b.title))
else: else:
blocknames.append(str(b.name)) blocknames.append(str(b.name))
# we can use the people, across all blocks that have this *ref QSpeople = SurvexPersonRole.objects.filter(survexblock=b)
QSpeople = SurvexPersonRole.objects.filter(survexblock=b) # print(f" - - {QSpeople=}")
# print(f" - - {QSpeople=}") for p in QSpeople:
for p in QSpeople: # print(f" - - {p.personname} ")
print(f" - - {p.personname} ") team.append(p.personname)
team.append(p.personname)
except: except:
message = " - No associated survex blocks found for this wallet" message = " - No associated survex blocks found for this wallet"
print(message) print(message)
# print(f" - - - ", wallet_refs, dates, blocknames, team)
return wallet_refs, dates, blocknames, team return wallet_refs, dates, blocknames, team
def scan_survexfiles(survex_paths): def scan_survexfiles(survex_paths):
@ -534,7 +560,7 @@ def walletedit(request, path=None):
if not svxf: # not a blank string if not svxf: # not a blank string
continue continue
print(f" - - {svxf=} ") # print(f" - - {svxf=} ")
svx = Path(svxf) svx = Path(svxf)
if svx.suffix.lower() != ".svx": if svx.suffix.lower() != ".svx":
svx = svx.with_suffix(".svx") svx = svx.with_suffix(".svx")
@ -547,22 +573,26 @@ def walletedit(request, path=None):
try: try:
svxfile = SurvexFile.objects.get(path=fpath) svxfile = SurvexFile.objects.get(path=fpath)
print(f" - {svxfile=}")
if svxfile.cave: if svxfile.cave:
caves.append(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: except:
message = "Specified survex file not found - database may be empty." message = "Specified survex file not found - database may be empty."
print(message) print(message)
# This failure will also get picked up by the "S" colour code red or orange # This failure will also get picked up by the "S" colour code red or orange
caves = list(set(caves))
if len(cave_refs) == 1: if len(caves) == 1:
caves = cave_refs[0] caves = caves[0]
elif len(cave_refs) > 1: elif len(caves) > 1:
print( print(
f" - More than one Cave {caves} in this wallet {wallet}. Not managed in this troggle release." 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)) cave_refs = list(set(cave_refs))
caves = list(set(caves))
firstdate = None firstdate = None
if dates: if dates:
firstdate = min(dates).isoformat() firstdate = min(dates).isoformat()
@ -613,9 +642,11 @@ def walletedit(request, path=None):
# print(f'--- POST processing starts {wallet=} {path=}') # print(f'--- POST processing starts {wallet=} {path=}')
if "psg" in request.POST: # handle metadata form if "psg" in request.POST: # handle metadata form
formj = WalletForm(request.POST) formj = WalletForm(request.POST)
# Beware. All fields returned as strings. Must re-type them as lists etc. before using or re-saving # Beware. All fields returned as strings. So must re-type them as
# Also lots of hassle with lists of strings interpreted as a single string # lists etc. before using or re-saving
# Unset checkboxes do not return any value, checked ones return "True". So need initialising to False
# Unset checkboxes do not return any value, checked ones return "True".
# So all need initialising to False
if formj.is_valid(): if formj.is_valid():
posted = request.POST.copy() posted = request.POST.copy()
posted.pop("csrfmiddlewaretoken") # discard this posted.pop("csrfmiddlewaretoken") # discard this
@ -625,26 +656,35 @@ def walletedit(request, path=None):
# print(f'--- wd ${f}$ - {wd[f]}') # print(f'--- wd ${f}$ - {wd[f]}')
for f in posted: for f in posted:
wd[xlate[f]] = posted[f].replace("'", '"') wd[xlate[f]] = posted[f].replace("'", '"')
print(f"'{f}' -{xlate[f]}- {posted[f]}")
if posted[f] == "True": if posted[f] == "True":
wd[xlate[f]] = True wd[xlate[f]] = True
newdate = make_valid_date(posted["date"])
wd["people"] = wd["people"][1:-1].replace('"', "").split(",") wd["people"] = wd["people"][1:-1].replace('"', "").split(",")
for i, elem in enumerate(wd["people"]): for i, elem in enumerate(wd["people"]):
wd["people"][i] = elem.strip() 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"]: # allow for no survex file at all
if wd["survex file"][0] == "[": if wd["survex file"][0] == "[":
wd["survex file"] = wd["survex file"][1:-1] wd["survex file"] = wd["survex file"][1:-1]
wd["survex file"] = wd["survex file"].replace('"', "").split(",") wd["survex file"] = wd["survex file"].replace('"', "").split(",")
for i, elem in enumerate(wd["survex file"]): for i, elem in enumerate(wd["survex file"]):
wd["survex file"][i] = elem.strip() wd["survex file"][i] = elem.strip()
# print(f'--- {wd["survex file"]} - {type(wd["survex file"])}')
save_json(wd) 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 = 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) commit_json(wd)
else: else:
@ -756,38 +796,31 @@ def walletedit(request, path=None):
print(message) print(message)
return render(request, "errors/generic.html", {"message": message}) return render(request, "errors/generic.html", {"message": message})
if no_people(waldata["people"]):
team = []
names = []
refs = [] refs = []
if "survex file" in waldata: if "survex file" in waldata:
date, team, caves, caverefs, wallet_refs, names = scan_survexfiles(waldata["survex file"]) 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: # Override the discovered values with those in the JSON file:
if not waldata["date"]: # either absent or empty string if not waldata["date"]: # either absent or empty string
waldata["date"] = date waldata["date"] = date
if "people" in waldata: if no_people(waldata["people"]):
people = waldata["people"] # text string
else:
people = team people = team
waldata["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 cave = waldata["cave"] # text string
else:
cave = caves
waldata["cave"] = caves
if waldata["name"]: if empty_string(waldata["name"]):
psg = waldata["name"]
else:
psg = names psg = names
waldata["name"] = names waldata["name"] = names
else:
psg = waldata["name"]
if "free text" in waldata: if "free text" in waldata:
freetext = waldata["free text"] freetext = waldata["free text"]
@ -798,23 +831,9 @@ def walletedit(request, path=None):
# for a in waldata: # for a in waldata:
# print(f"'{waldata[a]}' {a}") # print(f"'{waldata[a]}' {a}")
# find trips and survex files of the same date # find trips and survex files of the same date
if date: walletobject = make_wallet(wallet)
datestr = date.replace(".", "-") if waldata["date"]:
try: samedate = make_valid_date(waldata["date"])
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.walletdate = samedate walletobject.walletdate = samedate
walletobject.save() walletobject.save()
@ -847,7 +866,7 @@ def walletedit(request, path=None):
survexsize = str(min(len(str(waldata["survex file"])), 46)) survexsize = str(min(len(str(waldata["survex file"])), 46))
try: try:
thiswallet = Wallet.objects.get(walletname=wallet) thiswallet = walletobject # Wallet.objects.get(walletname=wallet)
caveifywallet(thiswallet) caveifywallet(thiswallet)
thiswallet.ticks = thiswallet.get_ticks() # the complaints in colour form thiswallet.ticks = thiswallet.get_ticks() # the complaints in colour form
# fixsurvextick(thiswallet, thiswallet.ticks) # fixsurvextick(thiswallet, thiswallet.ticks)
@ -865,6 +884,8 @@ def walletedit(request, path=None):
"dirs": dirs, "dirs": dirs,
"waldata": waldata, "waldata": waldata,
"svxfiles": svxfiles, "svxfiles": svxfiles,
"survex": waldata["survex file"],
"survexsize": survexsize,
"checked": checked, "checked": checked,
"trips": trips, "trips": trips,
"manywallets": [thiswallet], "manywallets": [thiswallet],
@ -889,8 +910,6 @@ def walletedit(request, path=None):
**context, **context,
"date": waldata["date"], "date": waldata["date"],
#'url': waldata["description url"], 'urlsize': str(len(str(waldata["description url"]))), #'url': waldata["description url"], 'urlsize': str(len(str(waldata["description url"]))),
"survex": waldata["survex file"],
"survexsize": survexsize,
"cave": cave, "cave": cave,
"psg": psg, "psg": psg,
"freetext": freetext, "freetext": freetext,

View File

@ -109,7 +109,6 @@
{% endif %} {% endif %}
</em> </em>
{% if not create %} {% if not create %}
{% if complaints %} {% if complaints %}
@ -145,7 +144,7 @@
<br> <br>
{% endif %} {% endif %}
</span> </span>
<span style="font-family: monospace; font-size: 130%; "> <span style="font-family: monospace; font-size: 130%; ">
@ -154,7 +153,7 @@
input {font-family: monospace; font-weight: bold; font-size: 100%; padding: 0.5em; } input {font-family: monospace; font-weight: bold; font-size: 100%; padding: 0.5em; }
</style> </style>
<div style = "max-width:100%; margin-left:15%; font-family: monospace; font-weight: bold; font-size: 150%; text-align: right; " > <div style = "max-width:100%; margin-left:15%; font-family: monospace; font-weight: bold; font-size: 150%; text-align: right; " >
<form method ='post'> <form method ='post'>
{% csrf_token %} {% csrf_token %}
<label for="date">Date of the survey trip</label> <label for="date">Date of the survey trip</label>
<input {% if not user.username %} disabled{% endif %} <input {% if not user.username %} disabled{% endif %}
@ -221,7 +220,7 @@
<input {% if not user.username %} disabled{% endif %} <input {% if not user.username %} disabled{% endif %}
label = "survex" name = "survex" size ="{{survexsize}}" label = "survex" name = "survex" size ="{{survexsize}}"
title="List of survex files using this data" title="List of survex files using this data"
placeholder="caves-1623/264/side_balkon/siriuscyberlift.svx" value="{{survex}}" /> placeholder="['caves-1623/148/148-2007.svx']" value="{{survex}}" />
<br><br>{% if user.username %} <br><br>{% if user.username %}
<button class="fancybutton" style="padding: 0.5em 25px; margin-left: 155px; font-size: 90%; " <button class="fancybutton" style="padding: 0.5em 25px; margin-left: 155px; font-size: 90%; "
type = "submit" value = "Edit" > type = "submit" value = "Edit" >
@ -254,7 +253,7 @@
<em>No other survex files found for this date.</em><br> <em>No other survex files found for this date.</em><br>
{% endif %} {% endif %}
{% if metadataurl %}<span style="font-size: 70%; "><details open><summary> {% if metadataurl %}<span style="font-size: 70%; "><details><summary>
JSON <br> JSON <br>
</summary> </summary>
<a href="{{metadataurl}}">{{metadataurl}}</a><br> <a href="{{metadataurl}}">{{metadataurl}}</a><br>