new method for /site-media/, /static/, /photos/

This commit is contained in:
Philip Sargent 2021-03-31 23:19:48 +01:00
parent 9d8a44696b
commit 2690203912
4 changed files with 36 additions and 31 deletions

View File

@ -90,7 +90,23 @@ def expowebpage(request, expowebpath, path):
has_menu = True has_menu = True
return render(request, 'flatpage.html', {'editable': editable, 'path': path, 'title': title, return render(request, 'flatpage.html', {'editable': editable, 'path': path, 'title': title,
'body': body, 'homepage': (path == "index.htm"), 'has_menu': has_menu}) 'body': body, 'homepage': (path == "index.htm"), 'has_menu': has_menu})
def mediapage(request, subpath=None, doc_root=None):
'''This is for special prefixe 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))
if doc_root is not None:
filetobeopened = Path(doc_root, subpath)
if filetobeopened.is_dir():
return render(request, 'nodirlist.html', {'path': subpath})
try:
return HttpResponse(content=open(filetobeopened, "rb"), content_type=getmimetype(subpath))
except IOError:
return render(request, 'pagenotfound.html', {'path': subpath})
else:
return render(request, 'pagenotfound.html', {'path': subpath})
def expopage(request, path): def expopage(request, path):
'''Either renders an HTML page from expoweb with all the menus, '''Either renders an HTML page from expoweb with all the menus,
@ -128,17 +144,9 @@ def expopage(request, path):
# the final / may have been appended by middleware if there was no page without it # the final / may have been appended by middleware if there was no page without it
# do not redirect to a file path without the slash as we may get in a loop. Let the user fix it: # do not redirect to a file path without the slash as we may get in a loop. Let the user fix it:
return render(request, 'dirnotfound.html', {'path': path, 'subpath': path[0:-1]}) return render(request, 'dirnotfound.html', {'path': path, 'subpath': path[0:-1]})
if path.startswith('site_media'): # BUT we may have missing files, directories or .html here too?! # So it must be a file in /expoweb/ but not .htm or .html probably an image
# print(" - MEDIA_ROOT: {} ...{}".format(settings.MEDIA_ROOT, path)) filetobeopened = os.path.normpath(expowebpath / path)
npath = path.replace("site_media", settings.MEDIA_ROOT)
filetobeopened = os.path.normpath(npath)
elif path.startswith("static"):
# print(" - STATIC_ROOT: {} ...{}".format(settings.MEDIA_ROOT, path))
npath = path.replace("static", settings.MEDIA_ROOT)
filetobeopened = os.path.normpath(npath)
else:
filetobeopened = os.path.normpath(expowebpath / path)
try: try:
return HttpResponse(content=open(filetobeopened, "rb"), content_type=getmimetype(path)) return HttpResponse(content=open(filetobeopened, "rb"), content_type=getmimetype(path))

View File

@ -41,6 +41,11 @@ TROGGLE_PATH = Path(__file__).parent
TEMPLATE_PATH = os.fspath(TROGGLE_PATH / 'templates') TEMPLATE_PATH = os.fspath(TROGGLE_PATH / 'templates')
MEDIA_ROOT = os.fspath(TROGGLE_PATH / 'media') MEDIA_ROOT = os.fspath(TROGGLE_PATH / 'media')
FILES = Path('/mnt/f/expofiles/')
EXPOFILES = Path('/mnt/f/expofiles/')
SURVEY_SCANS = EXPOFILES / 'surveyscans'
PHOTOS_ROOT = EXPOFILES / 'photos'
# URL that handles the media served from MEDIA_ROOT. Make sure to use a # URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases). # trailing slash if there is a path component (optional in other cases).
MEDIA_URL = '/site-media/' MEDIA_URL = '/site-media/'
@ -129,21 +134,16 @@ EMAIL_PORT=587
EMAIL_USE_TLS = True EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'django-test@klebos.net' DEFAULT_FROM_EMAIL = 'django-test@klebos.net'
SURVEX_DATA = REPOS_ROOT_PATH / "loser" SURVEX_DATA = REPOS_ROOT_PATH / "loser"
TUNNEL_DATA = REPOS_ROOT_PATH / "drawings" TUNNEL_DATA = REPOS_ROOT_PATH / "drawings"
THREEDCACHEDIR = REPOS_ROOT_PATH / 'expowebcache' / '3d' THREEDCACHEDIR = REPOS_ROOT_PATH / 'expowebcache' / '3d'
EXPOWEB = REPOS_ROOT_PATH / "expoweb" EXPOWEB = REPOS_ROOT_PATH / "expoweb"
SURVEYS = REPOS_ROOT_PATH SURVEYS = REPOS_ROOT_PATH
SURVEY_SCANS = '/mnt/f/expofiles/surveyscans/'
FILES = '/mnt/f/expofiles/'
CAVEDESCRIPTIONS = EXPOWEB / "cave_data" CAVEDESCRIPTIONS = EXPOWEB / "cave_data"
ENTRANCEDESCRIPTIONS = EXPOWEB / "entrance_data" ENTRANCEDESCRIPTIONS = EXPOWEB / "entrance_data"
EXPOWEB_URL = '' EXPOWEB_URL = ''
SURVEYS_URL = '/survey_scans/' SURVEYS_URL = '/survey_scans/'
EXPOFILES ='/mnt/f/expofiles/'
# Sanitise these to be strings as all other code is expecting strings # Sanitise these to be strings as all other code is expecting strings
# and we have not made the change to pathlib Path type in the other localsettings-* variants yet. # and we have not made the change to pathlib Path type in the other localsettings-* variants yet.

7
templates/nodirlist.html Normal file
View File

@ -0,0 +1,7 @@
{% extends "expobase.html" %}
{% block title %}Page not found {{ path }}{% endblock %}
{% block body %}
<h1>Directory not displayed: {{ path }}</h1>
<p>Only individual files may be displayed from this directory. No directory listing available.
{% include "menu.html" %}
{% endblock %}

18
urls.py
View File

@ -10,7 +10,7 @@ from troggle.core.views import surveys, logbooks, other, caves, statistics, surv
from troggle.core.views.other import troggle404, frontpage from troggle.core.views.other import troggle404, frontpage
from troggle.core.views.caves import ent, prospecting_image from troggle.core.views.caves import ent, prospecting_image
from troggle.core.views.statistics import pathsreport, stats from troggle.core.views.statistics import pathsreport, stats
from troggle.core.views.expo import expofiles_redirect, expofilessingle, expopage, editexpopage from troggle.core.views.expo import expofiles_redirect, expofilessingle, expopage, editexpopage, mediapage
from troggle.core.views.survex import survexcaveslist, survexcavesingle, svx from troggle.core.views.survex import survexcaveslist, survexcavesingle, svx
"""This sets the actualurlpatterns[] and urlpatterns[] lists which django uses """This sets the actualurlpatterns[] and urlpatterns[] lists which django uses
to resolve urls - in both directions as these are declarative. to resolve urls - in both directions as these are declarative.
@ -40,7 +40,6 @@ else:
expofilesurls = [ expofilesurls = [
url(r'^(?P<filepath>.*)$', expofilessingle, name="single"), # local copy of EXPOFILES url(r'^(?P<filepath>.*)$', expofilessingle, name="single"), # local copy of EXPOFILES
] ]
trogglepatterns = [ trogglepatterns = [
url(r'^expofiles/', include(expofilesurls)), url(r'^expofiles/', include(expofilesurls)),
@ -134,19 +133,10 @@ trogglepatterns = [
url(r'^prospecting/(?P<name>[^.]+).png$', prospecting_image, name="prospecting_image"), url(r'^prospecting/(?P<name>[^.]+).png$', prospecting_image, name="prospecting_image"),
# url(r'^javascript/(?P<filepath>.*)$', surveys.expofilessingle, name="single"), # JSLIB_URL - Apache: Alias /javascript /usr/share/javascript url(r'^photos/(?P<subpath>.*)$', mediapage, {'doc_root': settings.PHOTOS_ROOT}, name="mediapage"), # photo galleries
url(r'^site_media/(?P<subpath>.*)$', mediapage, {'doc_root': settings.MEDIA_ROOT}, name="mediapage"), # MEDIA_ROOT: CSS and JS
url(r'^static/(?P<subpath>.*)$', mediapage, {'doc_root': settings.STATIC_ROOT}, name="mediapage"), # STATIC: CSS and JS
# static views not working, removed as a plugin. Use apache instead to serve these:
# url(r'^photos/(?P<path>.*)$', staticviews.serve,
# {'document_root': settings.PHOTOS_ROOT, 'show_indexes':True}),
# url(r'^gallery/(?P<path>.*)$', staticviews.serve,
# {'document_root': settings.PHOTOS_ROOT, 'show_indexes':True}),
# url(r'^site_media/(?P<filepath>.*)$', surveys.expofilessingle, name="single"), # MEDIA_ROOT: CSS and JS
url(r'^(site_media/.*)$', expopage, name="expopage"), # MEDIA_ROOT: CSS and JS
# url(r'^static/(?P<filepath>.*)$', surveys.expofilessingle, name="single"), # MEDIA_ROOT: CSS and JS
url(r'^(static/.*)$', expopage, name="expopage"), # STATIC: CSS and JS
url(r'^(.*)_edit$', editexpopage, name="editexpopage"), url(r'^(.*)_edit$', editexpopage, name="editexpopage"),
url(r'^(.*)$', expopage, name="expopage"), # CATCHALL assumed relative to EXPOWEB url(r'^(.*)$', expopage, name="expopage"), # CATCHALL assumed relative to EXPOWEB