diff options
author | Philip Sargent <philip.sargent@gmail.com> | 2025-02-18 19:59:12 +0200 |
---|---|---|
committer | Philip Sargent <philip.sargent@gmail.com> | 2025-02-18 19:59:12 +0200 |
commit | cc06e2e1f4bdfbf354d79055595980a1bdef495c (patch) | |
tree | 67c5bfc0356ce571a318ddc8a8a9b60300863b26 /core | |
parent | 95190324fbbf9e0a5ce3e324119c1d59eee99951 (diff) | |
download | troggle-cc06e2e1f4bdfbf354d79055595980a1bdef495c.tar.gz troggle-cc06e2e1f4bdfbf354d79055595980a1bdef495c.tar.bz2 troggle-cc06e2e1f4bdfbf354d79055595980a1bdef495c.zip |
Attempt at append_slash, and backtrack.
Diffstat (limited to 'core')
-rw-r--r-- | core/context.py | 2 | ||||
-rw-r--r-- | core/middleware.py | 50 | ||||
-rw-r--r-- | core/views/logbook_edit.py | 1 | ||||
-rw-r--r-- | core/views/uploads.py | 1 |
4 files changed, 42 insertions, 12 deletions
diff --git a/core/context.py b/core/context.py index e410fd6..4e11f35 100644 --- a/core/context.py +++ b/core/context.py @@ -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 diff --git a/core/middleware.py b/core/middleware.py index 686d7d5..2b280a1 100644 --- a/core/middleware.py +++ b/core/middleware.py @@ -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": diff --git a/core/views/logbook_edit.py b/core/views/logbook_edit.py index a5c194f..198cc38 100644 --- a/core/views/logbook_edit.py +++ b/core/views/logbook_edit.py @@ -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",
diff --git a/core/views/uploads.py b/core/views/uploads.py index d65a803..12a3989 100644 --- a/core/views/uploads.py +++ b/core/views/uploads.py @@ -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, |