From b9fad1f4fb0fd496aedfcb439709f308cd366de8 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Wed, 28 Apr 2021 00:48:20 +0100 Subject: [PATCH] new path() interacts badly with include(). fixed --- core/TESTS/tests.py | 48 ++++++++++++++++++++++++++++++++++++++++++--- core/views/expo.py | 26 ++++++++++++++++-------- urls.py | 46 +++++++++++++++++++++++++------------------ 3 files changed, 90 insertions(+), 30 deletions(-) diff --git a/core/TESTS/tests.py b/core/TESTS/tests.py index 1cf9f60..77cd998 100644 --- a/core/TESTS/tests.py +++ b/core/TESTS/tests.py @@ -190,8 +190,22 @@ class PageTests(TestCase): content = response.content.decode() self.assertEqual(response.status_code, 302) - def test_page_expofiles_dir(self): - # Flat file tests. + def test_page_expofiles_root_dir(self): + # Root expofiles - odd interaction with url parsing so needs testing + response = self.client.get('/expofiles') + if response.status_code != 200: + self.assertEqual(response.status_code, 302) + if response.status_code != 302: + self.assertEqual(response.status_code, 200) + content = response.content.decode() + for ph in [ r'a href="/expofiles/geotiffsurveys">/geotiffsurveys/', + r'/photos/', + r'/surveyscans/' ]: + phmatch = re.search(ph, content) + self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph +"'") + + def test_page_expofiles_root_slash_dir(self): + # Root expofiles - odd interaction with url parsing so needs testing response = self.client.get('/expofiles/') if response.status_code != 200: self.assertEqual(response.status_code, 302) @@ -200,7 +214,35 @@ class PageTests(TestCase): content = response.content.decode() for ph in [ r'a href="/expofiles/geotiffsurveys">/geotiffsurveys/', r'/photos/', - r'/surveyscans' ]: + r'/surveyscans/' ]: + phmatch = re.search(ph, content) + self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph +"'") + + def test_page_expofiles_badness(self): + # should display expofiles directory contents not its parent + response = self.client.get('/expofiles/99badness99') + if response.status_code != 200: + self.assertEqual(response.status_code, 302) + if response.status_code != 302: + self.assertEqual(response.status_code, 200) + content = response.content.decode() + for ph in [ r'a href="/expofiles/geotiffsurveys">/geotiffsurveys/', + r'/photos/', + r'/surveyscans/' ]: + phmatch = re.search(ph, content) + self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph +"'") + + def test_page_expofiles_docs_dir(self): + # Flat file tests. + response = self.client.get('/expofiles/documents/') + if response.status_code != 200: + self.assertEqual(response.status_code, 302) + if response.status_code != 302: + self.assertEqual(response.status_code, 200) + content = response.content.decode() + for ph in [ r'a href="/expofiles/documents/bier-tent-instructions.pdf">bier-tent-instructions.pdf', + r'a href="/expofiles/documents/boc.pdf">boc.pdf', + r'a href="/expofiles/documents/bierbook">/bierbook' ]: phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph +"'") diff --git a/core/views/expo.py b/core/views/expo.py index 42ca8a2..a312143 100644 --- a/core/views/expo.py +++ b/core/views/expo.py @@ -48,11 +48,11 @@ default_head = ''' ''' -def expofiles_redirect(request, path): +def expofiles_redirect(request, filepath): '''This is used only when running as a test system without a local copy of /expofiles/ when settings.EXPOFILESREMOTE is True ''' - return redirect(urljoin('http://expo.survex.com/expofiles/', path)) + return redirect(urljoin('http://expo.survex.com/expofiles/', filepath)) def map(request): '''Serves unadorned the expoweb/map/map.html file @@ -70,6 +70,10 @@ def expofilessingle(request, filepath): '''sends a single binary file to the user, if not found, show the parent directory If the path actually is a directory, then show that. ''' + #print(f' - expofilessingle {filepath}') + if filepath =="" or filepath =="/": + return expofilesdir(request, settings.EXPOFILES, "") + fn=urlunquote(filepath) fn = Path(settings.EXPOFILES,filepath) if fn.is_dir(): @@ -77,21 +81,27 @@ def expofilessingle(request, filepath): if fn.is_file(): return HttpResponse(content=open(fn, "rb"),content_type=getmimetype(filepath)) # any file else: - # not a file, so show parent directory - return expofilesdir(request, Path(fn).parent, Path(filepath).parent) + # not a file, so show parent directory - DANGER need to check this is limited to below expofiles + if Path(fn).parent == Path(settings.EXPOFILES).parent: + return expofilesdir(request, Path(settings.EXPOFILES), Path(filepath).parent) + else: + return expofilesdir(request, Path(fn).parent, Path(filepath).parent) def expofilesdir(request, dirpath, filepath): '''does a directory display. If there is an index.html file we should display that. - dirpath is a full Path() resolved including lcoal machine /expofiles/ - filepath is a Path() and it does not have /expofiles/ in it ''' - # print(f' - expofilesdir {dirpath}') - urlpath = 'expofiles' / Path(filepath) + #print(f' - expofilesdir {dirpath} settings.EXPOFILESREMOTE: {settings.EXPOFILESREMOTE}') + if filepath: + urlpath = 'expofiles' / Path(filepath) + else: + urlpath = Path('expofiles') try: for f in dirpath.iterdir(): pass except FileNotFoundError: - print(f' - expofilesdir {dirpath}') + #print(f' - expofilesdir error {dirpath}') return expofilesdir(request, dirpath.parent, filepath.parent) fileitems = [] @@ -145,7 +155,7 @@ def mediapage(request, subpath=None, doc_root=None): '''This is for special prefix paths /photos/ /site_media/, /static/ etc. as defined in urls.py . If given a directory, gives a failure page. ''' - # print(" - XXXXX_ROOT: {} ...{}".format(doc_root, subpath)) + #print(" - XXXXX_ROOT: {} ...{}".format(doc_root, subpath)) if doc_root is not None: filetobeopened = Path(doc_root, subpath) if filetobeopened.is_dir(): diff --git a/urls.py b/urls.py index 23f63af..556f981 100644 --- a/urls.py +++ b/urls.py @@ -47,12 +47,14 @@ todo = '''Replace most re_path() with modern and simpler path() if settings.EXPOFILESREMOTE: expofilesurls = [ - re_path(r'^(?P.*)$', expofiles_redirect, name="expofiles_redirect"), # to http://expo.survex.com/expofiles - ] + path('/', expofiles_redirect, name="expofiles_redirect"), # to http://expo.survex.com/expofiles + path('', expofiles_redirect, {'filepath': ""}, name="expofiles_redirect"), + ] else: expofilesurls = [ - re_path(r'^(?P.*)$', expofilessingle, name="single"), # local copy of EXPOFILES - ] + path('/', expofilessingle, name="single"), # local copy of EXPOFILES + path('', expofilessingle, {'filepath': ""}, name="single"), + ] # The URLs provided by include('django.contrib.auth.urls') are: @@ -66,7 +68,8 @@ else: # accounts/reset/done/ [name='password_reset_complete'] trogglepatterns = [ - re_path(r'^expofiles/', include(expofilesurls)), + path('expofiles/', include(expofilesurls)), + path('expofiles', include(expofilesurls)), # curious interaction with the include() here, not just a slash problem. re_path(r'^caves$', caves.caveindex, name="caveindex"), re_path(r'^indxal.htm$', caves.caveindex, name="caveindex"), # ~420 hrefs to this url in expoweb files @@ -105,14 +108,14 @@ trogglepatterns = [ re_path(r'^getEntrances/(?P.*)', caves.get_entrances, name = "get_entrances"), # Cave description pages - re_path(r'^cave/new/$', caves.edit_cave, name="newcave"), + re_path(r'^newcave/$', caves.edit_cave, name="newcave"), re_path(r'^cave/3d/(?P[^/]+)$', caves.cave3d, name="cave3d"), - re_path(r'^cave/(?P[^/]+)/?$', caves.cave, name="cave"), - re_path(r'^cave/(?P[^/]+)/?(?P[^/])$', ent), # view_caves.ent - re_path(r'^cave/(?P[^/]+)/edit/$', caves.edit_cave, name="edit_cave"), re_path(r'^cave/entrance/([^/]+)/?$', caves.caveEntrance), re_path(r'^cave/description/([^/]+)/?$', caves.caveDescription), + re_path(r'^cave/(?P[^/]+)/?$', caves.cave, name="cave"), + re_path(r'^cave/(?P[^/]+)/?(?P[^/])$', ent), # view_caves.ent + re_path(r'^cave/(?P[^/]+)/edit/$', caves.edit_cave, name="edit_cave"), re_path(r'^(?P\d\d\d\d)(?P.*)$', cavepage, name="cavepage"), # shorthand /1623/264 BUT url links may break # Note that urls eg '1623/161/l/rl89a.htm' are handled by cavepage which redirects them to 'expopage' @@ -126,23 +129,26 @@ trogglepatterns = [ path('pathsreport', statistics.pathsreport, name="pathsreport"), path('dataissues', statistics.dataissues, name="dataissues"), - re_path(r'^troggle$', frontpage, name="frontpage"), # control panel. Shows recent actions. - re_path(r'^todo/(?P.*)$', todos, name="todos"), - re_path(r'^controlpanel/?$', controlpanel, name="controlpanel"), + path(r'troggle', frontpage, name="frontpage"), # control panel. Shows recent actions. + path(r'todo/', todos, name="todos"), + path(r'controlpanel', controlpanel, name="controlpanel"), # The survexfile pages + path('survexfile', survex.survexcavesingle, {'survex_cave': ''}, name="survexcavessingle"), + path('survexfile/', survex.survexcavesingle, {'survex_cave': ''}, name="survexcavessingle"), + path('survexfile/caves', survex.survexcaveslist, name="survexcaveslist"), + path('survexfile/caves/', survex.survexcaveslist, name="survexcaveslist"), # auto slash not working + path('survexfile/.svx', survex.svx, name="svx"), path('survexfile/.3d', survex.threed, name="threed"), path('survexfile/.log', survex.svxraw, name="svxraw"), path('survexfile/.err', survex.err, name="err"), + path('survexfile/', survex.survexcavesingle, name="survexcavessingle"), - path('survexfile/caves', survex.survexcaveslist, name="survexcaveslist"), - path('survexfile/caves/', survex.survexcaveslist, name="survexcaveslist"), # auto slash not working - path('survexfile/', survex.survexcavesingle, name="survexcavessingle"), - - path('survey_scans/', allwallets, name="allwallets"), - path('survey_scans//', singlewallet, name="singlewallet"), +# The survey scans in the wallets + path('survey_scans/', allwallets, name="allwallets"), + path('survey_scans//', singlewallet, name="singlewallet"), path('survey_scans//', scansingle, name="scansingle"), # The tunnel and therion drawings files pages @@ -154,7 +160,7 @@ trogglepatterns = [ re_path(r'^dwgdataraw/(?P.+?\.xml)/upload$', dwgfileupload, name="dwgfileupload"), # Not working -# QMs pages - must precede other /caves pages +# QMs pages - must precede other /caves pages? re_path(r'^cave/qms/([^/]+)/?$', caves.caveQMs), # Broken- QMs have no proper link to cave id re_path(r'^cave/(?P[^/]+)/(?P\d\d\d\d)-(?P\d*)(?P[ABCDX]?)?$', caves.qm, name="qm"), @@ -168,6 +174,8 @@ trogglepatterns = [ re_path(r'^static/(?P.*)$', mediapage, {'doc_root': settings.MEDIA_ROOT}, name="mediapage"), # STATIC is in MEDIA now! re_path(r'^javascript/(?P.*)$', mediapage, {'doc_root': settings.JSLIB_ROOT}, name="mediapage"), # JSLIB_URL re_path(r'^expowebcache/3d/(?P.*)$', mediapage, {'doc_root': settings.THREEDCACHEDIR}, name="mediapage"), + + re_path(r'^/loser/(?P.*)$', mediapage, {'doc_root': settings.SURVEX_DATA}, name="mediapage"), # Oddly not working !? re_path(r'^map/map.html', map, name="map"), # Redirects to OpenStreetMap JavaScript re_path(r'^map/(?P.*)$', mapfile, name="mapfile"), # css, js, gpx