From 6f17187b329e62835f47a8ecc45b82ee76f781e7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 8 Aug 2023 09:12:04 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .travis.yml | 2 - djangocms_multisite/__init__.py | 2 +- djangocms_multisite/middleware.py | 17 ++--- tests/test_middleware.py | 104 +++++++++++++++--------------- tests/test_utils/urls1.py | 14 ++-- tests/test_utils/urls2.py | 14 ++-- tests/utils.py | 5 +- 7 files changed, 73 insertions(+), 85 deletions(-) diff --git a/.travis.yml b/.travis.yml index d2cadbb..1667ff6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -62,5 +62,3 @@ matrix: cache: directories: - $HOME/.cache/pip - - diff --git a/djangocms_multisite/__init__.py b/djangocms_multisite/__init__.py index e1424ed..260c070 100644 --- a/djangocms_multisite/__init__.py +++ b/djangocms_multisite/__init__.py @@ -1 +1 @@ -__version__ = '0.3.1' +__version__ = "0.3.1" diff --git a/djangocms_multisite/middleware.py b/djangocms_multisite/middleware.py index eac275b..da93dce 100644 --- a/djangocms_multisite/middleware.py +++ b/djangocms_multisite/middleware.py @@ -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): @@ -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: @@ -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 @@ -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 diff --git a/tests/test_middleware.py b/tests/test_middleware.py index 226b9b9..c1f911a 100644 --- a/tests/test_middleware.py +++ b/tests/test_middleware.py @@ -14,87 +14,87 @@ 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()) @@ -102,37 +102,37 @@ def test_process_reponse(self): 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) diff --git a/tests/test_utils/urls1.py b/tests/test_utils/urls1.py index 540a416..f5bfbef 100644 --- a/tests/test_utils/urls1.py +++ b/tests/test_utils/urls1.py @@ -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 @@ -10,16 +10,14 @@ admin.autodiscover() urlpatterns = [ - url(r'^media/(?P.*)$', serve, - {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}), - url(r'^media/cms/(?P.*)$', 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.*)$", serve, {"document_root": settings.MEDIA_ROOT, "show_indexes": True}), + re_path(r"^media/cms/(?P.*)$", 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")), ) diff --git a/tests/test_utils/urls2.py b/tests/test_utils/urls2.py index 6efc265..cb3a1dd 100644 --- a/tests/test_utils/urls2.py +++ b/tests/test_utils/urls2.py @@ -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 @@ -10,16 +10,14 @@ admin.autodiscover() urlpatterns = [ - url(r'^media/(?P.*)$', serve, - {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}), - url(r'^media/cms/(?P.*)$', 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.*)$", serve, {"document_root": settings.MEDIA_ROOT, "show_indexes": True}), + re_path(r"^media/cms/(?P.*)$", 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")), ) diff --git a/tests/utils.py b/tests/utils.py index 0c97ed1..680166a 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -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)