From c929e2fddd46dd5939a173a5455c126afb8b34b1 2017-08-16 05:16:16 From: Joshua Simmons Date: 2017-08-16 05:16:16 Subject: [PATCH] Merge branch 'master' of github.com:northbaypython/website --- diff --git a/pinaxcon/middleware.py b/pinaxcon/middleware.py new file mode 100644 index 0000000000000000000000000000000000000000..f0a60b0141375dc77a3f145cc3f408f5e49558e0 --- /dev/null +++ b/pinaxcon/middleware.py @@ -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) diff --git a/pinaxcon/settings.py b/pinaxcon/settings.py index 715d4469c6743a91874e0507ff20af2a0f945880..d568f711bd08a5f2f43a43533147b02a6ba89f37 100644 --- a/pinaxcon/settings.py +++ b/pinaxcon/settings.py @@ -15,12 +15,14 @@ DATABASES = { } } +UNPREPEND_WWW = bool(os.environ.get("DJANGO_UNPREPEND_WWW", False)) + # HEROKU: Update database configuration with $DATABASE_URL. import dj_database_url db_from_env = dj_database_url.config() 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: # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name @@ -132,6 +134,7 @@ MIDDLEWARE_CLASSES = [ "reversion.middleware.RevisionMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware", "ssl_redirect.middleware.SSLRedirectMiddleware", + "pinaxcon.middleware.UnprependWWWMiddleware", ] diff --git a/pinaxcon/templates/static_pages/homepage.html b/pinaxcon/templates/static_pages/homepage.html index 45f916433c8a315413b345b78a5cd57951a2d8cc..9ce70b89009dc02adbc8f6312bed3d3e09523fe0 100644 --- a/pinaxcon/templates/static_pages/homepage.html +++ b/pinaxcon/templates/static_pages/homepage.html @@ -114,7 +114,7 @@ {% for level in levels %} {% if level.sponsors %} {% for sponsor in level.sponsors %} -
+