From 89b6463998e10cb5ee866f5925b368684b4ba00e Mon Sep 17 00:00:00 2001 From: Stefan Foulis Date: Tue, 12 Jul 2016 11:16:37 +0200 Subject: [PATCH] graceful handling of unset settings Use blank defaults for MULTISITE_CMS_URLS, MULTISITE_CMS_ALIASES and MULTISITE_CMS_FALLBACK to minimise required configuration --- djangocms_multisite/middleware.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/djangocms_multisite/middleware.py b/djangocms_multisite/middleware.py index 763d109..113f663 100644 --- a/djangocms_multisite/middleware.py +++ b/djangocms_multisite/middleware.py @@ -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.