From c88415688955041677f9951adb046a4c6008c3c2 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Sat, 30 Sep 2023 22:09:18 +0300 Subject: [PATCH] fixed duplicate paths to same page --- core/views/caves.py | 35 +++++++++++++++++++++++------------ urls.py | 8 ++++---- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/core/views/caves.py b/core/views/caves.py index a6c9659..e8db577 100644 --- a/core/views/caves.py +++ b/core/views/caves.py @@ -10,7 +10,7 @@ from pathlib import Path from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist from django.http import HttpResponse, HttpResponseNotFound, HttpResponseRedirect, FileResponse -from django.shortcuts import render +from django.shortcuts import render, redirect from django.urls import NoReverseMatch, reverse import troggle.settings as settings @@ -289,9 +289,27 @@ def rendercave(request, cave, slug, cave_id=""): request, templatefile, context ) # crashes here with NoReverseMatch if url not set up for 'edit_cave' in urls.py return r + +def cavepagefwd(request, karea=None, subpath=None): + """archaic, just send to the caves list page + """ + return redirect("/caves") - -def cavepage(request, karea=None, subpath=None, slug=None): +def caveslugfwd(request, slug): + """This is ass backwards. It would be better style to have the slug-identified request be the master, and have + other paths redirect to it, rather than what we have here. + Pending a change where we remove cave.url as a field and have an explicit fixed convention instead. + """ + if slug: + Gcavelookup = GetCaveLookup() + if slug in Gcavelookup: + cave = Gcavelookup[slug] + else: + message = f"Failed to find cave from identifier given: {slug}." + return render(request, "errors/generic.html", {"message": message}) + return redirect(f"/{cave.url}") + +def cavepage(request, karea=None, subpath=None): """Displays a cave description page accessed by kataster area number specifically OR @@ -306,16 +324,9 @@ def cavepage(request, karea=None, subpath=None, slug=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). """ - if slug: - Gcavelookup = GetCaveLookup() - if slug in Gcavelookup: - cave = Gcavelookup[slug] - return rendercave(request, cave, slug) - else: - message = f"Failed to find cave from identifier given: {slug}." - return render(request, "errors/generic.html", {"message": message}) - + # lack of validation for karea, it could be any 4 digits. + # subpath has an initial / kpath = karea + subpath # print(f" ! cavepage:'{kpath}' kataster area:'{karea}' rest of path:'{subpath}'") try: diff --git a/urls.py b/urls.py index 3ef237b..e1b9d3f 100644 --- a/urls.py +++ b/urls.py @@ -7,7 +7,7 @@ from django.conf.urls.static import static from troggle.core.views import statistics, survex from troggle.core.views.auth import expologin, expologout from troggle.core.views.caves import (cave3d, caveindex, entranceindex, - cavepage, caveQMs, edit_cave, cave_debug, + cavepage, caveslugfwd, cavepagefwd, caveQMs, edit_cave, cave_debug, edit_entrance, get_entrances, qm, expo_kml, expo_kmz) from troggle.core.views.drawings import dwgallfiles, dwgfilesingle from troggle.core.views.editor_helpers import image_selector, new_image_form @@ -142,7 +142,7 @@ trogglepatterns = [ re_path(r'^getEntrances/(?P.*)', get_entrances, name = "get_entrances"), # used internally ? # Cave description pages - path('cave/', cavepage, name="cavepage"), + path('cave/', caveslugfwd, name="caveslugfwd"), path('cave_debug', cave_debug, name="cave_debug"), re_path(r'^newcave/$', edit_cave, name="newcave"), re_path(r'^cave/3d/(?P[^/]+).3d$', cave3d, name="cave3d"), @@ -162,8 +162,8 @@ trogglepatterns = [ # 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 # These re-enable archaic URLs which are in old web pages which are still public on other servers - path('smkridge/', cavepage, {'karea': "1623/"}, name="cavepage"), - path('expo/smkridge/', cavepage, {'karea': "1623/"}, name="cavepage"), + path('smkridge/', cavepagefwd, {'karea': "1623"}, name="cavepagefwd"), + path('expo/smkridge/', cavepagefwd, {'karea': "1623"}, name="cavepagefwd"), # Archaic, kept. This /expo/ prefix only works for expoweb HTML pages not troggle pages path('expo/', expopage, name="expopage"),