new path() interacts badly with include(). fixed

This commit is contained in:
Philip Sargent 2021-04-28 00:48:20 +01:00
parent 5e478c7eb0
commit b9fad1f4fb
3 changed files with 90 additions and 30 deletions

View File

@ -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'<a href="/expofiles/photos">/photos/',
r'<a href="/expofiles/surveyscans">/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'<a href="/expofiles/photos">/photos/',
r'<a href="/expofiles/surveyscans">/surveyscans' ]:
r'<a href="/expofiles/surveyscans">/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'<a href="/expofiles/photos">/photos/',
r'<a href="/expofiles/surveyscans">/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 +"'")

View File

@ -48,11 +48,11 @@ default_head = '''<head>
<input type=submit value="Search"></li>
</ul>'''
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():

46
urls.py
View File

@ -47,12 +47,14 @@ todo = '''Replace most re_path() with modern and simpler path()
if settings.EXPOFILESREMOTE:
expofilesurls = [
re_path(r'^(?P<path>.*)$', expofiles_redirect, name="expofiles_redirect"), # to http://expo.survex.com/expofiles
]
path('/<path:filepath>', expofiles_redirect, name="expofiles_redirect"), # to http://expo.survex.com/expofiles
path('', expofiles_redirect, {'filepath': ""}, name="expofiles_redirect"),
]
else:
expofilesurls = [
re_path(r'^(?P<filepath>.*)$', expofilessingle, name="single"), # local copy of EXPOFILES
]
path('/<path:filepath>', 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<caveslug>.*)', 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<cave_id>[^/]+)$', caves.cave3d, name="cave3d"),
re_path(r'^cave/(?P<cave_id>[^/]+)/?$', caves.cave, name="cave"),
re_path(r'^cave/(?P<cave_id>[^/]+)/?(?P<ent_letter>[^/])$', ent), # view_caves.ent
re_path(r'^cave/(?P<slug>[^/]+)/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<cave_id>[^/]+)/?$', caves.cave, name="cave"),
re_path(r'^cave/(?P<cave_id>[^/]+)/?(?P<ent_letter>[^/])$', ent), # view_caves.ent
re_path(r'^cave/(?P<slug>[^/]+)/edit/$', caves.edit_cave, name="edit_cave"),
re_path(r'^(?P<karea>\d\d\d\d)(?P<subpath>.*)$', 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<module>.*)$', todos, name="todos"),
re_path(r'^controlpanel/?$', controlpanel, name="controlpanel"),
path(r'troggle', frontpage, name="frontpage"), # control panel. Shows recent actions.
path(r'todo/<path:module>', 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/<path:survex_file>.svx', survex.svx, name="svx"),
path('survexfile/<path:survex_file>.3d', survex.threed, name="threed"),
path('survexfile/<path:survex_file>.log', survex.svxraw, name="svxraw"),
path('survexfile/<path:survex_file>.err', survex.err, name="err"),
path('survexfile/<path:survex_cave>', 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_cave>', survex.survexcavesingle, name="survexcavessingle"),
path('survey_scans/', allwallets, name="allwallets"),
path('survey_scans/<path>/', singlewallet, name="singlewallet"),
# The survey scans in the wallets
path('survey_scans/', allwallets, name="allwallets"),
path('survey_scans/<path>/', singlewallet, name="singlewallet"),
path('survey_scans/<path>/<file>', scansingle, name="scansingle"),
# The tunnel and therion drawings files pages
@ -154,7 +160,7 @@ trogglepatterns = [
re_path(r'^dwgdataraw/(?P<path>.+?\.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<cave_id>[^/]+)/(?P<year>\d\d\d\d)-(?P<qm_id>\d*)(?P<grade>[ABCDX]?)?$', caves.qm, name="qm"),
@ -168,6 +174,8 @@ trogglepatterns = [
re_path(r'^static/(?P<subpath>.*)$', mediapage, {'doc_root': settings.MEDIA_ROOT}, name="mediapage"), # STATIC is in MEDIA now!
re_path(r'^javascript/(?P<subpath>.*)$', mediapage, {'doc_root': settings.JSLIB_ROOT}, name="mediapage"), # JSLIB_URL
re_path(r'^expowebcache/3d/(?P<subpath>.*)$', mediapage, {'doc_root': settings.THREEDCACHEDIR}, name="mediapage"),
re_path(r'^/loser/(?P<subpath>.*)$', 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<path>.*)$', mapfile, name="mapfile"), # css, js, gpx