2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2024-11-22 07:11:52 +00:00

fixing side effects of the new cave url scheme

This commit is contained in:
Philip Sargent 2023-11-12 01:23:20 +02:00
parent 699c19245a
commit d2ee32d3e6
2 changed files with 46 additions and 27 deletions

View File

@ -347,36 +347,55 @@ def cavepage(request, karea=None, subpath=None):
There are also A LOT OF URLS to e.g. /1623/161/l/rl89a.htm which are IMAGES and real html files There are also A LOT OF URLS to e.g. /1623/161/l/rl89a.htm which are IMAGES and real html files
in cave descriptions. These need to be handled HERE too (accident of history). in cave descriptions. These need to be handled HERE too (accident of history).
""" """
# lack of validation for karea, it could be any 4 digits. # lack of validation for karea, 162x
# subpath has an initial / # subpath has an initial /
kpath = karea + subpath kpath = karea + subpath
# print(f" ! cavepage:'{kpath}' kataster area:'{karea}' rest of path:'{subpath}'") #print(f" ! cavepage:'{kpath}' kataster area:'{karea}' rest of path:'{subpath}'")
try:
cave = Cave.objects.get(url=kpath) # ideally this will be unique caves = Cave.objects.filter(url=kpath)
except Cave.DoesNotExist: if len(caves) == 1:
# probably a link to text or an image e.g. 1623/161/l/rl89a.htm i.e. an expoweb page cave = caves[0]
# cannot assume that this is a simple cave page, for a cave we don't know. return rendercave(request, cave, cave.slug())
# print(f" ! cavepage: url={kpath} A cave of this name does not exist")
return expo.expopage(request, kpath)
except Cave.MultipleObjectsReturned:
caves = Cave.objects.filter(url=kpath)
# print(f" ! cavepage: url={kpath} multiple caves exist")
# we should have a -several variant for the cave pages, not just the svxcaves:
return render(request, "svxcaveseveral.html", {"settings": settings, "caves": caves})
try:
r = rendercave(request, cave, cave.slug()) # re do all this using pathlib functions
return r parts = subpath.strip("/").split("/")
except NoReverseMatch: if len(parts) > 5:
if settings.DEBUG: # recursive loop. break out of it.
raise subparts = parts[0].split(".")
caveid = subparts[0]
slug = f"{karea}-{caveid}"
caves = Cave.objects.filter(caveslug__slug=slug)
if len(caves) == 1:
cave = caves[0]
return redirect(f"/{cave.url}")
else: else:
message = f"Failed to render cave: {kpath} (it does exist and is unique) because of a Django URL resolution error. Check urls.py." return redirect(f"/caves")
return render(request, "errors/generic.html", {"message": message})
except: # BUGGER the real problem is the the cave descrit has embedded in it images like
# anything else is a new problem. Add in specific error messages here as we discover new types of error # src="110/entrance.jpeg and since the cave url is now /1623/110/110.html
raise # the images try to load from /1623/110/110/entrance.jpeg and of course fail.
# THIS IS A HORRIBLE HACK
if len(parts) == 1: # simple filename, no folders in path, need to insert caveid
subparts = parts[0].split(".")
caveid = subparts[0]
k2path = karea +"/"+ caveid + subpath
return redirect(f"/{k2path}") # infinite loop
elif len(parts) >2:
if parts[0] == parts[1]: # double caveid
epath = karea
for i in parts[1:]:
epath += "/" + i
#print(f"{subpath=}\n {epath=}")
return expo.expopage(request, epath)
# if either the first two parts are not /caveid/caveid/
# or the number of parts == 2,
# print(f"2 {subpath=}")
epath = karea + "/" + subpath
return expo.expopage(request, epath)
@login_required_if_public @login_required_if_public
def edit_cave(request, path="", slug=None): def edit_cave(request, path="", slug=None):

View File

@ -160,7 +160,7 @@ trogglepatterns = [
re_path(r'^(?P<path>.*)/(?P<caveslug>[^/]+)_entrance_new$', edit_entrance, name = "newentrance"), # new entrance for a cave re_path(r'^(?P<path>.*)/(?P<caveslug>[^/]+)_entrance_new$', edit_entrance, name = "newentrance"), # new entrance for a cave
re_path(r'^(.*)_edit$', editexpopage, name="editexpopage"), re_path(r'^(.*)_edit$', editexpopage, name="editexpopage"),
re_path(r'^(?P<karea>\d\d\d\d)(?P<subpath>.*)$', cavepage, name="cavepage"), # shorthand /1623/264 or 1623/161/top.htm re_path(r'^(?P<karea>162\d)(?P<subpath>.*)$', cavepage, name="cavepage"), # shorthand /1623/264 or 1623/161/top.htm
# Note that urls eg '/1623/161/l/rl89a.htm' are handled by cavepage which redirects them to 'expopage' # Note that urls eg '/1623/161/l/rl89a.htm' are handled by cavepage which redirects them to 'expopage'
# Note that _edit$ for a cave description page in a subfolder e.g. /1623/204/204.html_edit gets caught here and breaks with 404 # Note that _edit$ for a cave description page in a subfolder e.g. /1623/204/204.html_edit gets caught here and breaks with 404