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