mirror of
https://expo.survex.com/repositories/troggle/.git
synced 2025-12-15 03:17:12 +00:00
Attempt at append_slash, and backtrack.
This commit is contained in:
@@ -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":
|
||||
|
||||
Reference in New Issue
Block a user