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

refactored and most recent wallet link fixed

This commit is contained in:
Philip Sargent 2023-02-03 17:13:29 +00:00
parent e70d51e52b
commit c1231ff451
2 changed files with 263 additions and 258 deletions

View File

@ -315,7 +315,7 @@ def walletedit(request, path=None):
wallet = f"{year}:{wnumber:02d}"
return (None, wallet)
def identify_most_recent_wallet(wallet, y):
def identify_most_recent_wallet(wallet, currentyear):
""" Need to find the last wallet of the previous year
Has to cope with years when there are no wallets
Has to cope with weirdly named imported wallets from 1999 & earlier
@ -328,47 +328,47 @@ def walletedit(request, path=None):
try:
allwallets = Wallet.objects.all().order_by('walletname')
previous_wallet = allwallets.first()
recent_wallet = allwallets.first()
for w in allwallets:
if len(w.walletname) < 5:
continue
if w.walletname[4] != "#":
continue
continue
if w.walletname == current_name:
break
if int(w.walletyear.year) >= int(y):
if int(w.walletyear.year) >= int(currentyear):
break
previous_wallet = w
name = previous_wallet.walletname
y = name[:4]
prevy = f"{int(y)-1}"
n = name[5:]
prev = f"{int(n):02d}"
recent_wallet = w
recent_name = recent_wallet.walletname
except:
raise
prev = f"{int(n):02d}"
recent_year = recent_name[:4]
recent_number = recent_name[5:]
print(prev, prevy, y)
return prev, prevy, y
print(f"---identify_most_recent_wallet: {recent_year=} {recent_number=}")
return recent_year, recent_number
def create_nav_links(wallet):
"""Find the previous wallet and next wallet and create navigation shortcuts"""
y = wallet[:4]
n = wallet[5:]
nexty = f"{int(y)+1}"
prevy = f"{int(y)-1}"
next = f"{int(n)+1:02d}"
prev = f"{int(n)-1:02d}"
if int(n) == 0:
prev, prevy, y = identify_most_recent_wallet(wallet, y)
return next, nexty, prev, prevy, y
recent_year, recent_number = identify_most_recent_wallet(wallet, y)
prevy = recent_year # same as most recent wallet
recent_number = f"{int(recent_number):02d}"
else:
prevy = f"{int(y)-1}" # the previous year
recent_year = y # current year
recent_number = f"{int(n)-1:02d}" # previous number
nexty = f"{int(y)+1}"
next = f"{int(n)+1:02d}"
return prevy, recent_year, recent_number, y, next, nexty
def read_json():
def read_json(contents_path):
"""Read JSON from the wallet metadata file in the repo
or fills with blank data if that files can't be read
@ -476,7 +476,112 @@ def walletedit(request, path=None):
def get_logbook_trips():
return None
def no_people(team):
return (team == ["Unknown"]
or team == [""]
or team == "")
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
culling manually and only those relevant put in the JSON file"""
wallet_refs = []
dates = []
blocknames = []
team = []
try:
blocks = SurvexBlock.objects.filter(survexfile=svxfile)
for b in blocks:
print(f" - - {b=} {b.scanswallet=} {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)
except:
message = " - No associated survex blocks found for this wallet"
print(message)
return wallet_refs, dates, blocknames, team
def scan_survexfiles(survex_paths):
"""Read data from the list of survex file names attached to the wallet JSON file
NEED TO ALSO CHECK survex files which have a *ref to this wallet !
"""
cave_refs = []
wallet_refs = []
caves = []
dates = []
names = []
team = []
if not type(survex_paths) == list: # a string also is a sequence type, so do it this way
survex_paths = [survex_paths]
for svxf in survex_paths:
if not svxf: # not a blank string
continue
print(f" - - {svxf=} ")
svx = Path(svxf)
if svx.suffix.lower() != ".svx":
svx = svx.with_suffix(".svx")
f = Path(settings.SURVEX_DATA) / svx
if not f.is_file():
continue
fpath = svx.parent / svx.stem
# print(f' - {fpath=}')
try:
svxfile = SurvexFile.objects.get(path=fpath)
print(f" - {svxfile=}")
if svxfile.cave:
caves.append(svxfile.cave)
cave_refs.append(svxfile.cave.reference())
wallet_refs, dates, names, team = scan_survexblocks(svxfile)
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]
elif len(cave_refs) > 1:
print(
f" - More than one Cave {caves} in this wallet {wallet}. Not managed in this troggle release."
)
if len(names) == 1:
names = names[0]
elif len(names) > 1:
names = 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."
)
cave_refs = list(set(cave_refs))
caves = list(set(caves))
firstdate = None
if dates:
firstdate = min(dates).isoformat()
return firstdate, list(set(team)), caves, cave_refs, wallet_refs, names
checkboxes = [
"description written",
"survex not required",
@ -493,7 +598,7 @@ def walletedit(request, path=None):
redirect, wallet = preprocess_path(path)
if redirect:
return redirect
next, nexty, prev, prevy, year = create_nav_links(wallet)
prevy, recent_year, recent_number, year, next, nexty = create_nav_links(wallet)
wurl = f"/walletedit/{wallet}".replace("#", ":")
wallet = wallet.replace(":", "#")
@ -572,7 +677,7 @@ def walletedit(request, path=None):
multiple = request.FILES.getlist("uploadfiles")
fs = FileSystemStorage(os.path.join(dirpath)) # creates wallet folder if necessary
waldata = read_json()
waldata = read_json(contents_path)
actual_saved = []
if multiple:
for f in multiple:
@ -623,7 +728,7 @@ def walletedit(request, path=None):
if dirs:
dirs = sorted(dirs)
try:
waldata = read_json()
waldata = read_json(contents_path)
except:
message = f"Nasty failure in parsing wallets metadata in {contents_path}. Probably backslash not forward slash in filename path"
return render(request, "errors/generic.html", {"message": message})
@ -646,251 +751,151 @@ def walletedit(request, path=None):
trips = []
checked = {}
context = {}
if waldata: # should always be true as populated by blank data if json file doesn't exist
if 'notes not required' not in waldata:
waldata['notes not required'] = False
if not waldata: # should always be true as populated by blank data if json file doesn't exist
message = f" !! No Wallet data initialised or read ! Should not happen."
print(message)
return render(request, "errors/generic.html", {"message": message})
if (
not waldata["date"]
or not waldata["people"]
or waldata["people"] == ["Unknown"]
or waldata["people"] == [""]
or waldata["cave"] == ""
): # 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 or no cave set
# print(f'No date set')
print(f"\n - Incomplete, empty or default 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"]:
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():
fpath = svx.parent / svx.stem
# print(f' - {fpath=}')
try:
svxfile = SurvexFile.objects.get(path=fpath)
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
print(f" - {svxfile=}")
if svxfile.cave:
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:
print(f" - - {p.personname} ")
team.append(p.personname)
# else:
# print(f' - Wallet not matching *ref {b.scanswallet=} {wallet}')
except:
message = "Specified survex file not found - database may be empty, or this survex file is not *included anywhere."
# return render(request, 'errors/generic.html', {'message': message})
pass
if "people" in waldata:
people = waldata["people"] # text string
else:
people = team
waldata["people"] = team
if dates:
waldata["date"] = min(dates).isoformat()
print(f" - - {team=} ")
team = list(set(team))
waldata["people"] = team
if "cave" in waldata:
cave = waldata["cave"] # text string
else:
cave = caves
waldata["cave"] = caves
if waldata["name"]:
psg = waldata["name"]
else:
psg = names
waldata["name"] = names
if "free text" in waldata:
freetext = waldata["free text"]
if 'notes not required' not in waldata: # cope with schema change
waldata['notes not required'] = False
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(" - 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 "cave" in waldata:
cave = waldata["cave"] # text string
else:
cave = ""
if waldata["name"]:
psg = waldata["name"]
if "free text" in waldata:
freetext = waldata["free text"]
# find trips and survex files of the same date
if waldata["date"]:
datestr = waldata["date"].replace(".", "-")
# 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 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=} ")
except:
try:
samedate = datetime.date.fromisoformat(datestr)
samedate = datetime.date.fromisoformat(datestr[:10])
except:
try:
samedate = datetime.date.fromisoformat(datestr[:10])
except:
samedate = None
walletobject = make_wallet(wallet)
walletobject.walletdate = samedate
walletobject.save()
try:
thisexpo = Expedition.objects.get(year=int(year))
except: # creating a wallet for an expo that does not exist perhaps
message = f"Trying to access an Expo for '{year}' which does not exist (yet)."
message += " See /handbook/computing/newyear.html"
print(message)
return render(request, "errors/generic.html", {"message": message})
if samedate:
svxothers = SurvexBlock.objects.filter(date=samedate)
trips = LogbookEntry.objects.filter(date=samedate)
else:
svxothers = None
trips = None
samedate = None
walletobject = make_wallet(wallet)
walletobject.walletdate = samedate
walletobject.save()
try:
thisexpo = Expedition.objects.get(year=int(year))
except: # creating a wallet for an expo that does not exist perhaps
message = f"Trying to access an Expo for '{year}' which does not exist (yet)."
message += " See /handbook/computing/newyear.html"
print(message)
return render(request, "errors/generic.html", {"message": message})
if samedate:
svxothers = SurvexBlock.objects.filter(date=samedate)
trips = LogbookEntry.objects.filter(date=samedate)
else:
svxothers = None
trips = None
# Survex and survex complaints, comes from json file on disc, not as pre-populated as above
complaints, caveobject = get_complaints([], waldata, svxfiles, files, wallet, wurl)
# print(f' - {caveobject=}')
else:
svxothers = None
trips = None
for f in checkboxes:
if waldata[f]:
checked[f] = "checked"
# Survex and survex complaints, comes from json file on disc, not as pre-populated as above
complaints, caveobject = get_complaints([], waldata, svxfiles, files, wallet, wurl)
# print(f' - {caveobject=}')
survexsize = str(min(len(str(waldata["survex file"])), 46))
for f in checkboxes:
if waldata[f]:
checked[f] = "checked"
try:
thiswallet = Wallet.objects.get(walletname=wallet)
caveifywallet(thiswallet)
thiswallet.ticks = thiswallet.get_ticks() # the complaints in colour form
# fixsurvextick(thiswallet, thiswallet.ticks)
# print(f"--- {wallet} {thiswallet} walletdate={thiswallet.walletdate} immediately before form render")
except:
thiswallet = None
context = {
"year": year,
"prev": prev,
"next": next,
"prevy": prevy,
"nexty": nexty,
"files": files,
"dirs": dirs,
"waldata": waldata,
"svxfiles": svxfiles,
"checked": checked,
"trips": trips,
"manywallets": [thiswallet],
"svxothers": svxothers,
"create": create,
"metadataurl": metadataurl,
"metadata": metadata,
"complaints": complaints,
"caveobject": caveobject,
"people": waldata["people"],
"peoplesize": str(len(str(waldata["people"]))),
"filesaved": filesaved,
"actual_saved": actual_saved,
}
survexsize = str(min(len(str(waldata["survex file"])), 46))
return render(
request,
"walletform.html",
{
"form": form,
"wallet": wallet,
**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,
"psgsize": str(max(12, len(str(psg)))),
"freetextsize": str(max(60, len(str(freetext)))),
},
)
else: # no wallet data: should never happen as there should be default data in all cases
print(f"! - Should never get here ! Problem in wallet editing...")
context = {
"year": year,
"prev": prev,
"next": next,
"prevy": prevy,
"nexty": nexty,
"files": files,
"dirs": dirs,
"waldata": waldata,
"svxfiles": svxfiles,
"checked": checked,
"create": create,
"people": "",
"peoplesize": 12,
"filesaved": filesaved,
"actual_saved": actual_saved,
}
try:
thiswallet = Wallet.objects.get(walletname=wallet)
caveifywallet(thiswallet)
thiswallet.ticks = thiswallet.get_ticks() # the complaints in colour form
# fixsurvextick(thiswallet, thiswallet.ticks)
# print(f"--- {wallet} {thiswallet} walletdate={thiswallet.walletdate} immediately before form render")
except:
thiswallet = None
context = {
"year": year,
"recent_year": recent_year,
"recent_number": recent_number,
"next": next,
"prevy": prevy,
"nexty": nexty,
"files": files,
"dirs": dirs,
"waldata": waldata,
"svxfiles": svxfiles,
"checked": checked,
"trips": trips,
"manywallets": [thiswallet],
"svxothers": svxothers,
"create": create,
"metadataurl": metadataurl,
"metadata": metadata,
"complaints": complaints,
"caveobject": caveobject,
"people": people,
"peoplesize": str(len(str(people))),
"filesaved": filesaved,
"actual_saved": actual_saved,
}
return render(
request,
"walletform.html",
{
"form": form,
"wallet": wallet,
**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,
"psgsize": str(max(12, len(str(psg)))),
"freetextsize": str(max(60, len(str(freetext)))),
},
)
return render(
request,
"walletform.html",
{
"form": form,
"wallet": wallet,
**context,
"date": "",
#'url': "", 'urlsize': 12,
"survex": "",
"survexsize": 46,
"cave": cave,
"psg": psg,
"freetext": freetext,
"psgsize": 12,
"freetextsize": 20,
},
)

View File

@ -31,7 +31,7 @@
<p style="font-family: monospace; font-weight: bold; font-size: 130%; text-align: center">
<a style="font-weight: normal;" href="/walletedit/{{prevy}}:01">{{prevy}}</a>
&nbsp;...&nbsp;
<a href="/walletedit/{{year}}:{{prev}}">{{year}}:{{prev}}</a>
<a href="/walletedit/{{recent_year}}:{{recent_number}}">{{recent_year}}:{{recent_number}}</a>
&larr; {{wallet}} &rarr;
<a href="/walletedit/{{year}}:{{next}}">{{year}}:{{next}}</a>
&nbsp;...&nbsp;
@ -254,7 +254,7 @@
<em>No other survex files found for this date.</em><br>
{% endif %}
{% if metadataurl %}<span style="font-size: 70%; "><details><summary>
{% if metadataurl %}<span style="font-size: 70%; "><details open><summary>
JSON <br>
</summary>
<a href="{{metadataurl}}">{{metadataurl}}</a><br>