Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Aug 8, 2023
1 parent 8ef0dd8 commit 6f17187
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 85 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,3 @@ matrix:
cache:
directories:
- $HOME/.cache/pip


2 changes: 1 addition & 1 deletion djangocms_multisite/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.3.1'
__version__ = "0.3.1"
17 changes: 6 additions & 11 deletions djangocms_multisite/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@


class CMSMultiSiteMiddleware(MiddlewareMixin):

@staticmethod
def _get_sites():
return getattr(settings, 'MULTISITE_CMS_URLS', {})
return getattr(settings, "MULTISITE_CMS_URLS", {})

@staticmethod
def _get_aliases():
return getattr(settings, 'MULTISITE_CMS_ALIASES', {})
return getattr(settings, "MULTISITE_CMS_ALIASES", {})

@classmethod
def _get_domain(cls, request):
Expand All @@ -25,7 +24,7 @@ def _get_domain(cls, request):
sites = cls._get_sites()
aliases = cls._get_aliases()
parsed = urlparse(request.build_absolute_uri())
host = parsed.hostname.split(':')[0]
host = parsed.hostname.split(":")[0]
if host in sites:
return host
else:
Expand All @@ -44,16 +43,12 @@ def _get_urlconf(cls, domain):
resulting in setting the default urlconf.
"""
sites = cls._get_sites()
MULTISITE_CMS_FALLBACK = getattr(settings, 'MULTISITE_CMS_FALLBACK', '') # noqa
MULTISITE_CMS_FALLBACK = getattr(settings, "MULTISITE_CMS_FALLBACK", "") # noqa
try:
urlconf = sites[domain]
except KeyError:
urlconf = None
if (
not urlconf and
MULTISITE_CMS_FALLBACK and
MULTISITE_CMS_FALLBACK in sites.keys()
):
if not urlconf and MULTISITE_CMS_FALLBACK and MULTISITE_CMS_FALLBACK in sites.keys():
urlconf = sites[MULTISITE_CMS_FALLBACK]
return urlconf

Expand All @@ -68,7 +63,7 @@ def process_request(self, request):
reload_urlconf()

def process_response(self, request, response):
patch_vary_headers(response, ('Host',))
patch_vary_headers(response, ("Host",))
# set back to default urlconf
set_urlconf(None)
return response
104 changes: 52 additions & 52 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,125 +14,125 @@
class CMSMultiSiteMiddlewareTest(BaseTestCase):
def setUp(self):
Site.objects.all().delete()
self.site = Site.objects.create(pk=1, domain='www.example.com')
self.site2 = Site.objects.create(pk=2, domain='www.example2.com')
self.site = Site.objects.create(pk=1, domain="www.example.com")
self.site2 = Site.objects.create(pk=2, domain="www.example2.com")

def test_match_domain(self):
"""Resolve the request domain against the list of configured main and aliases."""
request = RequestFactory(host='www.example.com').get('/')
self.assertEqual(CMSMultiSiteMiddleware._get_domain(request), 'www.example.com')
request = RequestFactory(host="www.example.com").get("/")
self.assertEqual(CMSMultiSiteMiddleware._get_domain(request), "www.example.com")

request = RequestFactory(host='alias1.example.com').get('/')
self.assertEqual(CMSMultiSiteMiddleware._get_domain(request), 'www.example.com')
request = RequestFactory(host="alias1.example.com").get("/")
self.assertEqual(CMSMultiSiteMiddleware._get_domain(request), "www.example.com")

request = RequestFactory(host='alias3.example.com').get('/')
request = RequestFactory(host="alias3.example.com").get("/")
self.assertEqual(CMSMultiSiteMiddleware._get_domain(request), None)

request = RequestFactory(host='blabla.com').get('/')
request = RequestFactory(host="blabla.com").get("/")
self.assertEqual(CMSMultiSiteMiddleware._get_domain(request), None)

request = RequestFactory(host='www.example2.com').get('/')
self.assertEqual(CMSMultiSiteMiddleware._get_domain(request), 'www.example2.com')
request = RequestFactory(host="www.example2.com").get("/")
self.assertEqual(CMSMultiSiteMiddleware._get_domain(request), "www.example2.com")

request = RequestFactory(host='alias2.example2.com').get('/')
self.assertEqual(CMSMultiSiteMiddleware._get_domain(request), 'www.example2.com')
request = RequestFactory(host="alias2.example2.com").get("/")
self.assertEqual(CMSMultiSiteMiddleware._get_domain(request), "www.example2.com")

# port is always ignored, only domain is considered
request = RequestFactory(host='alias2.example2.com:8000').get('/')
self.assertEqual(CMSMultiSiteMiddleware._get_domain(request), 'www.example2.com')
request = RequestFactory(host="alias2.example2.com:8000").get("/")
self.assertEqual(CMSMultiSiteMiddleware._get_domain(request), "www.example2.com")

# don't set port in MULTISITE_CMS_ALIASES, otherwise it will not be matched
request = RequestFactory(host='alias3.example2.com:8000').get('/')
request = RequestFactory(host="alias3.example2.com:8000").get("/")
self.assertEqual(CMSMultiSiteMiddleware._get_domain(request), None)

def test_match_urlconf(self):
"""Match main domain return the correct one - Any other domain -including alias- return the default."""
self.assertEqual(CMSMultiSiteMiddleware._get_urlconf('www.example.com'), 'tests.test_utils.urls1')
self.assertEqual(CMSMultiSiteMiddleware._get_urlconf('www.example2.com'), 'tests.test_utils.urls2')
self.assertEqual(CMSMultiSiteMiddleware._get_urlconf('alias1.example.com'), 'tests.test_utils.urls1')
self.assertEqual(CMSMultiSiteMiddleware._get_urlconf('alias2.example2.com'), 'tests.test_utils.urls1')
self.assertEqual(CMSMultiSiteMiddleware._get_urlconf("www.example.com"), "tests.test_utils.urls1")
self.assertEqual(CMSMultiSiteMiddleware._get_urlconf("www.example2.com"), "tests.test_utils.urls2")
self.assertEqual(CMSMultiSiteMiddleware._get_urlconf("alias1.example.com"), "tests.test_utils.urls1")
self.assertEqual(CMSMultiSiteMiddleware._get_urlconf("alias2.example2.com"), "tests.test_utils.urls1")

@override_settings(SITE_ID=1)
def test_process_site_1(self):
request = RequestFactory(host='www.example.com').get('/')
request = RequestFactory(host="www.example.com").get("/")
CMSMultiSiteMiddleware().process_request(request)
self.assertEqual(get_urlconf(), 'tests.test_utils.urls1')
self.assertEqual(get_urlconf(), "tests.test_utils.urls1")

request = RequestFactory(host='alias1.example.com').get('/')
request = RequestFactory(host="alias1.example.com").get("/")
CMSMultiSiteMiddleware().process_request(request)
self.assertEqual(get_urlconf(), 'tests.test_utils.urls1')
self.assertEqual(get_urlconf(), "tests.test_utils.urls1")

@override_settings(SITE_ID=2)
def test_process_site_2(self):
request = RequestFactory(host='www.example2.com').get('/')
request = RequestFactory(host="www.example2.com").get("/")
CMSMultiSiteMiddleware().process_request(request)
self.assertEqual(get_urlconf(), 'tests.test_utils.urls2')
self.assertEqual(get_urlconf(), "tests.test_utils.urls2")

request = RequestFactory(host='alias2.example2.com').get('/')
request = RequestFactory(host="alias2.example2.com").get("/")
CMSMultiSiteMiddleware().process_request(request)
self.assertEqual(get_urlconf(), 'tests.test_utils.urls2')
self.assertEqual(get_urlconf(), "tests.test_utils.urls2")

# don't set port in MULTISITE_CMS_ALIASES, otherwise it will not be matched
request = RequestFactory(host='alias3.example2.com').get('/')
request = RequestFactory(host="alias3.example2.com").get("/")
CMSMultiSiteMiddleware().process_request(request)
self.assertEqual(get_urlconf(), 'tests.test_utils.urls1')
self.assertEqual(get_urlconf(), "tests.test_utils.urls1")

# don't set port in MULTISITE_CMS_ALIASES, otherwise it will not be matched
request = RequestFactory(host='alias3.example2.com:8000').get('/')
request = RequestFactory(host="alias3.example2.com:8000").get("/")
CMSMultiSiteMiddleware().process_request(request)
self.assertEqual(get_urlconf(), 'tests.test_utils.urls1')
self.assertEqual(get_urlconf(), "tests.test_utils.urls1")

@override_settings(SITE_ID=2)
def test_process_reponse(self):
request = RequestFactory(host='www.example2.com').get('/')
request = RequestFactory(host="www.example2.com").get("/")
CMSMultiSiteMiddleware().process_request(request)
self.assertEqual(get_urlconf(), 'tests.test_utils.urls2')
CMSMultiSiteMiddleware().process_response(request, HttpResponse(''))
self.assertEqual(get_urlconf(), "tests.test_utils.urls2")
CMSMultiSiteMiddleware().process_response(request, HttpResponse(""))
# Default is restored after request is processed
self.assertIsNone(get_urlconf())

request = RequestFactory(host='alias2.example2.com').get('/')
request = RequestFactory(host="alias2.example2.com").get("/")
CMSMultiSiteMiddleware().process_request(request)
self.assertEqual(get_urlconf(), 'tests.test_utils.urls2')
CMSMultiSiteMiddleware().process_response(request, HttpResponse(''))
self.assertEqual(get_urlconf(), "tests.test_utils.urls2")
CMSMultiSiteMiddleware().process_response(request, HttpResponse(""))
# Default is restored after request is processed
self.assertIsNone(get_urlconf())


class CMSMultiSiteMiddlewareAliasTest(BaseTestCase):
def setUp(self):
Site.objects.all().delete()
self.site = Site.objects.create(pk=1, domain='www.example.com')
self.site2 = Site.objects.create(pk=2, domain='www.example2.com')
Alias.objects.create(domain='alias1.example.com', site=self.site)
Alias.objects.create(domain='alias2.example.com', site=self.site, redirect_to_canonical=True)
self.site = Site.objects.create(pk=1, domain="www.example.com")
self.site2 = Site.objects.create(pk=2, domain="www.example2.com")
Alias.objects.create(domain="alias1.example.com", site=self.site)
Alias.objects.create(domain="alias2.example.com", site=self.site, redirect_to_canonical=True)

Alias.objects.create(domain='alias1.example2.com', site=self.site2)
Alias.objects.create(domain='alias2.example2.com', site=self.site2, redirect_to_canonical=True)
Alias.objects.create(domain="alias1.example2.com", site=self.site2)
Alias.objects.create(domain="alias2.example2.com", site=self.site2, redirect_to_canonical=True)

def test_process_site_1(self):
request = RequestFactory(host='www.example.com').get('/')
request = RequestFactory(host="www.example.com").get("/")
DynamicSiteMiddleware().process_request(request)
CMSMultiSiteMiddleware().process_request(request)
self.assertEqual(get_urlconf(), 'tests.test_utils.urls1')
self.assertEqual(get_urlconf(), "tests.test_utils.urls1")

request = RequestFactory(host='alias1.example.com').get('/')
request = RequestFactory(host="alias1.example.com").get("/")
DynamicSiteMiddleware().process_request(request)
CMSMultiSiteMiddleware().process_request(request)
self.assertEqual(get_urlconf(), 'tests.test_utils.urls1')
self.assertEqual(get_urlconf(), "tests.test_utils.urls1")

def test_process_site_2(self):
request = RequestFactory(host='www.example2.com').get('/')
request = RequestFactory(host="www.example2.com").get("/")
DynamicSiteMiddleware().process_request(request)
CMSMultiSiteMiddleware().process_request(request)
self.assertEqual(get_urlconf(), 'tests.test_utils.urls2')
self.assertEqual(get_urlconf(), "tests.test_utils.urls2")

request = RequestFactory(host='alias2.example2.com').get('/')
request = RequestFactory(host="alias2.example2.com").get("/")
DynamicSiteMiddleware().process_request(request)
CMSMultiSiteMiddleware().process_request(request)
self.assertEqual(get_urlconf(), 'tests.test_utils.urls2')
self.assertEqual(get_urlconf(), "tests.test_utils.urls2")

# aliases not configured on django-multisite will not be recognizes
request = RequestFactory(host='alias3.example2.com').get('/')
request = RequestFactory(host="alias3.example2.com").get("/")
with self.assertRaises(Http404):
DynamicSiteMiddleware().process_request(request)
14 changes: 6 additions & 8 deletions tests/test_utils/urls1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from cms.utils.conf import get_cms_setting
from django.conf import settings
from django.conf.urls import include, url
from django.urls import include, path, re_path
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
Expand All @@ -10,16 +10,14 @@
admin.autodiscover()

urlpatterns = [
url(r'^media/(?P<path>.*)$', serve,
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
url(r'^media/cms/(?P<path>.*)$', serve,
{'document_root': get_cms_setting('MEDIA_ROOT'), 'show_indexes': True}),
url(r'^jsi18n/$', JavaScriptCatalog.as_view(), name='javascript-catalog'),
re_path(r"^media/(?P<path>.*)$", serve, {"document_root": settings.MEDIA_ROOT, "show_indexes": True}),
re_path(r"^media/cms/(?P<path>.*)$", serve, {"document_root": get_cms_setting("MEDIA_ROOT"), "show_indexes": True}),
path("jsi18n/", JavaScriptCatalog.as_view(), name="javascript-catalog"),
]

urlpatterns += staticfiles_urlpatterns()

urlpatterns += i18n_patterns(
url(r'^admin/', admin.site.urls),
url(r'^', include('cms.urls')),
re_path(r"^admin/", admin.site.urls),
path("", include("cms.urls")),
)
14 changes: 6 additions & 8 deletions tests/test_utils/urls2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from cms.utils.conf import get_cms_setting
from django.conf import settings
from django.conf.urls import include, url
from django.urls import include, path, re_path
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
Expand All @@ -10,16 +10,14 @@
admin.autodiscover()

urlpatterns = [
url(r'^media/(?P<path>.*)$', serve,
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
url(r'^media/cms/(?P<path>.*)$', serve,
{'document_root': get_cms_setting('MEDIA_ROOT'), 'show_indexes': True}),
url(r'^jsi18n/$', JavaScriptCatalog.as_view(), name='javascript-catalog'),
re_path(r"^media/(?P<path>.*)$", serve, {"document_root": settings.MEDIA_ROOT, "show_indexes": True}),
re_path(r"^media/cms/(?P<path>.*)$", serve, {"document_root": get_cms_setting("MEDIA_ROOT"), "show_indexes": True}),
path("jsi18n/", JavaScriptCatalog.as_view(), name="javascript-catalog"),
]

urlpatterns += staticfiles_urlpatterns()

urlpatterns += i18n_patterns(
url(r'^admin/', admin.site.urls),
url(r'^subpath/', include('cms.urls')),
re_path(r"^admin/", admin.site.urls),
path("subpath/", include("cms.urls")),
)
5 changes: 2 additions & 3 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@


class RequestFactory(DjangoRequestFactory):

# borrowed from django-multisite, we can't import it directly due to pytest upstream dependencies
def __init__(self, host):
super(RequestFactory, self).__init__()
super().__init__()
self.host = host

def get(self, path, data=None, host=None, **extra):
if host is None:
host = self.host
return super(RequestFactory, self).get(path=path, data=data, HTTP_HOST=host, **extra)
return super().get(path=path, data=data, HTTP_HOST=host, **extra)

0 comments on commit 6f17187

Please sign in to comment.