graceful handling of unset settings

Use blank defaults for MULTISITE_CMS_URLS, MULTISITE_CMS_ALIASES and 
MULTISITE_CMS_FALLBACK to minimise required configuration
This commit is contained in:
Stefan Foulis 2016-07-12 11:16:37 +02:00
parent 35397dd4ff
commit 89b6463998

View file

@ -9,20 +9,29 @@ 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]
request.urlconf = urlconf
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
# urlconf.