diff options
author | Philip Sargent <philip.sargent@klebos.com> | 2021-04-13 00:14:15 +0100 |
---|---|---|
committer | Philip Sargent <philip.sargent@klebos.com> | 2021-04-13 00:14:15 +0100 |
commit | ca1df94be55f321b581a6384679ff50bcb15346e (patch) | |
tree | f4080d26351ba20ce17218ddecadba795ff72e33 /core/middleware.py | |
parent | 2a1710596a48ce0dbd2b4aa5f510f0c79bd57b99 (diff) | |
download | troggle-ca1df94be55f321b581a6384679ff50bcb15346e.tar.gz troggle-ca1df94be55f321b581a6384679ff50bcb15346e.tar.bz2 troggle-ca1df94be55f321b581a6384679ff50bcb15346e.zip |
moved clever slash middleware & unused.py
Diffstat (limited to 'core/middleware.py')
-rw-r--r-- | core/middleware.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/core/middleware.py b/core/middleware.py new file mode 100644 index 0000000..56078a9 --- /dev/null +++ b/core/middleware.py @@ -0,0 +1,68 @@ +from django.conf import settings +from django import http +from django.urls import reverse, resolve,Resolver404 +"""Non-standard django middleware is loaded from this file. +""" + +class SmartAppendSlashMiddleware(object): + """ + "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. + Otherwise it won't touch the URL. + """ + + 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 / + ''' + if not settings.SMART_APPEND_SLASH: + return None + + if request.path.endswith('/'): + return None + + if request.path.endswith('_edit'): + return None + + host = http.HttpRequest.get_host(request) + old_url = [host, request.path] + if _resolves(old_url[1]): + return None + + # So: it does not resolve according to our criteria, i.e. _edit doesn't count + new_url = old_url[:] + new_url[1] = new_url[1] + '/' + if not _resolves(new_url[1]): + return None + else: + if settings.DEBUG and request.method == 'POST': + # replace this exception with a redirect to an error page + raise RuntimeError("You called this URL via POST, but the URL doesn't end in a slash and you have SMART_APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to %s%s (note the trailing slash), or set SMART_APPEND_SLASH=False in your Django settings." % (new_url[0], new_url[1])) + if new_url != old_url: + # Redirect + if new_url[0]: + newurl = "%s://%s%s" % (request.is_secure() and 'https' or 'http', new_url[0], new_url[1]) + else: + newurl = new_url[1] + if request.GET: + newurl += '?' + request.GET.urlencode() + return http.HttpResponsePermanentRedirect(newurl) + + return None + +def _resolves(url): + try: + # If the URL does not resolve, the function raises a Resolver404 exception (a subclass of Http404) + match = resolve(url) + # this will ALWAYS be resolved by expopages because it will produce pagenotfound if not the thing asked for + # so handle this in expopages, not in middleware + return True + except Resolver404: + return False + except: + print(url) + raise |