Adds UNPREPEND_WWW
, because why not?
This commit is contained in:
parent
e364d5fafa
commit
5db3a22dbb
2 changed files with 38 additions and 1 deletions
34
pinaxcon/middleware.py
Normal file
34
pinaxcon/middleware.py
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
import re
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
from django import http
|
||||||
|
from django.conf import settings
|
||||||
|
from django.utils.deprecation import MiddlewareMixin
|
||||||
|
|
||||||
|
class UnprependWWWMiddleware(MiddlewareMixin):
|
||||||
|
""" Unprepends www if necessary. """
|
||||||
|
|
||||||
|
response_redirect_class = http.HttpResponsePermanentRedirect
|
||||||
|
|
||||||
|
def process_request(self, request):
|
||||||
|
"""
|
||||||
|
Rewrite the URL based on settings.UNPREPEND_WWW
|
||||||
|
"""
|
||||||
|
|
||||||
|
unprepend_www = getattr(settings, "UNPREPEND_WWW", False)
|
||||||
|
|
||||||
|
if not unprepend_www:
|
||||||
|
return
|
||||||
|
|
||||||
|
# Check for a redirect based on settings.UNPREPEND_WWW
|
||||||
|
host = request.get_host()
|
||||||
|
must_unprepend = unprepend_www and host and host.lower().startswith('www.')
|
||||||
|
wwwless_host = host[4:]
|
||||||
|
redirect_url = ('%s://%s' % (request.scheme, wwwless_host)) if must_unprepend else ''
|
||||||
|
|
||||||
|
path = request.get_full_path()
|
||||||
|
|
||||||
|
# Return a redirect if necessary
|
||||||
|
if redirect_url or path != request.get_full_path():
|
||||||
|
redirect_url += path
|
||||||
|
return self.response_redirect_class(redirect_url)
|
|
@ -15,12 +15,14 @@ DATABASES = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UNPREPEND_WWW = bool(os.environ.get("DJANGO_UNPREPEND_WWW", False))
|
||||||
|
|
||||||
# HEROKU: Update database configuration with $DATABASE_URL.
|
# HEROKU: Update database configuration with $DATABASE_URL.
|
||||||
import dj_database_url
|
import dj_database_url
|
||||||
db_from_env = dj_database_url.config()
|
db_from_env = dj_database_url.config()
|
||||||
DATABASES['default'].update(db_from_env)
|
DATABASES['default'].update(db_from_env)
|
||||||
|
|
||||||
ALLOWED_HOSTS = ["localhost", ".herokuapp.com", ".northbaypython.org"]
|
ALLOWED_HOSTS = [".localhost", ".herokuapp.com", ".northbaypython.org"]
|
||||||
|
|
||||||
# Local time zone for this installation. Choices can be found here:
|
# Local time zone for this installation. Choices can be found here:
|
||||||
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
|
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
|
||||||
|
@ -132,6 +134,7 @@ MIDDLEWARE_CLASSES = [
|
||||||
"reversion.middleware.RevisionMiddleware",
|
"reversion.middleware.RevisionMiddleware",
|
||||||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||||
"ssl_redirect.middleware.SSLRedirectMiddleware",
|
"ssl_redirect.middleware.SSLRedirectMiddleware",
|
||||||
|
"pinaxcon.middleware.UnprependWWWMiddleware",
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue