summaryrefslogtreecommitdiffstats
path: root/core/middleware.py
diff options
context:
space:
mode:
authorPhilip Sargent <philip.sargent@gmail.com>2025-02-18 19:59:12 +0200
committerPhilip Sargent <philip.sargent@gmail.com>2025-02-18 19:59:12 +0200
commitcc06e2e1f4bdfbf354d79055595980a1bdef495c (patch)
tree67c5bfc0356ce571a318ddc8a8a9b60300863b26 /core/middleware.py
parent95190324fbbf9e0a5ce3e324119c1d59eee99951 (diff)
downloadtroggle-cc06e2e1f4bdfbf354d79055595980a1bdef495c.tar.gz
troggle-cc06e2e1f4bdfbf354d79055595980a1bdef495c.tar.bz2
troggle-cc06e2e1f4bdfbf354d79055595980a1bdef495c.zip
Attempt at append_slash, and backtrack.
Diffstat (limited to 'core/middleware.py')
-rw-r--r--core/middleware.py50
1 files changed, 41 insertions, 9 deletions
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":