2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2025-12-18 12:07:12 +00:00

Attempt at append_slash, and backtrack.

This commit is contained in:
2025-02-18 19:59:12 +02:00
parent 95190324fb
commit cc06e2e1f4
8 changed files with 73 additions and 53 deletions

View File

@@ -1,6 +1,6 @@
from django.conf import settings
from troggle.core.models.troggle import Expedition
# from troggle.core.models.troggle import Expedition
"""This is the only troggle-specific 'context processor' that troggle uses
in the processing of Django templates

View File

@@ -1,31 +1,48 @@
import pathlib
from django import http
from django.conf import settings
from django.urls import Resolver404, resolve
from django.utils.deprecation import MiddlewareMixin
from troggle import settings
"""Non-standard django middleware is loaded from this file.
"""
todo = """SmartAppendSlashMiddleware(object) Not Working.
It needs re-writing to be compatible with Django v2.0 and later
It needs re-writing. Can we make this work even though we have a catchall url rule ?
"""
class SmartAppendSlashMiddleware(object):
class TroggleAppendSlashMiddleware(MiddlewareMixin):
"""
"SmartAppendSlash" middleware for taking care of URL rewriting.
This middleware appends a missing slash, if:
* the SMART_APPEND_SLASH setting is True
* the URL without the slash does not exist
* the URL with an appended slash does exist.
* the URL without the slash does not exist in urls.py
* the URL with an appended slash does exist in urls.py
Otherwise it won't touch the URL.
MODIFICATION
Since we have a universal catchall url pattern in urls.py, the usual way this works
won't ever trigger adding a slash. So we check for the existence of a file in expoweb,
not the existence of a pattern in urls.py...
but site_media..
but css etc....
CONCLUSION
This technique "works" but would be a maintence nightmare, so DO NOT USE IT
do NOT include
troggle.core.middleware.TroggleAppendSlashMiddleware
in settings.py
"""
def process_request(self, request):
"""Called for every url so return as quickly as possible
Append a slash if SMART_APPEND_SLASH is set, the resulting URL resolves and it doesn't without the /
Append a slash if TROGGLE_APPEND_SLASH is set, the resulting URL resolves and it doesn't without the /
"""
if not settings.SMART_APPEND_SLASH:
if not settings.TROGGLE_APPEND_SLASH:
return None
if request.path.endswith("/"):
@@ -33,16 +50,31 @@ class SmartAppendSlashMiddleware(object):
if request.path.endswith("_edit"):
return None
if request.path.startswith("/"):
relative_path = request.path[1:]
else:
relative_path = request.path
for root in [settings.MEDIA_ROOT, settings.JSLIB_ROOT, settings.EXPOFILES, settings.SCANS_ROOT, settings.PHOTOS_ROOT]:
full_path = root / relative_path
print(f"+++++ MIDDLEWARE checking {root} / {relative_path} ")
if full_path.is_file():
print(f"+++++ MIDDLEWARE It IS a {root} file {full_path=} so use it as-is.")
return None
else:
print(f"+++++ MIDDLEWARE NOT a {root}file {full_path=}")
host = http.HttpRequest.get_host(request)
old_url = [host, request.path]
if _resolves(old_url[1]):
return None
# if _resolves(old_url[1]):
# return None
# So: it does not resolve according to our criteria, i.e. _edit doesn't count
# So: it does not resolve according to our criteria, i.e. _edit doesn't count, and URL resolves doesn't count because of the catch all
new_url = old_url[:]
new_url[1] = new_url[1] + "/"
if not _resolves(new_url[1]):
print(f"+++++ MIDDLEWARE add SLASH and resolves {old_url=} => {new_url=}")
return None
else:
if settings.DEBUG and request.method == "POST":

View File

@@ -393,7 +393,6 @@ def logbookedit(request, year=None, slug=None):
text = lbe.text
rows = max(5,len(text)/50)
print("IDENT",identified_login, who_are_you)
return render(
request,
"logbookform.html",

View File

@@ -733,7 +733,6 @@ def dwgupload(request, folder=None, gitdisable="no"):
if identified_login:
# disable editing the git id string as we get it from the logged-on user data
print(f"IDENTIFIED {identified_login}")
form.fields["who_are_you"].widget.attrs["readonly"]="readonly"
response = render(
request,