Merge pull request #11 from stefanfoulis/feature/graceful-default-settings

graceful handling of unset settings
This commit is contained in:
Iacopo Spalletti 2017-01-01 18:19:59 +01:00 committed by GitHub
commit 9d07f0c714

View file

@ -9,19 +9,28 @@ from django.utils.six.moves import urllib_parse as urlparse
class CMSMultiSiteMiddleware(object):
def process_request(self, request):
MULTISITE_CMS_URLS = getattr(settings, 'MULTISITE_CMS_URLS', {})
MULTISITE_CMS_ALIASES = getattr(settings, 'MULTISITE_CMS_ALIASES', {})
MULTISITE_CMS_FALLBACK = getattr(settings, 'MULTISITE_CMS_FALLBACK', '')
try:
parsed = urlparse.urlparse(request.build_absolute_uri())
host = parsed.hostname.split(':')[0]
urlconf = None
try:
urlconf = settings.MULTISITE_CMS_URLS[host]
urlconf = MULTISITE_CMS_URLS[host]
except KeyError:
for domain, hosts in settings.MULTISITE_CMS_ALIASES.items():
if host in hosts:
urlconf = settings.MULTISITE_CMS_URLS[domain]
for domain, hosts in MULTISITE_CMS_ALIASES.items():
if host in hosts and domain in MULTISITE_CMS_URLS:
urlconf = MULTISITE_CMS_URLS[domain]
break
if not urlconf:
urlconf = settings.MULTISITE_CMS_URLS[settings.MULTISITE_CMS_FALLBACK]
if (
not urlconf and
MULTISITE_CMS_FALLBACK and
MULTISITE_CMS_FALLBACK in MULTISITE_CMS_URLS.keys()
):
urlconf = MULTISITE_CMS_URLS[MULTISITE_CMS_FALLBACK]
if urlconf:
request.urlconf = urlconf
# sets urlconf for current thread, so that code that does not know
# about the request (e.g MyModel.get_absolute_url()) get the correct
@ -32,7 +41,6 @@ class CMSMultiSiteMiddleware(object):
set_urlconf(None)
def process_response(self, request, response):
if getattr(request, 'urlconf', None):
patch_vary_headers(response, ('Host',))
# set back to default urlconf
set_urlconf(None)