From d2ee32d3e651c4fd9c06bc962ba1e2e9653c3fae Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Sun, 12 Nov 2023 01:23:20 +0200 Subject: [PATCH] fixing side effects of the new cave url scheme --- core/views/caves.py | 71 ++++++++++++++++++++++++++++----------------- urls.py | 2 +- 2 files changed, 46 insertions(+), 27 deletions(-) diff --git a/core/views/caves.py b/core/views/caves.py index 3884a1a..ae9cef7 100644 --- a/core/views/caves.py +++ b/core/views/caves.py @@ -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 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 / kpath = karea + subpath - # print(f" ! cavepage:'{kpath}' kataster area:'{karea}' rest of path:'{subpath}'") - try: - cave = Cave.objects.get(url=kpath) # ideally this will be unique - except Cave.DoesNotExist: - # probably a link to text or an image e.g. 1623/161/l/rl89a.htm i.e. an expoweb page - # cannot assume that this is a simple cave page, for a cave we don't know. - # 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}) + #print(f" ! cavepage:'{kpath}' kataster area:'{karea}' rest of path:'{subpath}'") + + caves = Cave.objects.filter(url=kpath) + if len(caves) == 1: + cave = caves[0] + return rendercave(request, cave, cave.slug()) - try: - r = rendercave(request, cave, cave.slug()) - return r - except NoReverseMatch: - if settings.DEBUG: - raise + + # re do all this using pathlib functions + parts = subpath.strip("/").split("/") + if len(parts) > 5: + # recursive loop. break out of it. + 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: - message = f"Failed to render cave: {kpath} (it does exist and is unique) because of a Django URL resolution error. Check urls.py." - return render(request, "errors/generic.html", {"message": message}) - except: - # anything else is a new problem. Add in specific error messages here as we discover new types of error - raise + return redirect(f"/caves") + + # BUGGER the real problem is the the cave descrit has embedded in it images like + # src="110/entrance.jpeg and since the cave url is now /1623/110/110.html + # 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 def edit_cave(request, path="", slug=None): diff --git a/urls.py b/urls.py index 5283849..a78167b 100644 --- a/urls.py +++ b/urls.py @@ -160,7 +160,7 @@ trogglepatterns = [ re_path(r'^(?P.*)/(?P[^/]+)_entrance_new$', edit_entrance, name = "newentrance"), # new entrance for a cave re_path(r'^(.*)_edit$', editexpopage, name="editexpopage"), - re_path(r'^(?P\d\d\d\d)(?P.*)$', cavepage, name="cavepage"), # shorthand /1623/264 or 1623/161/top.htm + re_path(r'^(?P162\d)(?P.*)$', 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 _edit$ for a cave description page in a subfolder e.g. /1623/204/204.html_edit gets caught here and breaks with 404